簡體   English   中英

你如何提示用戶在java中輸入

[英]How do you prompt a user for an input in java

所以我剛開始學習 Java,這就像我的第一天,我想嘗試制作一個投幣游戲。 我已經了解相當多的 Javascript,所以我試圖將這些知識應用到 Java 中。 所以到目前為止一切都在工作,除了一件事:提示用戶進行選擇。 所以在線閱讀我必須導入掃描儀所以我這樣做了,正如您從我的代碼中看到的那樣。 我還嘗試了一些代碼,您可以讓用戶導入一個字符串,但稍后您可以在我的程序中看到我將變量 userChoice 更改為一個數字。 所以基本上我只需要幫助。 如果有某種方法可以擁有可以存儲數字或字符串的變量類型,那將是最好的。 但我完全願意接受其他方式來做到這一點:在此先感謝! 這是代碼:

package test;
import java.util.Scanner;
public class testclass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hi");
        int bob;
        bob = (int) Math.floor(Math.random()*2);
        System.out.println(bob);


          System.out.println("Enter heads or tails?");
          System.out.println("You entered "+ userChoice);

          if (bob == 0) {
            System.out.println("Computer flipped heads"); 
          }

          else {
              System.out.println("Computer flipped tails");
          }



          if(userChoice == "Heads") {
              userChoice = 0;

          }

          else {
              userChoice = 1;
          }



          if (userChoice == bob) {
              System.out.println("You win!");
          }


          else {
              System.out.println("Sorry you lost!")

          }


          }

    }

如您所說,使用掃描儀:

Scanner in = new Scanner(System.in);

然后,提示用戶輸入一些東西:

String userChoice = in.nextLine();

此外,當您比較字符串時:

if(userChoice == "Heads") {...

這對非原始對象來說是不好的。 最好只使用==來比較intenum的值。 如果你像這樣比較一個字符串,它不會工作,因為它正在檢查對象是否相同。 相反,像這樣比較:

if(userChoice.equals("Heads")) {...

此外,要轉換為 int (注意:您不能將一種類型的對象轉換為另一種不相關的對象,如果您想這樣做,則必須創建一個新對象):這個:

int myInt = Integer.parseInt(myString); // NOTE: Can throw NumberFormatException if non-number character is found.

所以你的程序應該看起來像:

    package test;
    import java.util.Scanner;

    public class testclass {

        public static void main(String[] args) {
            //System.out.println("hi");
            Scanner in = new Scanner(System.in);
            int bob;
            int userChoice;
            String input;
            bob = (int) Math.floor(Math.random()*2);
            System.out.println(bob);

            System.out.println("Enter heads or tails?");
            input = in.nextLine(); // waits for user to press enter.
            System.out.println("You entered "+ input);

            if (bob == 0) {
                System.out.println("Computer flipped heads"); 
            }

            else {
                System.out.println("Computer flipped tails");
            }

            if(input.equals("Heads")) {
                userChoice = 0;
            }
            else {
                userChoice = 1;
            }

            if (userChoice == bob) {
                System.out.println("You win!");
            }
            else {
                System.out.println("Sorry you lost!");
            }

            in.close(); // IMPORTANT to prevent memory leaks
        }
    }

您已經導入了 Scanner 類,因此您現在可以創建一個 Scanner 類型的變量來獲取輸入。

 Scanner in = new Scanner();
 userChoice = in.nextLine();

nextLine()可用於從用戶輸入字符或字符串。

要將字符串轉換為整數,您可以通過以下方式將整數值分配給字符串。

   if(userChoice == "Heads") {
             userChoice = "" + 0;
          }
      else {
             userChoice = "" + 1;
      }

導入 java.util.Scanner 后,為了從用戶那里獲取字符串形式的輸入,創建一個參數化 System.in 的 Scanner 對象,並為 userChoice 分配由 Scanner 對象調用的 nextLine() 的值:

Scanner input = new Scanner(System.in);
String userChoice = input.nextLine();

關於您的代碼的一些事情。 關系運算符==用於比較原始數據 - 而不是對象。 使用string1.equals(string2)查看兩個字符串是否相等。 另外, bob = (int) Math.floor(Math.random()*2); 真的是bob = (int)(Math.random() * 2); 因為將 double 轉換為整數會將 double 截斷為小於或等於它的最高整數。

Java 中的“字符串”數據類型可以包含數字和字符串(如您所問)。 您可以使用 Scanner 實用程序獲取用戶輸入,如下所示:

Scanner input = new Scanner();
userChoice = input.nextLine(); // if it is a string 
//userChoice = input.nextInt(); // if it's integer choice 

如果您的字符串是整數,那么您還可以解析它以獲取其整數值。 用於解析:

int value = Integer.parseInt(userChoice);

此外,為了比較字符串值,您應該使用“等於”函數而不是“==”。

if(userChoice.equals("Heads")){...} //rather than if(userChoice == "Heads"){...} 

它可能會幫助您獲得想法。

public static void main(String[] args) {
    Random rd = new Random();
    //Enter 1 0R 0
    int bob = rd.nextInt(2);
    String userChoice;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter a number");
    userChoice = sc.nextLine();
    System.out.println("You entered " + userChoice + " and bob is " + bob);
    int uc = Integer.parseInt(userChoice);
    if (uc == bob) {
        System.out.println("Hehe");
    } else {
        System.out.println("Sorry");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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