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

标签 java

这是我迄今为止制作的程序的代码,该程序接受用户输入的 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/

相关文章:

java - Play Framework 调用 Controller 导致打开文件过多错误

java - 连接表上没有主复合键的 JPA @ManyToMany 关系

用于并发数据库/REST 调用的 Java 8 parallelStream

java - 什么是NullPointerException,我该如何解决?

java - 带有注入(inject)服务的 REST 端点 - 在哪里抛出自定义 WebServiceException?

java - 带搜索的严格自动完成 JComboBox

java - 处理Java : Cursor position of text box off

java - 在 Eclipse 中,是否可以找到项目中所有采用某种参数类型的方法?

java - 为什么在我的示例中需要 try-catch with throws?

java - JAX RS客户端: Catch exception message while making a REST call