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

在没有大括号的 while 中,只有紧跟在 while 后面的语句才会被执行。


在您的情况下,这意味着只有 while 之后的第一个语句将被循环,即“System.out.println(myintarray[index]);”。


由于“index++;” 未达到时,您的索引将具有相同的值 0,因此您的程序将始终打印数组的第一个元素。您的代码范围将在大括号中转换为:


while(index < 3) {

    System.out.println(myintarray[index]);

}

index++;

要执行多个语句,请将其全部括在大括号内。


while(index < 3) {

    System.out.println(myintarray[index]);

    index++;

}


慕的地6264312
浏览 108回答 2
2回答

aluckdog

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

蓝山帝景

scan.next();代替使用。public static void main(String[] args) {&nbsp; &nbsp; int firstnumber;&nbsp; &nbsp; int secondnumber;&nbsp; &nbsp; String operator;&nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; while (true) { //While Loop (main)&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter First Number");&nbsp; &nbsp; &nbsp; &nbsp; firstnumber = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Second Number");&nbsp; &nbsp; &nbsp; &nbsp; secondnumber = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Select Operator.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For Multiplaction: Type X");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For Division: Type %");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For Addition: Type +");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For Subtraction: Type -");&nbsp; &nbsp; &nbsp; &nbsp; operator = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(firstnumber);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(secondnumber);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(operator);&nbsp; &nbsp; } //While Loop (main)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java