2014-11-07 219 views
2

所以我刚开始学习Java,它的字面意思就像我的第一天,我想尝试做一个coinflip游戏。我已经知道了大量的Javascript,所以我试图将这些知识应用到Java。所以,除了一件事情之外,所有事情都一直在努力:提示用户进行选择。所以在线阅读,我必须导入扫描仪,所以我做到了,因为你可以从我的代码中看到。我也尝试了一些代码,你可以让用户导入一个字符串,但你可以稍后在我的程序中看到我将变量userChoice更改为一个数字。所以基本上我只需要帮助。如果有某种方法可以存储一个可以存储数字或字符串的变量类型,那么这种方法最好。但即时通讯开放给其他方式做到这一点!先谢谢了!这里是代码:你如何提示用户输入java

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!") 

      } 


      } 

    } 
+0

哦是的顺便说一句,你可以看到,我经常提到userChoice,这就是我想要的无论什么商店用户导入的变量名称! – 2014-11-07 02:44:07

+0

通过导入,你的意思是输入文字?这通常被称为“输入”,“导入”是一个非常不同的东西? – Pokechu22 2014-11-07 02:44:39

+0

哦,好吧,对不起,这就是我的意思,我不是一个非常好的程序员,但XP。有时我的终结者有点偏离! – 2014-11-07 03:28:32

回答

1

使用扫描仪,正如你所说的:

Scanner in = new Scanner(System.in); 

然后,提示用户输入的东西:

String userChoice = in.nextLine(); 

此外,当你比较字符串:

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

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

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

非常感谢你!我真的很感谢这个解释的详细程度和答案的彻底性!非常感谢花时间写这篇文章!很高兴! – 2014-11-07 03:17:50

0

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

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

nextLine()可用于从用户输入字符或字符串。

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

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

另一部分是将它转换为一个'int'(至少从目前的问题我可以看出来;这是很难看的) – Pokechu22 2014-11-07 02:54:19

+0

@ Pokechu22该字符串可以很容易地转换为整数 – 2014-11-07 03:07:16

+0

是的,但它似乎像OP不知道如何,所以最好包括这一点。 – Pokechu22 2014-11-07 03:08:12

0

已经导入java.util.Scanner中,从用户作为字符串得到输入,创建参数化System.in扫描器对象,并指定由所述扫描仪对象调用userChoice 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重新截断为小于或等于它的最大整数。

+0

很好,谢谢!还要感谢关于Math.random的一点小技巧。对不起,这个即时通讯用于JavaScript所以你! – 2014-11-07 03:18:34

+0

没问题,阿什温。祝你在学习java的旅程中好运。起初有点棘手/笨拙,但变得更容易Lol。 – 2014-11-07 03:23:09

0

在Java中“字符串”数据类型可以容纳数字和字符串(正如你所问)。您可以通过如下扫描工具获取用户输入:

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"){...} 
0

它可能会帮助您获得创意。

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