So I have to make a java program that lets you enter test scores and then displays the test scores back to you showing the number of A's, B's' C's, 'D's F's entered based on the numbers put into the program. The program is supposed to end when you enter a negative number and the negative number is not used actually in the program so the program if you entered 70 80 90 and 100 -2 it would display something like this based on the input provided
Number of A's = 2
Number of B's = 1
Number of C's = 1
Number of D's = 0
Number of F's = 0
My code is as below I have redone this program so many times it is baffling me... I really would like any kind of help possible I hope I explained this correctly, >_<...

``````import java.util.Scanner;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input;

System.out.println("Input grades scores, -1 to exit");
String garbage;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
while (!sc.hasNextInt()) {
System.out.println("Input numbers only 0 ... 100, -1 to exit you can enter up to 6 numbers");
int number = sc.nextInt();
System.out.println("Enter another score");
int number1 = sc.nextInt();
System.out.println("Enter another score");
int number2 = sc.nextInt();
System.out.println("Enter another score");
int number3 = sc.nextInt();
System.out.println("Enter another score");
int number4 = sc.nextInt();
System.out.println("Enter another score");
int number5 = sc.nextInt();
if (number >= 90)
{
A=A+1;
A++;
}
if (number >= 80)
{
B=B+1;
B++;
}
if (number >= 70)
{
C=C+1;
C++;
}
if (number >= 60)
{
D=D+1;
D++;
}
if (number >= 0)
{
F=F+1;
F++;
}
}
input = sc.nextInt();
while (input != -1);
{
System.out.println("Number of A's " + A);
System.out.println("Number of B's " + B);
System.out.println("Number of C's " + C);
System.out.println("Number of D's " + D);
System.out.println("Number of F's " + F);
}
}
}
``````

You have repeated code for number1 - number5 where you really only need one number - the loop will allow you to enter mutiple values...

``````print "Input grades scores, -1 to exit"
do
input a number
if (number >= 90) etc etc
while (number >= 0) // exit if number <0
print results
``````

WAs I correct on the incrementing of the variables ? Because I want it to show the number of A's if someone entered say 4 numbers above 90 it would say:
Number of A's 4
I was assuming I would do A=A+1 A++ but I don't know if that is correct or not.
Thank you so much for your help by the way.

A=A+1; and A++; both do exactly the same thing - they both add 1 to the value of A, so either is correct. Doing both will add 1 twice, giving you a result that's exactly 2x too big.

Okay I did all of that I could not manage the do loop I kept getting the error "while loop needed"
so I tried this:
(still doesn't work loop never ends)

``````import java.util.Scanner;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input;
System.out.println("Input grades scores, -1 to exit");
String garbage;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
System.out.println("Input numbers only 0 ... 100, -1 to exit you can enter up to 6 numbers");
int number = sc.nextInt();
while (number >= 0)
{
while (number >= 90)
{
A++;
}
while (number >= 80)
{
B++;
}
while (number >= 70)
{
C++;
}
while (number >= 60)
{
D++;
}
while (number >= 1)
{
F=F+1;
F++;
}
}
System.out.println("Number of A's " + A);
System.out.println("Number of B's " + B);
System.out.println("Number of C's " + C);
System.out.println("Number of D's " + D);
System.out.println("Number of F's " + F);
}
}
``````
``````import java.util.Scanner;
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input numbers only 0 ... 100, negative number to exit");
int number = sc.nextInt();
//get input
boolean run = true; //start value to run program again
//define integers to hold test score data
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int F = 0;
for (run = true; number > 0; number++)
if (number >= 90)
{
A++;
}
else if (number >= 80)
{
B++;
}
else if (number >= 70)
{
C++;
}
else if (number >= 60)
{
D++;
}
else if (number >= 1)
{
F++;
}
//output data
System.out.println("Number of A's " + A);
System.out.println("Number of B's " + B);
System.out.println("Number of C's " + C);
System.out.println("Number of D's " + D);
System.out.println("Number of F's " + F);
run=false; //have senitel value set here
//do you want to run again loop
if(run == false)
{
System.out.println("Would you like to run again? Y/N");
char again = sc.next().charAt(0);
again = Character.toUpperCase(again); //force all leters inputed to upper case, lower would work too if i change if conditions
if (again == 'Y')
{
run = true;
}
else if (again == 'N')
{
System.out.println("Goodbye.");
run=false;
}
}
}
}
``````