我有這個(gè)作業(yè)。我很難將第一部分與第二部分聯(lián)系起來。這就是我所擁有的。我知道我在某些時(shí)候遺漏了一些東西來表明它應(yīng)該從輸入的數(shù)字中添加接下來的 100 個(gè)數(shù)字。package test;import java.util.Scanner;public class Ex216 { public static void main(String[] args) { // Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen Scanner myInput = new Scanner(System.in); int =a int sum; System.out.print("Enter first integer: "); a = myInput.nextInt(); for (int n = a; n <= 100; n++) System.out.printf("Sum = %d\n", sum); }}這就是給我?guī)砺闊┑脑颉?
1 回答

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
首先,int =a不是一個(gè)有效的表達(dá)式。它應(yīng)該是int a;因?yàn)槟阆霃?00給定值添加下一個(gè)數(shù)字,你需要將這些值添加到總和中,例如sum = sum+number.
這是一個(gè)代碼片段:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);
添加回答
舉報(bào)
0/150
提交
取消