Java-整数输入的累积和在while循环中不起作用

Java-整数输入的累积和在while循环中不起作用,java,cumulative-sum,Java,Cumulative Sum,我很难弄明白为什么我的代码不起作用。首先,我是Java新手,所以请容忍我。 任务是: 编写一个程序,读取整数输入序列并打印累计总数。如果输入为1 7 2 9,程序应打印1 8 10 19 package lektion05forb; import java.util.Scanner; /** * * @author Lars */ public class P52LoopsC { /** * @param args the command line arguments

我很难弄明白为什么我的代码不起作用。首先,我是Java新手,所以请容忍我。 任务是: 编写一个程序,读取整数输入序列并打印累计总数。如果输入为1 7 2 9,程序应打印1 8 10 19

package lektion05forb;

    import java.util.Scanner;

/**
 *
 * @author Lars
 */
public class P52LoopsC 
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{

    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    double cumulative_sum = 0;
    String output_cumulative_sum= "";

    while (input.hasNextDouble())
    {
        double input_number = input.nextDouble();
        cumulative_sum += input_number;
        output_cumulative_sum += String.format("%s ", String.valueOf(cumulative_sum));
        break;
    }
    input.close();
    System.out.println(output_cumulative_sum);
}

}

当我输入一个像3 4 5 8 2这样的数字序列时,它返回34582,而不是数字的累积和。有人能解释为什么,以及我如何修复它,使其返回序列的累计总数吗?

从while循环中删除
中断

从while循环中删除
中断

您需要使用
执行while
,因为这样,循环将在您编写的代码中至少执行一次,循环从不执行,因为
input.hasNextDouble()
为空,因为初始化之后还没有来自用户的输入。(实际上,在扫描仪的情况下确实会执行循环,但通常认为在没有隐式定义和初始化控制变量的情况下执行while循环是一种糟糕的设计。)

代码是这样的

do {
        double input_number = input.nextDouble();
        cumulative_sum += input_number;
        output_cumulative_sum += String.format("%s ", String.valueOf(cumulative_sum));

    }  while (input.hasNextDouble());

此外,切勿将
中断在while语句中,这样即使参数在第一次运行后为true,它也只会循环一次,因为您打破了循环。

您需要使用
do while
,因为这样,循环将至少执行一次,在您编写的代码中,循环永远不会执行,因为
input.hasNextDouble()
为空,因为初始化后还没有来自用户的输入。(实际上,在扫描仪的情况下确实会执行循环,但通常认为在没有隐式定义和初始化控制变量的情况下执行while循环是一种糟糕的设计。)

代码是这样的

do {
        double input_number = input.nextDouble();
        cumulative_sum += input_number;
        output_cumulative_sum += String.format("%s ", String.valueOf(cumulative_sum));

    }  while (input.hasNextDouble());

此外,切勿将
中断在while语句中,这样即使参数在第一次运行后为true,它也只会循环一次,因为您打破了循环。

下面是一个
double

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    double cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Double.parseDouble(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}
输入:

数字:1729

输出:

1.0

8.0

10.0

19.0

下面是使用
Integer

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    Integer cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Integer.parseInt(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}
输入:

数字:1729

输出:

一,

八,

十,

十九,


下面是答案,答案是一个
double

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    double cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Double.parseDouble(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}
输入:

数字:1729

输出:

1.0

8.0

10.0

19.0

下面是使用
Integer

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    Integer cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Integer.parseInt(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}
输入:

数字:1729

输出:

一,

八,

十,

十九,


什么不起作用?你期望从代码中输出什么?你试过调试你的代码吗?'@thegauravmahawar-它应该输出一系列输入的累计和,但如果我输入3 4 7 2 1,它只输出34721。首先,删除循环中的
中断
。您也不需要在while循环的每次迭代中格式化总和。什么不起作用?你期望从代码中输出什么?你试过调试你的代码吗?'@thegauravmahawar-它应该输出一系列输入的累计和,但如果我输入3 4 7 2 1,它只输出34721。首先,删除循环中的
中断
。您也不需要在while循环的每次迭代中格式化总和。