2 回答
TA贡献1847条经验 获得超7个赞
我确实将运算符更改为 char,现在它可以工作了:-
import java.util.Scanner;
class main { //Main
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int firstnumber = 0;
int secondnumber = 0;
char operator;
boolean erase = true;
while (erase) { //While Loop (main)
System.out.println("Enter First Number");
firstnumber = scan.nextInt();
System.out.println("Enter Second Number");
secondnumber = scan.nextInt();
System.out.println("Select Operator.");
operator = scan.next().charAt(0);
System.out.println("For Multiplaction: Type X");
System.out.println("For Division: Type %");
System.out.println("For Addition: Type +");
System.out.println("For Subtraction: Type -");
} //While Loop (main)
}
} //Main
也许您的 nextLine() 不会触发,因为您已经有两个扫描仪提示,因此它的行为是被接受的输入。也许你应该对此进行更多调试。尽管如此,仍然scan.next();有效...尝试使用 Oracle JDK 1.8.0_u221 在 Visual Studio Code 1.38.1 上
TA贡献1843条经验 获得超7个赞
scan.next();代替使用。
public static void main(String[] args) {
int firstnumber;
int secondnumber;
String operator;
Scanner scan = new Scanner(System.in);
while (true) { //While Loop (main)
System.out.println("Enter First Number");
firstnumber = scan.nextInt();
System.out.println("Enter Second Number");
secondnumber = scan.nextInt();
System.out.println("Select Operator.");
System.out.println("For Multiplaction: Type X");
System.out.println("For Division: Type %");
System.out.println("For Addition: Type +");
System.out.println("For Subtraction: Type -");
operator = scan.next();
System.out.println(firstnumber);
System.out.println(secondnumber);
System.out.println(operator);
} //While Loop (main)
}
添加回答
举报