0

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

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

Edited by mehnihma: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by thines01
0

Your program seems to be out of order.
Can you explain the order the questions are supposed to have?

0

Yes I know

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

0

So, without making it too fancy (and without writing the whole thing), consider keeping the totals on the outside of the loop:

import java.util.Scanner;

public class DW_390767
{
	private static int getPoints(char cGrade)
	{
		int points = 0;
		switch (cGrade)
		{
			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':
			default:
			points = 0;
			break;
		}

		return points;
	}

	public static void main(String[] args)
	{
		int numCourses = 0;

		String grade = "";

		Scanner input = new Scanner(System.in);

		System.out.println("Enter number of courses");
		numCourses = input.nextInt();
		int intCredits = 0;
		int intPoints = 0;

		for(int intCourseCount = 0; intCourseCount < numCourses; intCourseCount++)
		{
			System.out.println("Course " + (intCourseCount+1) + " of " + numCourses);


			System.out.println("Enter the grade for course " + (intCourseCount+1));
			char chrGrade = (char)input.next().charAt(0);

			intPoints += getPoints(chrGrade);

			System.out.println("Enter credits for course " + (intCourseCount+1));
			intCredits += input.nextInt();

		}
	}
}

That will get you a start and you can add up the totals.
Later, you can make it fancier.

Edited by thines01: typo

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.