[英]Restart simple java game
這是一個簡單的Java數字猜謎游戲。
游戲結束后,我想問用戶是否要再次玩,例如:您想再次玩嗎? (用Y / N回答)如果用戶輸入Y,則游戲將重新啟動並隨機分配一個新的數字,如果他回答“ N”,則游戲將結束。 這是我的代碼:
import static java.lang.Math.*;
import java.util.Scanner;
public class HomeWorkLoopGame1 {
public static void main(String[] args) {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random()*100);
Scanner guess1 = new Scanner (System.in);
System.out.println("To begin please input your number");
int num1 = guess1.nextInt();
if (num1<answer) {
System.out.print(num1);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your second guess");
}
else if (num1>answer){
System.out.println(num1);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your second guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num2 = guess1.nextInt();
if (num2<answer) {
System.out.print(num2);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your third guess");
}
else if (num2>answer){
System.out.println(num2);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your third guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num3 = guess1.nextInt();
if (num3<answer) {
System.out.print(num3);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your fourth guess");
}
else if (num3>answer){
System.out.println(num3);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your fourth guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num4 = guess1.nextInt();
if (num4<answer) {
System.out.print(num4);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your final guess");
}
else if (num4>answer){
System.out.println(num4);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your final guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num5= guess1.nextInt();
if (num5<answer) {
System.out.print(num5);
System.out.print(" Your answer is too small\n");
System.out.println("Game Over");
}
else if (num5>answer){
System.out.println(num5);
System.out.print("Your answer is too big\n");
System.out.println("Game Over");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
System.out.println("Correct Answer is "+ answer);
Scanner userReply = new Scanner (System.in);
char reply;
System.out.println("Do you want to play again? Answer with Y/N)");
}
}
您可以嘗試這樣。 順便說一句,這不是執行此操作的好方法。
Scanner userReply = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with Y/N)");
String answer= userReply.nextLine().toLowerCase();
if("y".equals(answer)){
main(args); // recursively call
}else {
System.out.println("game exit");
}
您可以使用do-while
以正確的方式進行操作。
更好的方法
public static void main(String[] args) {
boolean isValid;
do {
isValid = true;
runGame();
Scanner userReply = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with (Y/N)");
String answer = userReply.nextLine().toLowerCase();
if ("y".equals(answer)) {
isValid =true;
} else {
isValid =false;
System.out.println("game exit");
}
} while (isValid);
}
public static void runGame() {
// your game logic
}
您可以這樣調用類的main方法並按如下方式創建類的實例:
HomeWorkLoopGame1 variable = new HomeWorkLoopGame1();
HomeWorkLoopGame1.main(args);
基本上,它從頭開始重新運行游戲-就像玩家第一次開始再次玩游戲一樣。
就像這樣:
Scannner in = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with Y/N");
String answer = in.nextLine().toLowerCase();
if(answer.equals.("Y")){
HomeWorkLoopGame1 variable = new HomeWorkLoopGame1();
HomeWorkLoopGame1.main(args);
}
else{
//exit
}
但是,我尚未對此進行測試,因此我不確定100%是否可行。 祝你好運
編輯:-將代碼包裝在for循環中以重復多次,而不僅僅是重復一次
(我認為)最好為播放功能創建一種方法。
public static void main(String[] args) {
playGame ();
System.out.print("Enter something:");
String input = System.console().readLine();
if (input=="y"){
playGame();
} else {
System.exit(0);
}
}
private void playGame() {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random()*100);
Scanner guess1 = new Scanner (System.in);
....
System.out.println("Correct Answer is "+ answer);
Scanner userReply = new Scanner (System.in);
char reply;
System.out.println("Do you want to play again? Answer with Y/N)");
}
只需在主方法中放入一個do-while循環即可。
如第一行所示:
do {
在System.out.println("Do you want to play again? Answer with Y/N)");
放:
while (reply=='Y' or reply=='y');
當然,您需要確定自己在閱讀回復中是否正確。
您可以循環執行整個操作:
public static void main(String[] args) {
while (true) {
// your code goes here
System.out.println("Do you want to play again? Answer with Y/N)");
try {
char reply;
reply = (char) System.in.read(); // read a char
if (Character.toLowerCase(reply) != 'y') // when equals to Y break the loop
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我嘗試不對您的原始代碼進行太多更改。 我添加了一些while循環,一些注釋和其他代碼來處理用戶的響應。 希望對您有所幫助。
import static java.lang.Math.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
boolean play = true;
boolean notCorrect = true;
while (play) {
while (notCorrect) {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random() * 100);
Scanner guess1 = new Scanner(System.in);
//Guess 1
System.out.println("To begin please input your number");
int num1 = guess1.nextInt();
if (num1 < answer) {
System.out.print(num1);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your second guess");
} else if (num1 > answer) {
System.out.println(num1);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your second guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 2
int num2 = guess1.nextInt();
if (num2 < answer) {
System.out.print(num2);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your third guess");
} else if (num2 > answer) {
System.out.println(num2);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your third guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 3
int num3 = guess1.nextInt();
if (num3 < answer) {
System.out.print(num3);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your fourth guess");
} else if (num3 > answer) {
System.out.println(num3);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your fourth guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 4
int num4 = guess1.nextInt();
if (num4 < answer) {
System.out.print(num4);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your final guess");
} else if (num4 > answer) {
System.out.println(num4);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your final guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 5
int num5 = guess1.nextInt();
if (num5 < answer) {
System.out.print(num5);
System.out.print(" Your answer is too small\n");
System.out.println("Game Over");
} else if (num5 > answer) {
System.out.println(num5);
System.out.print("Your answer is too big\n");
System.out.println("Game Over");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
System.out.println("Correct Answer is " + answer);
break;
} // while(correct)
Scanner userReply = new Scanner(System.in);
String reply;
System.out.println("Do you want to play again? Answer with (Y/N)");
reply = userReply.next().toLowerCase();
if (reply.equals("y")) {
play = true;
} else if (reply.equals("n")) {
play = false;
} else {
System.out.println("Invalid choice.");
break;
}
} // while(play)
} // main()
} // class Test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.