Java - Loop

I add this part to the code.
int i;
   for (i = 1; i <=10; i++)
but it is not doing what I need.  
I just repeats the answer 10 times.  

How do you determine the numeric grade for one single letter grade, it repeatedly calculates and displays numeric grades for a sequence of exactly 10 grades?

Any help would greatly be appreciated.

package charatmethod;
import java.util.Scanner;
 
public class CharAtMethod
{
 
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
 
System.out.println("Enter a letter grade:");
String input = in.nextLine();
 
Grade g = new Grade(input);
 
double grade = g.getNumericGrade();
 
int i;
   for (i = 1; i <=10; i++)
System.out.println("Numeric value: " + grade);
}
 
}
************************************
package charatmethod;
public class Grade
{
    public Grade(String initGrade)
    {
        letterGrade = initGrade;
    }
public double getNumericGrade()
{
    double grade = 0;
switch(letterGrade.charAt(0)) 
{
case 'A': grade = 4.0;break;
case 'B': grade = 3.0; break;
case 'C': grade = 2.0; break;
case 'D': grade = 1.0; break;
default: grade = 0.0; break;
 
}
 
for (int i = 0; i < letterGrade.length(); i++)
{
   if (letterGrade.charAt(i) == '-')   
       grade = grade - 0.3;
     else if (letterGrade.charAt(i) == '+')
        grade = grade + 0.3;
}
    return grade;
            
}  
    private String letterGrade;
}

Open in new window

ca1358Asked:
Who is Participating?
 
Kevin CrossChief Technology OfficerCommented:
Think you want something more like this so that you keep prompting for letter grade and displaying for 10 times and then stops.
public class CharAtMethod {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
 
        Grade g;
        double grade;
        
        for (int i = 1; i <= 10; i++) {
            System.out.println("Enter a letter grade:");
            g = new Grade(in.nextLine());
            grade = g.getNumericGrade();
            System.out.println("Numeric value: " + grade);
        }
    }
}

Open in new window

0
 
marklorenzCommented:
I'm not totally sure what you're trying to do.  If you take out the for loop in your main method, you will ask for one letter grade and output the related numeric grade.  If you want a loop, it would have to encompass the part where you ask for a grade.  You'd probably want to go until nothing is entered, to indicate it's done (versus a for loop).
0
 
ca1358Author Commented:
I am not sure what it asking.

It states
Alter the main class of the project so that, instead of just determining the numeric grade for one single letter grade, it repeatedly calculates and displays numeric grades for a sequence of exactly 10 grades.  

You should add a loop to the main method that will ensure that the process of "retrieve letter grade, then calculate and display the numeric grade, will be repeated 10 times.

Of course it gives no explanation of how to do this.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
quincydudeCommented:
do you actually want something like
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
String input;
Grade g;
double grade;
 
 
int i;
   for (i = 1; i <=10; i++)
	{
	   System.out.println("Enter a letter grade:");
		input = in.nextLine();
		g = new Grade(input);
		grade = g.getNumericGrade();
		System.out.println("Numeric value: " + grade);
	}
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
That is what I get for not refreshing, what quincydude is pretty much what I was trying to show you.  You just needed to move the code for input and conversion from letter grade to inside the for loop.

This is just hte basics of the solution and should get you started.  You will have to ensure that you get valid input each time if they want 10 good executions.  Other interpretation/implementation could be to allow entry of 10 grades in a row and then calculate and display all ten at once.  Again that is all based on the above code as a start so good luck.

Regards,
Kevin
0
 
ca1358Author Commented:
Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.