繁体   English   中英

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

[英]Can't figure out why the program will not run properly

我一直在尝试使该程序正常运行,该程序应该继续接受用户的输入,直到输入-1并计算总和。 问题如下:设计并实现一个Java程序(将其命名为InputSum),该程序提示用户输入一个正整数。 程序应接受整数,直到用户输入值-1(负1)。 用户输入-1后,程序将显示输入的数字及其总和,如下所示。 请注意,-1不是输出的一部分。 在继续处理每个输入的数字之前,请确保程序已对其进行验证,因为用户可能会输入除负数-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**************************//
   }
}

问题是,程序一直挂在应该打印总和的行上。 任何和所有帮助表示赞赏!

代替使用数组(因为它限制了您的输入数量),您可以使用一个临时变量来计算总和的值。 如下图所示:

int sum=0;
int num=0;
while(num != -1) {
    sum = sum + num;
    num = scan.nextInt(); //note that the variables can be reused
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM