我一直试图让这个程序工作,它应该继续从用户那里获取输入,直到输入-1并计算总和。
问题如下:
设计并实现一个Java程序(命名它
InputSum
)提示用户
输入正整数。该计划应该是ac
cept整数,直到用户输入值-1
(否定的)。用户输入-1后,程序s
应显示输入的数字,然后显示其总和
如下所示。请注意,-1不是输出的一部分。
确保程序
在proc之前验证每个输入的数字
因为用户可能输入负数
句子值以外的数字-1
。
设计你的程序就好了
允许用户重新运行
程序在同一次运行中使用不同的输入集,如上所示。
记录您的代码,并组织和
如上所示,将输出空间化。
这是我的代码:
/* Class: CS1301
* Section: 9:30
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 6
* Program: 1
* ProgramName: InputSum
* Purpose: The program prompts the user to input numbers until -1 is entered and calculates the sum
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input is the numbers
* Output(s): The output will be the sum of the numbers
* Methodology: The program will use loops to determine if numbers or still to be entered or if -1 was entered
*
*/
import java.util.*;
import java.io.*;
public class InputSum
{
public static void main (String[] args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
Scanner scan = new Scanner(System.in); //Initializes scanner
int n = 1;
int [] num = new int[n]; //Creates array for input numbers
int i;
int sum=0;
/******************************************************************************
* Inputs Section *
******************************************************************************/
System.out.print("Please input integers, note that -1 ends the submissions: "); //Prompts the user for input
/****************************variables********************************/
//***************************Calculations in processing************************//
/******************************************************************************
* Processing Section *
******************************************************************************/
for(i=0; i<num.length; i++)
{
num[i] = scan.nextInt(); //Continues to read numbers and add them to the sum
n = n + 1; //Adds to the array maximum
sum = sum + num[i]; //Calculates the sum
if (num[i] == -1){
break;
}
}
System.out.print("The numbers entered are: " + num[i]);
System.out.print("\nThe sum of the numbers is: " + sum);
/******************************************************************************
* Outputs Section *
******************************************************************************/
//***************Output is in the processing**************************//
}
}
问题是,程序一直挂在应该打印总和的行上。任何和所有的帮助表示赞赏!
答案 0 :(得分:1)
您可以使用临时变量来计算总和的值,而不是使用数组(因为它限制了输入的数量)。如下图所示:
int sum=0;
int num=0;
while(num != -1) {
sum = sum + num;
num = scan.nextInt(); //note that the variables can be reused
}
答案 1 :(得分:1)
您可以使用List来存储数字和无限循环以保持接收来自用户的输入。此外,您应该在开始处理数字之前检查停止条件(因为您的问题提到-1不是输出的一部分)。这是一个例子
import java.util.*;
import java.io.*;
public class InputSum
{
public static void main (String[] args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
Scanner scan = new Scanner(System.in); //Initializes scanner
int number; //Declare a variable that will hold the temporal value that is read on the input stream
int sum=0;
// Use a List
List<Integer> numbers = new ArrayList<Integer>();
/******************************************************************************
* Inputs Section *
******************************************************************************/
System.out.print("Please input integers, note that -1 ends the submissions: "); //Prompts the user for input
/****************************variables********************************/
//***************************Calculations in processing************************//
/******************************************************************************
* Processing Section *
******************************************************************************/
// use an infinite loop
for(; ; )
{
// You should normally do this check when you enter the loop
// so that -1 which is a stop token should not be added to the list
// and not taken into account in the sum
number = scan.nextInt(); //Continues to read numbers and add them to the sum
if (number == -1){
break;
}
// You could write numbers.add(number) which would be
// Java's autoboxing feature, but this is what would really take place
numbers.add(Integer.valueOf(number));
sum += number; //Calculates the sum
}
System.out.print("The numbers entered are: " + numbers);
System.out.print("\nThe sum of the numbers is: " + sum);
/******************************************************************************
* Outputs Section *
******************************************************************************/
//***************Output is in the processing**************************//
}
}