在Java isn'中使用if和扫描仪;比较字符串时无法正常工作
如您所见,我正在使用在Java isn'中使用if和扫描仪;比较字符串时无法正常工作,java,Java,如您所见,我正在使用Keyboard.readString()当我询问被提名人时。当我使用s.readLine()时,它不工作,它跳转到else子句,然后崩溃,因为之后它接受了float值的总统输入。很抱歉,这有点政治色彩,但我没有费心去摆弄它。当我把“伯尼·桑德斯”当作“伯尼·桑德斯”的时候,我没有发现错误,有人知道原因吗?我猜这是因为s.nextFloat()和s.nextInt()仅分别读取float和int,而不是整行,因此扫描仪的光标位于float或int之后,而不在下一行。这里有一个
Keyboard.readString()代码>当我询问被提名人时。当我使用s.readLine()时
,它不工作,它跳转到else
子句,然后崩溃,因为之后它接受了float
值的总统输入。很抱歉,这有点政治色彩,但我没有费心去摆弄它。当我把“伯尼·桑德斯”
当作“伯尼·桑德斯”的时候,我没有发现错误,有人知道原因吗?我猜这是因为s.nextFloat()代码>和s.nextInt()
仅分别读取float
和int
,而不是整行,因此扫描仪的光标位于float或int之后,而不在下一行。这里有一个例子:
如果您的输入是:
import java.util.*;
class Sept1Little {
public static void main (String args []) {
Scanner s = new Scanner(System.in);
System.out.println("Hey! I'm not gonna program today o.o but I'm gonna do a little program :3");
System.out.println("Let's play with some math!");
System.out.println("Enter two numbers and magic will happen");
System.out.print("Enter the first number: ");
float a = s.nextInt();
System.out.print("Enter the second number: ");
float b = s.nextFloat();
float c = (float) Math.pow( a, b);
System.out.printf("The number is :%f\n" , c);
System.out.println("We love Bernie Sanders!");
System.out.println("Come on, let's push him over the line!");
int poll1 = 30;
System.out.println("Current poll amount: " + poll1);
System.out.println("Let's add 7% and an extra 1%!");
int poll2 = poll1 + 7;
int poll3 = ++poll2;
System.out.println("Bernie Sanders would get :" + poll3 + ". Bernie would get a better percentage than Hillo Clinto-Money");
System.out.println("Write in the best US President Nominee: ");
String president = Keyboard.readString();
if (president.equals("Bernie Sanders")){
System.out.println("You chose Bernie Sanders.");
}
else if (president.equals("Donald Trump")) {
System.out.println("You chose Donald Trump");
}
else if (president.equals("Hillary Clinton")) {
System.out.println("You chose Hillary Clinton ");
}
else {
System.out.println("Choose someone we know...");
}
System.out.println("Let's calculate how much money you owe the govt by... SQUARE ROOTING IT!");
System.out.print("Enter the amount of money you owe the govt: ");
float debt1 = Keyboard.readFloat();
System.out.printf("The amount is now :%f\n" , Math.sqrt(debt1));
System.out.println("Welcome to the Euro to Dollar Convertor!");
System.out.print("Enter the amount in US Dollars you wish to convert to Euro: ");
float euro1 = Keyboard.readFloat();
final float rate = (float) 1.13;
System.out.printf("The amount in Euro is %f\n" , euro1 / rate);
}
}
您的代码中包含:
5 Bench
Prove
为什么它没有捕获Prove
值?因为在使用s.nextInt()
后,扫描仪的光标
在5
之后,而不是在下一行(我们有Prove
的那一行)。为了避免它,您必须阅读该行的其余部分(因为您不想要它,所以对您没有用处),但不获取该值。像这样:
a= s.nextInt(); //5
b= s.nextLine(); //" Bench"
p.S.:您必须使用S.nextLine()每次使用s.nextInt()代码>或s.nextFloat()代码>如我上面的示例所示
我希望它会对你有帮助 我猜这是因为s.nextFloat()代码>和s.nextInt()
仅分别读取float
和int
,而不是整行,因此扫描仪的光标位于float或int之后,而不在下一行。这里有一个例子:
如果您的输入是:
import java.util.*;
class Sept1Little {
public static void main (String args []) {
Scanner s = new Scanner(System.in);
System.out.println("Hey! I'm not gonna program today o.o but I'm gonna do a little program :3");
System.out.println("Let's play with some math!");
System.out.println("Enter two numbers and magic will happen");
System.out.print("Enter the first number: ");
float a = s.nextInt();
System.out.print("Enter the second number: ");
float b = s.nextFloat();
float c = (float) Math.pow( a, b);
System.out.printf("The number is :%f\n" , c);
System.out.println("We love Bernie Sanders!");
System.out.println("Come on, let's push him over the line!");
int poll1 = 30;
System.out.println("Current poll amount: " + poll1);
System.out.println("Let's add 7% and an extra 1%!");
int poll2 = poll1 + 7;
int poll3 = ++poll2;
System.out.println("Bernie Sanders would get :" + poll3 + ". Bernie would get a better percentage than Hillo Clinto-Money");
System.out.println("Write in the best US President Nominee: ");
String president = Keyboard.readString();
if (president.equals("Bernie Sanders")){
System.out.println("You chose Bernie Sanders.");
}
else if (president.equals("Donald Trump")) {
System.out.println("You chose Donald Trump");
}
else if (president.equals("Hillary Clinton")) {
System.out.println("You chose Hillary Clinton ");
}
else {
System.out.println("Choose someone we know...");
}
System.out.println("Let's calculate how much money you owe the govt by... SQUARE ROOTING IT!");
System.out.print("Enter the amount of money you owe the govt: ");
float debt1 = Keyboard.readFloat();
System.out.printf("The amount is now :%f\n" , Math.sqrt(debt1));
System.out.println("Welcome to the Euro to Dollar Convertor!");
System.out.print("Enter the amount in US Dollars you wish to convert to Euro: ");
float euro1 = Keyboard.readFloat();
final float rate = (float) 1.13;
System.out.printf("The amount in Euro is %f\n" , euro1 / rate);
}
}
您的代码中包含:
5 Bench
Prove
为什么它没有捕获Prove
值?因为在使用s.nextInt()
后,扫描仪的光标
在5
之后,而不是在下一行(我们有Prove
的那一行)。为了避免它,您必须阅读该行的其余部分(因为您不想要它,所以对您没有用处),但不获取该值。像这样:
a= s.nextInt(); //5
b= s.nextLine(); //" Bench"
p.S.:您必须使用S.nextLine()每次使用s.nextInt()代码>或s.nextFloat()代码>如我上面的示例所示
我希望它会对你有帮助 Keyboard
是您创建的类吗?如果您使用扫描器进行用户输入,请使用相同的扫描器进行所有用户输入。Keyboard是一个classs I copied我只使用扫描器做了相同的事情@hovercraftfullofels所说的,然后放入.usedimiter(“\n”)代码>在…new Scanner…
行的末尾。键盘
是您创建的类吗?如果您使用扫描仪进行用户输入,请使用相同的扫描仪进行所有用户输入。键盘是一个classs I Copied。我只使用Scanner做了@HovercraftFullOfEels所说的事情,并放置。使用分隔符(“\n”)代码>在…新扫描仪…
行的末尾。我已尝试放置s.nextLine();在if语句、else语句和两者之后。还是一样的问题。@AuroraRossi你不必把它们放在if
语句之后。就在nextInt()
和nextFloat()
方法之后。@AuroraRossi:那么你做错了。通过编辑原始问题并在底部添加更改的代码向我们展示。@AuroraRossi:您告诉我们您正在进行一些更改,但这些更改不起作用。我们完全不知道你做了什么改变,也不知道它是如何起作用的。我要求你们简单地向我们展示一下。错误——我的评论的开头有一个@Aurora的名字,所以应该清楚地知道我是针对谁的。很好的回答,顺便说一句,1+。我试过把s.nextLine()放在上面;在if语句、else语句和两者之后。还是一样的问题。@AuroraRossi你不必把它们放在if
语句之后。就在nextInt()
和nextFloat()
方法之后。@AuroraRossi:那么你做错了。通过编辑原始问题并在底部添加更改的代码向我们展示。@AuroraRossi:您告诉我们您正在进行一些更改,但这些更改不起作用。我们完全不知道你做了什么改变,也不知道它是如何起作用的。我要求你们简单地向我们展示一下。错误——我的评论的开头有一个@Aurora的名字,所以应该清楚地知道我是针对谁的。回答不错,顺便说一句,1+。