gpt4 book ai didi

无论我正确输入,Java 都没有运行我的扫描仪

转载 作者:行者123 更新时间:2023-12-02 00:41:13 25 4
gpt4 key购买 nike

所以我试图在 VSCode 中制作一个计算器,但由于某种原因,我的扫描仪没有运行。这是迄今为止我的项目的完整代码...

import java.util.Scanner;
class main { //Main
public static void main(String[] args) {
int firstnumber = 0;
int secondnumber = 0;
String Operator = "-";

boolean erase = true;

Scanner scan = new Scanner(System.in);

while (erase = 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.nextLine()
} //While Loop (main)
}
} //Main

出于某种原因,当我运行该代码时,它会运行前两个扫描仪,但随后无法运行要求运算符(operator)的扫描仪。如果有人知道为什么我会很高兴知道。

最佳答案

我确实将运算符更改为 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 上进行了尝试

关于无论我正确输入,Java 都没有运行我的扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com