数组测试程序 - 初学者

时间:2016-04-21 18:44:00

标签: java arrays

我很长时间潜伏和第一次溢出的用户,希望有人能够帮助指导我朝正确的方向发展。该计划显然存在的问题比我承认的要多,但是,我已经到了那里!我现在主要担心的是......

  1. 我的第一个问题是关于将程序循环回到开头。例如,如果用户输入整数< 1程序输出ERROR MESSAGE。程序在那里停止。如何将其循环回来以提示用户输入另一个整数。

  2. 当程序提示用户输入()整数列表时,我也遇到了问题:"出于某种原因,我必须输入比我应该更多的整数。最后,程序只采用适当的数字,这很奇怪。 (参见程序测试 - 通知1到5然后是整数6以下。我在5之后点击输入但是程序没有继续。只有当我按下回车键时,它才会继续,键入6然后再次按回车键。下面的数组但这很准确。)

  3. 请告诉我是否应该更改其他内容。

  4. 希望我能够在发现这个问题时发布我的更新代码!如果您已经阅读过这一点,请提前致谢!      / **      * N Irwin - 编程I - 实验23      * Array Tester - 2016年4月21日      * /

    import java.util.Scanner;
    
    public class ArrayTest
    {
    
       public static void main(String[] args)
       { 
    
         /**
          * Beginning of Program and OUTPUT.
          */
         System.out.println("       * * * Array Test * * *");
         System.out.println("      WRITTEN by N Irwin");
         System.out.println();
    
         /**
          * Scanner object.
          */
         Scanner input = new Scanner(System.in);
    
         /**
          * Program OUTPUT - user is required to provide input.
          */
         System.out.print("Enter the number of integers to read: ");
         int integerNumber = input.nextInt();
         System.out.println();
    
         /**
          * Program begins if integer is greater than or equal to 1.
          */
         if(integerNumber >= 1)
         {
    
         /**
          * Program OUTPUT - user is required to provide input.
          */
         final int LENGTH = 100;
         int[] values = new int [LENGTH];
         int currentSize = 0;
    
         System.out.print("Enter a list of " + integerNumber + " integers: ");
    
         /**
          * 
          */
         while (input.hasNextInt() && currentSize < integerNumber)
         {
             if (currentSize < values.length)
             {
                 values[currentSize] = input.nextInt();
                 currentSize++;
                }
            }
    
         /**
          * Program OUTPUT - integers in listed formatting.
          */
         System.out.println();
         System.out.println("Your integers are:");
         System.out.println();
    
         int listNumber = 1;
    
         for (int i = 0; i < currentSize; i++)
         {
             System.out.printf("%6d", listNumber);
             System.out.println(". " + values[i]);
             listNumber++;
            }
    
         /**
          * Program OUTPUT - integers in listed formatting in reverse.
          */
         System.out.println();
         System.out.println("Your integers in reverse are:");
         System.out.println();
    
         int listReverse = 1;
    
         for (int i = 0; i < currentSize; i++)
         {
             System.out.printf("%6d", listReverse);
             System.out.println(". " + values[i]);
             listReverse++;
            }
    
         /**
          * Program OUTPUT - total of integers.
          */
    
         double totalInteger = 0;
    
         for (int element : values)
         {
             totalInteger += element;
            }
    
         System.out.println();
         System.out.println("The total of these " + integerNumber + " numbers is: " + totalInteger);
         System.out.println();
    
         /**
          * Program OUTPUT - average of integers.
          */
    
         double averageInteger = 0;
    
         if (values.length > 0)
         {
             averageInteger = (totalInteger / integerNumber);
            }
    
         System.out.print("The average of these " + integerNumber + " numbers is: ");
         System.out.printf("%.1f", averageInteger);
         System.out.println();
         System.out.println();
    
         /**
          * Program OUTPUT - minimum integer.
          */
    
         int minimumInteger = values[0];
    
         for(int i = 1; i < values.length; i++)
         {
             if (values[i] < minimumInteger)
             {
                 minimumInteger = values[i];
                }
            }
    
         System.out.println("The minimum value in these " + integerNumber + " numbers is: " + minimumInteger);
         System.out.println();
    
         /**
          * Program OUTPUT - maximum integer.
          */
    
         int maximumInteger = values[0];
    
         for(int i = 1; i < values.length; i++)
         {
             if (values[i] > maximumInteger)
             {
                 maximumInteger = values[i];
                }
            }
    
         System.out.println("The maximum value in these " + integerNumber + " numbers is: " + maximumInteger);
         System.out.println();
    
         /**
          * Program OUTPUT - pairs that have been summed.
          */
         System.out.println("Summing pairs of numbers produces: ");
         System.out.println();
        }
    
        /**
          * Program produces an ERROR message if the integer is less than 1.
          */
        else
        {
            System.out.println("ERROR: The integer you entered was invalid.");
        }
    }
    }
    
    ***** PROGRAM TEST *****
    
    * * * Array Test * * *
    WRITTEN by Nathan Irwin
    
    Enter the number of integers to read: 5
    

    输入5个整数的列表:1 2 3 4 5 6

    你的整数是:

     1. 1
     2. 2
     3. 3
     4. 4
     5. 5
    

    反过来的整数是:

     1. 1
     2. 2
     3. 3
     4. 4
     5. 5
    

    这5个数字的总和是:15.0

    这5个数字的平均值是:3.0

    这5个数字的最小值为:0

    这5个数字的最大值是:5

    汇总数字对产生:

1 个答案:

答案 0 :(得分:0)

如果您希望用户强制输入number > 1,您可以这样做:

int integerNumber;

do{
  integerNumber = input.nextInt();

  if(integerNumber < 1)
     System.out.println("Error");
}while(integerNumber < 1);

因此输入将始终显示,直到用户输入number > 1

对于第二个问题,您可以阅读完整的数字行,然后split(除以)您读过的字符串作为参考,在每个数字之间放置一个空格。像这样:

String string = input.nextLine(); //Reads the full line
String[] parts = string.split(" "); //Here you divide the string directly in an array taking as reference the space between each number

for (int i = 0; i < parts.length; i++)
{
   System.out.println(parts[i]); //You will see each input that you have done
}

我希望它会对你有所帮助。不要怀疑你是否有任何问题。