簡體   English   中英

如何使我的do while循環與用戶提示一起工作?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM