【问题标题】:How do you prompt a user for an input in javajava中如何提示用户输入
【发布时间】:2015-01-03 18:26:40
【问题描述】:

所以我刚开始学习 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!")

          }


          }

    }

【问题讨论】:

  • 哦,是的,顺便说一句,你可以看到我经常引用 userChoice,这就是我想要用户导入的任何存储的变量名!
  • 导入是指输入的文本吗?那通常称为“输入”,“导入”是完全不同的东西?
  • 哦,好吧,对不起,这就是我的意思,我还不是一个很好的程序员 xD。有时我的术语有点不对劲!

标签: java variables import int java.util.scanner


【解决方案1】:

如你所说,使用扫描仪:

Scanner in = new Scanner(System.in);

然后,提示用户输入内容:

String userChoice = in.nextLine();

此外,当您比较字符串时:

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

这对非原始对象是不好的。最好只使用== 来比较ints 或enums 的值。如果你像这样比较一个字符串,它将不起作用,因为它正在检查对象是否相同。相反,像这样比较:

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
        }
    }

【讨论】:

  • 哇,非常感谢!我非常感谢这个解释的详细程度和答案的彻底性!非常感谢您花时间写这篇文章!帮了很多忙!
【解决方案2】:

您已经导入了 Scanner 类,因此您现在可以创建一个 Scanner 类型的变量来获取输入。

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

nextLine() 可用于输入来自用户的字符或字符串。

要将字符串转换为整数,可以通过以下方式将整数值分配给字符串。

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

【讨论】:

  • 另一部分是将其转换为int(至少到目前为止我可以从问题中看出这一点;这有点难以阅读)
  • @Pokechu22 字符串可以轻松转换为整数
  • 是的,但似乎 OP 不知道如何做,所以最好包括在内。
  • 谢谢!将字符串转换为整数的好方法!
【解决方案3】:

Java 中的“String”数据类型可以同时包含数字和字符串(如您所问)。您可以使用 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);

此外,对于比较字符串值,您应该使用“equals”函数而不是“==”。

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

【讨论】:

    【解决方案4】:

    导入 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 截断为小于或等于它的最大整数。

    【讨论】:

    • 很酷,谢谢!还要感谢关于 Math.random 的小提示。对不起,我习惯了 javascript 所以你!
    • 没问题,阿什温。祝你学习java的旅程好运。一开始有点棘手/笨拙,但越来越容易大声笑。
    【解决方案5】:

    它可能会帮助您获得想法。

    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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多