I need help with while loop.
Code java:
import java.util.Scanner;
public class ExamScores
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;
int scores, totalnumber;
System.out.println("Enter all your exam scores here: ");
System.out.println("Enter a negative number after ");
System.out.println("you have entered all the scores.");
totalnumber = 0;
scores = input.nextInt();
while (scores >= 0)
{
scores = input.nextInt();
totalnumber++;
if (scores >= 90 & scores <= 100){
gradeA = gradeA + 1;}
else if (scores <= 89 & scores >= 80){
gradeB = gradeB + 1;}
else if (scores <= 79 & scores >= 70){
gradeC = gradeC + 1;}
else if (scores <= 69 & scores >= 60){
gradeD = gradeD + 1;}
else if (scores < 60 & scores >= 0){
gradeF = gradeF + 1;}
else
System.out.println("");
}
System.out.println
("Total number of grades = " + totalnumber
+ "\nNumber of A's = " + gradeA
+ "\nNumber of B's = " + gradeB
+ "\nNumber of C's = " + gradeC
+ "\nNumber of D's = " + gradeD
+ "\nNumber of F's = " + gradeF);
}
}
for example the input:
98 87 78 77 85 68 55 -1
The output:
Total number of grades = 7
Number of A's = 1
Number of B's = 2
Number of C's = 2
Number of D's = 1
Number of F's = 1
Re: I need help with while loop.
Please read the link in my signature on asking questions the smart way before you post again. What is the problem? What is your question?
I've added highlight tags as well as moved your post to a more appropriate forum.
Re: I need help with while loop.
Hey,
how wiil the while loop termimates ,
the variable score never changes after inputting from keyboard and moreover to put a series of scores use "Input Statement" within While
Hope this helps
Cheers
Re: I need help with while loop.
"Change this"
scores=1;
Before( scores>0)
& is Bitwise And Operator
Use && and it would work
if(a>b && a>c)
The code is working fine
Re: I need help with while loop.
Quote:
Originally Posted by
vivjo
& is Bitwise And Operator
Use && and it would work
if(a>b && a>c)
The code is working fine
Hmm, that should still work. Using a single & (or |) instead of the double && or || will simply cause both statements to be evaluated instead of shortcutting. With double &&, if the left side is false, it doesn't bother testing the right side. Same with double ||, only with true instead of false.
With a single & or |, it tests both sides no matter what. Try out different values for t with both & and && in this program:
Code java:
public class Test{
public static void main(String... args)
{
int x = 9;
boolean test = testVariable(x, 4) & testVariable(x, 5);
System.out.println(test);
}
public static boolean testVariable(int x, int t){
System.out.println("Testing: " + x + " < " + t + ": " + (x < t) );
return x < t;
}
}