简体   繁体   中英

How can I read the amount of input a user has entered into an array with a set size?

The objective is to make an array of size 100, have a user enter a bunch of positive numbers, and quit whenever they enter -1 (or fill up 100 spots I guess). After that, print out the number of inputs read in, the average value, and highest value. I was testing my code with a smaller sized array originally to get the average and now realize that array.length isn't what I'm looking for as I switch it to 100 and don't fill up 100 slots. My question is, how can I count the amount of input thats been entered into an array and not just the total length of an array?

import java.util.*;
class Array{
    public static void main(String [] args){
       Scanner scan = new Scanner(System.in);
       int[] array = new int[100];

       System.out.println("Enter your number: ");
       for(int i = 0; i < 100; i++){
          int entry = scan.nextInt();
          if(entry == -1){
             System.out.println("Goodbye.");
             i--;
             break;
          }else if(entry < -1){
             System.out.println("Invalid.");
             i--;
          }else{
             array[i] = entry;
          }
       } 
       //prints amount of elements inside array
       //System.out.println("There are " + countArray(array) + " numbers in this array.");

       //prints average
       System.out.println("The average is: " + findAverage(array));   

       //prints highest val
       System.out.println("The highest value is: " + findHigh(array));
 
    }
    //public static int countArray(int[] array)
    //need help here and then fixing the issue in the average method

     public static double findAverage(int [] array){
         double sum = 0;
         for(int i = 0; i < array.length; i++){
            sum += array[i];
         }
         double average = (sum/array.length);
         return average;
      }

      public static int findHigh(int[]array){
         int max = array[0];
         for(int i = 1; i < array.length; i++){
            if (array[i] > max){
               max = array[i];
            }
         } 
         return max;
      }  
}

There is no method you can use for that because all indexes where you don't specify an element yourself get set to 0. What you can do instead is just have a variable which you increase by one each time you add an element to your array. In your case you can use i for this (declare it outside the for header so you can access it later on).

You can keep a variable to count the number of inputs.

        int inputCount = 0;
        for (int i = 0; i < 100; i++) {
            int entry = scan.nextInt();
            inputCount++;
            if (entry == -1) {
                System.out.println("Goodbye.");
                i--;
                break;
            } else if (entry < -1) {
                System.out.println("Invalid.");
                i--;
            } else {
                array[i] = entry;
            }
        }

Typically, to enter some number of values into a properly sized array, one first prompts for the size.

Scanner input = new Scanner(System.in);
System.out.print("Please enter number of values: ");
int size = input.nextInt();
int[] values = new int[size];
System.out.println("Start entering " + size + " values);
for (int i = 0; i < size; i++) {
    values[i] = input.nextInt();
}
// now do something with the array.

This allows negative values to be entered too as it does not require a value to signal end of input. Just an example, but this technique can be easily adapted to your requirements as you can still ignore negative numbers if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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