[英]How do I make my do while loop with user prompt work?
我正在创建一个基本上可以用作猜测游戏的应用程序。 在游戏结束时,我想问用户是否要再次玩。 如果他们键入“ Y”或“ y”,则游戏将重新启动,如果输入了其他任何字符,则循环结束。
public class NumberGame {
public static void main(String[] args) throws java.io.IOException {
int NumberGame;
int NumberUser;
char BonusResponse;
char charGen;
String Guess = " ";
String Bonus = " ";
do {
NumberGame = (int) (Math.random() * 10);
charGen = (char)(NumberGame + 48);
//for(NumberGame < 1; NumberGame++){
System.out.print("The computer generated number was " + NumberGame);
System.out.print("\nGuess a number between 1 and 9: ");
NumberUser = (char) System.in.read();
NumberUser = (int) (NumberUser - 48);
if (NumberGame > NumberUser)
Guess = " Too low! Try again";
else if (NumberGame == NumberUser)
Guess = " Congratulations, you win!";
else if (NumberGame < NumberUser)
Guess = " Too high! Try again";
System.out.println("You guessed " + NumberUser + "." + Guess);
if (NumberGame == NumberUser)
Bonus = "Now that you've won, wanna play again? Type Y to play again or any other key to exit:";
else
Bonus = " ";
System.out.println(Bonus);
BonusResponse = (char) System.in.read();
} while (BonusResponse == 'Y' || BonusResponse == 'y');
}
}
您可以使用Scanner类而不是普通的System.in.read()
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
String response= sc.next();
您可以使用扫描仪类在此所看到的答案或BufferedReader中上所看到这个答案 。
BufferedReader
类的示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Areas {
public static void main(String args[]){
float PI = 3.1416f;
int r=0;
String rad; //We're going to read all user's text into a String and we try to convert it later
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //Here you declare your BufferedReader object and instance it.
System.out.println("Radius?");
try{
rad = br.readLine(); //We read from user's input
r = Integer.parseInt(rad); //We validate if "rad" is an integer (if so we skip catch call and continue on the next line, otherwise, we go to it (catch call))
System.out.println("Circle area is: " + PI*r*r + " Perimeter: " +PI*2*r); //If all was right, we print this
}
catch(Exception e){
System.out.println("Write an integer number"); //This is what user will see if he/she write other thing that is not an integer
Areas a = new Areas(); //We call this class again, so user can try it again
//You can also print exception in case you want to see it as follows:
// e.printStackTrace();
}
}
}
Scanner
类的示例:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
//For string
String text= scan.nextLine();
System.out.println(text);
//for int
int num= scan.nextInt();
System.out.println(num);
}
}
您可以尝试在do-while
循环内添加阅读代码以验证用户输入。 然后在您的while (BonusResponse == 'Y' || BonusResponse == 'y');
后添加此代码while (BonusResponse == 'Y' || BonusResponse == 'y');
if (BonusResponse == 'y' || BonusResponse == 'Y') {
startApplication();
}
将所有main()
代码移动到startApplication()
方法。 并将main更改为:
public static void main(String args[]) {
startApplication();
}
另外,请遵循Java命名约定 :
除变量外,所有实例,类和类常量的大小写都混合使用首字母小写 。 内部单词以大写字母开头。 变量名称不应以下划线_或美元符号$字符开头,即使两者都允许。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.