Reputation: 1
I'm in a Java beginning course and am having problems extracting data from user input and using it to create a set of grades based on the amount of user input. I'm then supppose to organize that set of data from highest to lowest and average it out. Here is my code so far
//import Scanner
import java.util.Scanner;
public class Looper {
public static void main(String[] args) {
//introduce integers
int highest = 0;
int lowest = 0;
int count = 0;
int grade = (int) (Math.random() * 100);
int student;
int total = 0;
double average = 0;
//introduce Scanner
Scanner input = new Scanner(System.in);
//printout number of students
System.out.println("Enter number of students");
student = input.nextInt();
//narrow amount of students
if (student <= 10) {
System.out.println("Enter the grades of the students");
}
else if (student >= 11) {
System.out.println("Too many Students");
}
//close off 0
while (student != 0) {
System.out.println("Didn't enter students");
}
grade = input.nextInt();
//connect student and grade
for (grade = 0; grade <= 100; grade++) {
student = grade;
if (grade > 100) {
System.out.println("Must be between 0 and 100")
} if (grade <= highest) {
grade++;
}
}
for (grade = 0; grade >= 0; grade++) {
student = grade;
if (grade < -1) {
System.out.println(" Must be between 0 and 100");
}
else if (grade >= lowest) {
grade++;
}
// form total and average
total = total + grade;
grade++;
{
average = (double) total / grade;
//printout highest, lowest, and average
System.out.println("The highest is" + highest + "\n The lowest is" + lowest + "The average is\n" + average);
}
}
}
}
Upvotes: 0
Views: 5997
Reputation: 3630
First off, using ifs without brackets makes for poor code readability.
Secondly, there's ;
missing after System.out.println("Must be between 0 and 100")
and you have a ;
after your while loop: while (Student!= 0);{
Your variables all start with an upper case when they should start with lower case.
There's a double ;
after this line: int Grade= (int) (Math.random() *100);;
The comments in your code don't make any sense either....
//introduce scanner
//introduce integers
int highest = 0;
int lowest=0;
int count=0;
int Grade= (int) (Math.random() *100);;
int Student;
int Total=0;
double average= 0;
//print out number of students
Scanner input = new Scanner(System.in);
Scanner is introduced way lower than your comment, you don't print out the number of students but input the number of students...
I'm sorry but this code is a mess, I don't even know where to start.
EDIT: I did some refactoring on your original code, This should put you on the right track.
import java.util.Scanner;
public class Looper {
public static void main(String[] args) {
int highest = 100;
int lowest = 0;
int grade = 0;
int students = 0;
float total = 0;
double average = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of students: ");
students = input.nextInt();
if (students <= 10) {
System.out.println("Enter the grades of the students: ");
// ADDED CODE
for(int i = 0; i < students; i++) {
do {
grade = input.nextInt();
if(grade >= 0 && grade <= 100) {
System.out.println("Grade must be between 0 and 100");
}
} while(grade < 0 || grade > 100);
if(grade > highest) {
highest = grade;
}
if(grade < lowest) {
lowest = grade;
}
total += grade;
}
average = (total/students);
System.out.println("The highest is " + highest);
System.out.println("The lowest is " + lowest);
System.out.println("The average is " + average);
// END ADDED CODE
} else if (students >= 11) {
System.out.println("Too many Students");
}
}
}
Upvotes: 1