Question about Tutorial 21

0 Rick Reiter · April 14, 2015
This code seems to be endlessly looping for me. Why is this?
import java.util.Scanner;
/**
 *
 * @author Rick
 */
public class J21_SimpleAveragingProgram {
    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        int total = 0;
        int grade;
        int average;
        int counter = 0;
        
        while (counter < 10){
            grade = input.nextInt();
            total = total + grade;
            counter++;
        }
        average =  total/10;
        System.out.println("Your average is "+average);
}
}
looks the same as the tutorial as far as I can see.

Post a Reply

Replies

Oldest  Newest  Rating
0 Branislav Lazic · April 14, 2015
All works fine for me (after inserting 10 numbers). 
0 Pblolpb Minecraft · April 16, 2015
import java.util.Scanner;

public class J21_SimpleAveragingProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total = 0;
int grade;
int average;
int counter = 0;

while (counter < 10) {
//Prints out how many numbers have been added already, and then asks for the next number
System.out.println(counter + " numbers added. Please enter next number:");
grade = input.nextInt();
total = total + grade;
counter++;
}
average = total / 10;
System.out.println("Your average is " + average);
}
}

Does the above code work? I added the println in the while loop to show how many numbers are have been added, and to show when it wants a number.
  • 1

Java / Android Development

106,950 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator