Results 1 to 7 of 7

Thread: loop wuestion

  1. #1
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default loop wuestion

    My question is ; how to take a value from a loop and continue the loop and take the new value again, print it and add it to the old one?

    thanks


    Java Code:
          while (numCourses <= 3) // loop 1 times
          {
             System.out.print( "Enter grade (one character): " ); // prompt 
    			
    		numCourses +=  1;
    			
              grade = input.next(); // read grade from user
    			  
    			  char c = grade.charAt(0);  // from string to Character 
    			  
    			  int points = 0;
    			  
    			 
    			  
    			// case switch
    			  
    			  switch (c) {
    			  
    			  
    			  
    			                                
                    case 'A':              
                    case 'a':
                   points = 4;
                        break;
                    case 'b':
                    case 'B':
                        points = 3;
                        break;
                    case 'c':
                    case 'C':
                        points = 2;
                        break;
                    case 'd':
                    case 'D':
                       points = 1;
                        break;
                   case 'f':
                    case 'F':
                        points = 0;
    				       break;
    		
                        
                }// end switch
                              
         Scanner kurs1 = new Scanner(System.in);
         System.out.println("Enter credits: ");
         int kredit;
         kredit = kurs1.nextInt();
         int sumkredit = 0;
         sumkredit += kredit;
         int point1 = 0;
         points = point1;
         System.out.println("Sum Points: "+ point1);
         System.out.println("Sum Credits: "+sumkredit);
         
         
         
         
         
         
    
    
          } // end while
    Last edited by mehnihma; 11-02-2011 at 10:23 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: loop wuestion

    By using addition. Check out line 5 of your code.

  3. #3
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: loop wuestion

    I am not sure what I need to do?

    Thanks

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: loop wuestion

    Neither am I since your first post is a bit vague. All I picked up on was that you want to add a value to a variable so you need to use addition. I really don't understand how this is difficult.

  5. #5
    mehnihma is offline Member
    Join Date
    Sep 2011
    Posts
    19
    Rep Power
    0

    Default Re: loop wuestion

    I need to add a value then use it and print it out
    then when again when it loops again I need that second value and print it and add first value to it and so on 3 times

    I need to enter two numbers, grade and credit and sum them

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: loop wuestion

    Then why do you need a loop? You just need to read those 2 values into 2 different variables and then have a third variable to hold the sum.

  7. #7
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: loop wuestion

    Ok from reading your post and your code what I understand is this.
    1) You want to input grade.
    2) than get points based on grade
    3) Input kredit
    4) get a sum of kredit
    5) get a sum of points
    5) print out sumPoint and sumKredit


    First of all i don't know if you want his or not your loops goes on for a total of 4 time because the condition is "<=" if you change it to "<" than it'll be 3 times.

    Second if you want to get a sum of the kredits or points than you need to declare them outside of the while loop. Meaning it should either be in the method or declared
    as a instance variable. With your current code every time the code goes into the next loop sumKredit and sumPoints are reset to "0" because those variables only last until
    the end of the loop. New loop = new value.

    Hope that helps

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •