Java没有给出两个整数之间的和的输出
因此,我试图制作以下程序: 用户提供两个整数作为输入(第一个和最后一个)。程序应该给出这两个数字之间的和,但是当我运行程序时,我没有得到输出。然而,当我为第一个输入输入一个比最后一个输入大的整数时,我得到第一个输入作为结果。这是我的代码:Java没有给出两个整数之间的和的输出,java,intellij-idea,Java,Intellij Idea,因此,我试图制作以下程序: 用户提供两个整数作为输入(第一个和最后一个)。程序应该给出这两个数字之间的和,但是当我运行程序时,我没有得到输出。然而,当我为第一个输入输入一个比最后一个输入大的整数时,我得到第一个输入作为结果。这是我的代码: public class TheSumBetweenTwoNumbers { public static void main(String[] args) { Scanner reader = new Scanner(System.in
public class TheSumBetweenTwoNumbers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("First: ");
int first = Integer.parseInt(reader.nextLine());
System.out.println("Last: ");
int last = Integer.parseInt(reader.nextLine());
int sum = 0;
while (first <= last); {
sum += first;
first++;
}
System.out.println("The sum is: " + sum);
}
}
public类两个数字之间的子类{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“第一个:”);
int first=Integer.parseInt(reader.nextLine());
System.out.println(“Last:”);
int last=Integer.parseInt(reader.nextLine());
整数和=0;
while(首先您的while循环运行时不更改它包含的块。在while循环进入块之前,您已经关闭了它
while (first <= last) {
sum += first;
first++;
}
while(首先,while
条件后面不应该有分号;它充当while
循环的主体。由于“简单的印刷错误”,thx@RGETM关闭,您是否尝试调试以发现问题?谢谢!我发现我错放了一个“;”。