Java 什么';这个非常简单的代码有什么错
当我运行代码时,它会一直工作,直到它询问“您希望从(sum、subst、multi、div)中使用哪个操作”为止。无论用户选择什么,我的程序都没有响应 为什么会这样Java 什么';这个非常简单的代码有什么错,java,if-statement,calculator,iostream,Java,If Statement,Calculator,Iostream,当我运行代码时,它会一直工作,直到它询问“您希望从(sum、subst、multi、div)中使用哪个操作”为止。无论用户选择什么,我的程序都没有响应 为什么会这样 import java.util.Scanner; import java.io.*; public class three3 { public static void main (String[] args) { int x; int y; int opera;
import java.util.Scanner;
import java.io.*;
public class three3 {
public static void main (String[] args) {
int x;
int y;
int opera;
String oper;
Scanner in = new Scanner (System.in);
System.out.println(" write the first number ");
x = in.nextInt();
System.out.println(" write the second number ");
y = in.nextInt();
System.out.println(" which operation do you want to use from ( sum , subst , multi , div )");
oper = in.nextLine();
if (oper == "sum") {
opera=x+y;
System.out.println(" the sum of two numbers is " + opera );
}
if (oper == "subst") {
opera = x - y;
System.out.println(" the subtraction of two numbers is " + opera );
}
if (oper == "multi") {
opera = x * y;
System.out.println(" the multi of two numbers is " + opera );
}
if (oper == "div") {
opera = x / y;
System.out.println(" the division of two numbers is " + opera );
}
}
}
因为这些if子句都没有执行。
您正在将
字符串
与=
进行比较,这是错误的。改用运算等于(“总和”)
。请参阅以供参考。结论是,对于字符串
始终使用等于
,因为没有执行任何if子句。
您正在将
字符串
与=
进行比较,这是错误的。改用运算等于(“总和”)
。请参阅以供参考。你的结论是,总是使用<代码>等于<代码> <代码>字符串 ,添加到其他人的点,你也应该考虑使用<代码>其他,如果{} 和<代码>其他{} /COD>语句,这样你就可以捕获无效的输入。 添加到其他人的点,你也应该考虑使用<代码>否则{}。和
else{}
语句,以便捕获无效的输入。在上次调用.nextLine()中的之后,需要调用.nextLine()中的
,原因是仅请求下一个整数不会占用输入中的整行,因此,您需要通过调用.nextLine()中的in
,提前跳到输入中的下一个新行字符
在调用不占用整行的方法后,每次需要获取新行时,例如调用nextBoolean()
等时,都必须执行此操作
此外,您不需要使用==
运算符检查字符串是否相等,而是使用.equals()
字符串方法。您需要在最后一次调用In.nextLine()
In.nextInt()
后立即调用In.nextLine()
,原因是仅请求下一个整数不会占用输入的整行,因此,您需要通过调用.nextLine()中的in
,提前跳到输入中的下一个新行字符
在调用不占用整行的方法后,每次需要获取新行时,例如调用nextBoolean()
等时,都必须执行此操作
此外,如果不使用
==
运算符检查字符串是否相等,请改用.equals()
字符串方法。问题是,In.nextLine()
会使用在输入int后单击enter时隐式插入的\n。这意味着程序不需要用户提供任何其他输入。要解决此问题,可以先在.nextLine()中使用一个新行,
,然后再将其放入实际变量中,如下所示:
System.out.println(" write the second number ");
y=in.nextInt();
System.out.println(" which operation do you want to use from ( sum , subst , multi , div )");
in.nextLine(); //New line consuming the \n
oper=in.nextLine();
if(oper.equals("sum")){//replace == by .equals
opera=x+y;
}
除此之外,正如runDOSrun所说,您应该将字符串的比较从
a==b
替换为a.equals(b)
问题是,in.nextLine()
在输入int后单击enter时使用\n隐式插入的字符串。这意味着程序不需要用户提供任何其他输入。要解决此问题,可以先在.nextLine()中使用一个新行,
,然后再将其放入实际变量中,如下所示:
System.out.println(" write the second number ");
y=in.nextInt();
System.out.println(" which operation do you want to use from ( sum , subst , multi , div )");
in.nextLine(); //New line consuming the \n
oper=in.nextLine();
if(oper.equals("sum")){//replace == by .equals
opera=x+y;
}
除此之外,正如runDOSrun所说,您应该将字符串的比较从
a==b
替换为a.equals(b)
这是正确答案这是正确答案这不是正确答案answer@gurghet我没有注意到胡安回答中的错误,但我要说的是,这是导致意外行为的一个原因。没有错误的回答。没有回应谁说有回应?这不是正确的answer@gurghet我没有注意到胡安回答中的错误,但我要说的是,这是导致意外行为的一个原因。没有错误的回答。没有回应谁说有回应?