如何使用用户提示工作进行while循环?

时间:2015-10-01 18:50:18

标签: java do-while

我正在创建一个基本上可以作为猜谜游戏的应用程序。在游戏结束时,我想询问用户是否想再次玩游戏。如果他们输入" 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');   
  }
}

2 个答案:

答案 0 :(得分:0)

您可以使用扫描仪类而不是普通的System.in.read()

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
String response= sc.next();

答案 1 :(得分:0)

您可以使用此Scanneranswer上显示的BufferedReader课程,如this answer所示。

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');

之后添加此代码
if (BonusResponse == 'y' || BonusResponse == 'Y') {
    startApplication();
}

将您的所有main()代码移至startApplication()方法。并将主要更改为:

public static void main(String args[]) {
    startApplication();
}

另请关注Java naming conventions

  

变量外,所有实例,类和类常量都在   带有小写首字母的混合大小写。内部词开头   大写字母。变量名称不应以下划线_或开头   美元符号$字符,即使两者都被允许。