Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I need help with while loop.

  1. #1
    Junior Member unknowncode's Avatar
    Join Date
    Sep 2011
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post I need help with while loop.

    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
    Last edited by KevinWorkman; September 21st, 2011 at 07:19 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need help with while loop.

    Quote Originally Posted by vivjo View Post
    & 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:

    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;
    	}
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  2. help using for while loop
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 30th, 2010, 02:36 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM