如何在java中提示用户输入
所以我刚开始学习Java,就像我的第一天一样,我想尝试制作一个coinflip游戏。我已经知道了相当多的Javascript,所以我试图将这些知识应用到java中。所以到目前为止,一切都在运行,除了一件事:提示用户进行选择。所以在线阅读,我必须导入一个扫描仪,所以我这样做了,你可以从我的代码中看到。我还尝试了一些代码,您可以让用户导入一个字符串,但稍后在我的程序中可以看到,我将变量userChoice更改为一个数字。所以基本上我只是需要帮助。如果有某种方法可以使变量类型同时存储数字或字符串,那将是最好的。但我完全愿意用其他方式来做这件事!提前感谢!代码如下:如何在java中提示用户输入,java,variables,import,int,java.util.scanner,Java,Variables,Import,Int,Java.util.scanner,所以我刚开始学习Java,就像我的第一天一样,我想尝试制作一个coinflip游戏。我已经知道了相当多的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类,因此现在可以创建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截断为小于或等于它的最大整数。使用扫描仪,如您所说:
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
}
}
Java中的“String”数据类型可以包含数字和字符串(如您所要求的)。您可以使用扫描仪实用程序获取用户输入,如下所示:
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”函数而不是“==”函数
这可能有助于你获得想法
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");
}
}
哦,是的,顺便说一句,你可以看到我经常提到userChoice,这是我想要的存储用户导入的任何变量名!“导入”是指输入的文本吗?这通常被称为“输入”,“导入”是一个非常不同的东西?哦,好吧,对不起,这就是我的意思,我还不是一个很好的程序员。有时候我的术语有点不对劲!另一部分是将其转换为int
(至少从目前的问题中我可以看出这一点;它有点难读)@Pokechu22字符串可以很容易地转换为整数yyea,但OP似乎不知道如何转换,所以最好包含它。谢谢!将字符串转换为整数的好方法!哇,太谢谢你了!我真的很欣赏这个解释是多么详细,以及答案的彻底性!非常感谢你花时间写这篇文章!帮了大忙!好的,谢谢!也谢谢你关于数学随机性的小提示。对不起,我已经习惯了javascript,所以你!没问题,阿什温。祝您在学习java的旅途中好运。刚开始有点棘手/笨拙,但会变得更容易,哈哈。
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");
}
}