gpt4 book ai didi

java - 语法错误插入 while 表达式来完成 JAVA block 语句

转载 作者:行者123 更新时间:2023-12-02 05:08:48 28 4
gpt4 key购买 nike

这是我迄今为止制作的程序的代码,该程序接受用户输入的 1 到 100 之间的数字,并且仅接受偶数。还需要在输入N或n时结束,计算有效数字之和并显示最大数字。

我有一个错误,提示语法错误插入“while(表达式)”;完成这段代码的 block 语句...:

'System.out.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);
}
}
}

}

最佳答案

试试这个

    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);

尽量保持代码格式正确,所有 IDE 都可以选择为您格式化代码。它将帮助您发现这些类型

关于java - 语法错误插入 while 表达式来完成 JAVA block 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27527976/

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