插入表达式以完成JAVA语句块时出现语法错误
这是我的代码到目前为止,使一个程序,接受用户输入的1和100之间的数字,只有偶数。它还需要在输入N或N时结束,计算有效数字的总和并显示最大数字
我有一个错误,说语法错误插入“while(expression)”;要完成此代码图片上的块语句…:
"制度。出来println(“输入整数列表,按N或N将计算您的数字”);'
任何帮助都将不胜感激
- 这是代码的开始:-
package week11;
import java.util.*;
public class repeatlist {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int number = 0, Sum = 0; //variables
char letter;
char choice;
//if (Number %2 ==0)
{
do
System.out.println("Enter the list of whole numbers, pressing N or n will Calculate your numbers");
number = sc.nextInt();
letter = sc.next().charAt(0);
{
while(letter != 'N' && letter != 'n' );
System.out.println("end of program");
Sum = Sum + number;
number = sc.nextInt();
System.out.println("Sum is "+ Sum);
System.out.print("Do you want to repeat the ");
System.out.println(" Program ['Y' or 'N']");
choice = sc.next().charAt(0);
}
}
}
}
# 1 楼答案
这是为什么缩进很重要的一个很好的例子。当前循环本质上执行以下操作:
大量不必要的括号和语句将导致代码无法按预期运行
也许你在找
或
# 2 楼答案
试试这个
尽量保持代码的良好格式,所有IDE都有一个选项可以为您设置格式。它会帮助你发现这些