Skip to main content

while looping

1 reply [Last post]
salmang
Offline
Joined: 2012-07-12
Points: 0

Can any one help me with this code so that it can loop calculating Exam GP.The code is working correctly but it is not calculating while it loop.Please see the cod below:

package grade;

import javax.swing.*;
import java.text.DecimalFormat;

class Grade
{
public static void main(String[]arg)
{
DecimalFormat dig = new DecimalFormat("0.00");
int grade;
int crUnit;
int counter ;
counter = 0;
double gp ;

String gradeStr = JOptionPane.showInputDialog("Enter grade or -1 to quit");
String crUnitStr = JOptionPane.showInputDialog("Enter Credit Unit or 0 to quit");
grade = Integer.parseInt(gradeStr);
crUnit = Integer.parseInt(crUnitStr);

int counters;
int pt;
pt = 1;
if(grade < 40)
{
pt = 0;
counters = crUnit * pt;
counter = counter + crUnit;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is" + counter);
}
else
if (grade >= 40 && grade <= 44)
{
pt = 1;
counters = crUnit * pt;
counter = counter + crUnit;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is" + counter);
}
else
if (grade >=45 && grade <=49)
{
pt = 2;
counters = crUnit * pt;
counter = counter + crUnit;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is" + counter);
}
else
if (grade >=50 && grade <=59)
{
pt = 3;
counter = counter + crUnit;
counters = counter * pt;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is" + counter);
}
else
if (grade >=60 && grade <= 69)
{
pt =4;
counter = counter + crUnit;
counters = counter * pt;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is" + counter);
}
else
if (grade >=70)
{
pt = 5;
counter = counter + crUnit;
counters = counter*pt;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is" +dig.format(gp) +"\n"+"Your total credit unit is"
+ counter+"\n"+"your point is"+ counters );
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tutortutor
Offline
Joined: 2012-09-09
Points: 0

If I understand you correctly, you would like to introduce a while loop so that you can keep entering grades and credit units until either -1 is entered as the grade or 0 is entered as the credit unit. I've rewritten your source code to accomplish this task. The reformatted code appears below:

import javax.swing.*;
import java.text.DecimalFormat;

class Grade
{
public static void main(String[] arg)
{
DecimalFormat dig = new DecimalFormat("0.00");
int grade;
int crUnit;
int counter;
counter = 0;
double gp;

while (true)
{
String gradeStr = JOptionPane.
showInputDialog("Enter grade or -1 to quit");
if (gradeStr.equals("-1"))
break;
String crUnitStr = JOptionPane.
showInputDialog("Enter Credit Unit or 0 to quit");
if (crUnitStr.equals("0"))
break;
grade = Integer.parseInt(gradeStr);
crUnit = Integer.parseInt(crUnitStr);

int counters;
int pt;
pt = 1;
if (grade < 40)
{
pt = 0;
counters = crUnit*pt;
counter = counter+crUnit;
gp = (double)counters/(double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter);
}
else
if (grade >= 40 && grade <= 44)
{
pt = 1;
counters = crUnit*pt;
counter = counter+crUnit;
gp = (double)counters/(double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter);
}
else
if (grade >= 45 && grade <= 49)
{
pt = 2;
counters = crUnit * pt;
counter = counter + crUnit;
gp = (double)counters / (double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter);
}
else
if (grade >= 50 && grade <= 59)
{
pt = 3;
counter = counter+crUnit;
counters = counter*pt;
gp = (double)counters/(double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter);
}
else
if (grade >= 60 && grade <= 69)
{
pt = 4;
counter = counter+crUnit;
counters = counter*pt;
gp = (double)counters/(double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter);
}
else
if (grade >= 70)
{
pt = 5;
counter = counter+crUnit;
counters = counter*pt;
gp = (double)counters/(double)counter;
JOptionPane.showMessageDialog(null, "Your GP is"+dig.format(gp)+"\n"+
"Your total credit unit is"+counter+
"\n"+"your point is"+counters);
}
}
}
}

I hope this helps.

Jeff
tutortutor.ca