gpt4 book ai didi

java - 无法弄清楚为什么程序无法正常运行

转载 作者:行者123 更新时间:2023-11-30 03:14:56 25 4
gpt4 key购买 nike

我一直在尝试让这个程序正常工作,它应该继续接受用户的输入,直到输入 -1 并计算总和。问题如下: 设计并实现一个 Java 程序(将其命名为输入和),提示用户输入一个正整数。该程序应接受整数,直到用户输入值 -1(负数)。用户输入-1后,程序应显示输入的数字及其总和如下所示。请注意,-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**************************//
}
}

问题是,程序总是卡在应该打印总和的行上。感谢任何和所有的帮助!

最佳答案

您可以使用列表来存储数字,并使用无限循环来不断接收用户的输入。此外,您应该在开始处理数字之前检查停止条件(因为您的问题提到 -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**************************//
}
}

关于java - 无法弄清楚为什么程序无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com