Results 1 to 7 of 7
Thread: loop wuestion
- 11-02-2011, 10:13 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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 whileLast edited by mehnihma; 11-02-2011 at 10:23 PM.
- 11-02-2011, 10:36 PM #2
Re: loop wuestion
By using addition. Check out line 5 of your code.
- 11-02-2011, 10:45 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
Re: loop wuestion
I am not sure what I need to do?
Thanks
- 11-02-2011, 10:51 PM #4
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.
- 11-02-2011, 10:59 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 19
- Rep Power
- 0
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
- 11-02-2011, 11:14 PM #6
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.
- 11-03-2011, 12:54 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
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
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks