简体   繁体   中英

Storing input from a while loop outside the loop

so I have a question that wants a user to input an unknown amount of numbers then compute the total and average of these numbers. It wants us to use a while loop to ask for the input of the numbers based off of how many numbers the user said they had to input. How do I store the input to then use to compute the total and average?

This is what I have:

int numOfMarks, mark ;
    Scanner kdb = new Scanner(System.in);
    System.out.print("Enter number of marks: ");
    numOfMarks = kbd.nextInt() ;
    int i = 1 ;

    while(i<=numOfMarks) {
        System.out.print("Enter a mark: ") ;
        mark = kbd.nextInt() ;
        i=i+1 ;
    }

But as is, mark will get overwritten every time the loop is performed. How do I store each loops input for further use?

No need to store them all, have a running total and average at the end...

double total=0.0;

while(i<=numOfMarks) {
        System.out.print("Enter a mark: ") ;
        mark = kbd.nextInt() ;
        i=i+1 ;

        //new code
        total+= mark;
    }

System.out.println("Total= "  + total);
System.out.println("Avergae= "  + total/numOfMarks);

You can create a running total, and use average = total / numOfMarks

int total = 0;
while(i<=numOfMarks) {
    System.out.print("Enter a mark: ") ;
    mark = kbd.nextInt() ;
    total += mark;
    i=i+1 ;
}    

int avg = total / numOfMarks;

FYI, if you needed to do more complex analysis (such as finding the median), you could use a data structure such as an ArrayList<int> to store the data as it comes in

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

ArrayList<int> marks = new ArrayList<int>();
while(i<=numOfMarks) {
    System.out.print("Enter a mark: ") ;
    mark = kbd.nextInt() ;
    marks.add(mark);
    i=i+1 ;
}    

You could save it in an ArrayList.

int numOfMarks, mark ;
Scanner kdb = new Scanner(System.in);
List<Integer> myValues = new ArrayList<Integer>();
System.out.print("Enter number of marks: ");
numOfMarks = kbd.nextInt() ;
int i = 1 ;

while(i<=numOfMarks) {
    System.out.print("Enter a mark: ") ;
    myValues.add(kbd.nextInt());
    i=i+1 ;
}

//now you have a list of all the values and you can do what you want.

For just total and average, you need to keep two variables around - one for the total, and one for the number of numbers. But you already have that - numOfMarks .

So define:

double total = 0;

Inside the loop, add each mark to the total.

total += mark;

Then at the end, you can print:

System.out.println( "Total: " + total );
System.out.println( "Average: " + ( total / numOfMarks ) );

The reason I'm using a double is that the average would probably not be a whole number. You could use long (or even int for most cases) if you want the average to be round.

You could store them in an array, but considering all you need is the total and average, you can just keep adding to the total each loop. At the end, you'll have the total and can just compute the average from that. Like this:

int numOfMarks, totalMarks;
Scanner kdb = new Scanner(System.in);
System.out.print("Enter number of marks: ");
numOfMarks = kbd.nextInt();
int i = 1;

while(i<=numOfMarks) {
    System.out.print("Enter a mark: ");
    totalMarks = totalMarks + kbd.nextInt(); // add it to the total
    i=i+1;
}

// when the loop is done, compute the average. The total is already computed.
double average = totalMarks / numOfMarks;

// print it out maybe
System.out.println("Total: " + totalMarks);
System.out.println("Average: " + average);

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