這是一個簡單的java猜數字遊戲。重新開始簡單的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)");
}
}
您需要使用'do ... while'循環 – user902383 2014-09-11 10:04:59
呃,冗餘太多。創建一個方法來接受一個整數,並將你的if-elseif-else登錄到那裏,並返回一個帶有響應的String來打印出來。 'System.out.println(checkGuess(guess1.nextInt()));' – 2014-09-11 10:05:12
像@ user902383所提到的。 「儘管」就足夠了。您可能需要稍微清理一下代碼,以使其更易於閱讀。 – Bahamut 2014-09-11 10:12:21