Java application output data are missing something - java

Thank you some member help me to correct the code, but I have more questions to ask :
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import javax.swing.*;
public class Average2 {
public static void main(String args[]) {
int total;
int gradeCounter;
int grade = 0;
int result;
int passed = 0;
int failed = 0;
int absent = 0;
double average;
String gradeString;
String s1;
total = 0;
gradeCounter = 1;
gradeString = JOptionPane.showInputDialog("Enter Exam Marks first or -1 to Quit:");
grade = Integer.parseInt(gradeString);
String output = "Name of the Student\tExam Marks\n";
String ns = "No. of student passed: \t No. of students failed: \t No. of student Absent: \n";
if (grade != -1) {
s1 = JOptionPane.showInputDialog("Enter the Name of Student - ");
output += gradeCounter + "\t" + s1 + "\t" + gradeString + "\n";
ns = "no. of students passed:" + passed + "\n no. of students failed:" + failed + "\n no. of students absent:" + absent;
while (grade != -1) {
if(grade >= 40){
passed = passed + 1;
}
else if(grade > 0 && grade < 40){
failed = failed + 1;
}
else if(grade > 0 && grade <1){ //why grade can't check = 0??
absent = absent + 1; //
}
total = total + grade;
gradeCounter = gradeCounter + 1;
gradeString = JOptionPane.showInputDialog("Enter Exam Marks or -1 to Quit:" + gradeCounter);
grade = Integer.parseInt(gradeString);
if (grade == -1) {
break;
}
s1 = JOptionPane.showInputDialog("Enter the Name of Student - " + gradeString);
output += gradeCounter + "\t" + s1 + "\t" + gradeString + "\n";
ns = "no. of students passed:" + passed + "\n no. of students failed:" + failed + "\n no. of students absent:" + absent;
}
}
DecimalFormat twoDigits = new DecimalFormat("0.00");
if (gradeCounter != 0) {
average = (double) total / (gradeCounter-1);
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"Analysis of Exam Marks", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, ns,
"Analysis of Exam Marks", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Class average is " + twoDigits.format(average), "Class Average",
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "No grades were entered", "Class Average",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
http://i.stack.imgur.com/sn8VJ.png
http://i.stack.imgur.com/4yG6g.png
why if(grade = 0) will be error and the the result absent can't increase??
In this case, how to use the Math.squr to calculate standard deviation??

This is because you are reading the two inputs outside your while loop the first time and then replacing their values inside the while loop again.
gradeString = JOptionPane.showInputDialog("Enter Integer Grade or -1 to Quit:" );
s1 = JOptionPane.showInputDialog( "Enter the Name of Student - " );
grade = Integer.parseInt( gradeString );
You are appending these inputs to your output String only in the while loop and not outside.
You can do one of the following:
Append it to the output outside your while loop by doing:
String output = "Name of the Student\tExam Marks\n";
output += gradeCounter + "\t" + s1 + "\t" + gradeString + "\n";
Simple remove these statements as you are anyways calling them inside your while loop

Made the changes in the following version of the code. It should work as you wanted.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import javax.swing.*;
public class Average {
public static void main(String args[]) {
int total;
int gradeCounter;
int grade;
double average;
String gradeString;
String s1;
total = 0;
gradeCounter = 1;
gradeString = JOptionPane.showInputDialog("Enter Integer Grade or -1 to Quit:");
grade = Integer.parseInt(gradeString);
String output = "Name of the Student\tExam Marks\n";
if (grade != -1) {
s1 = JOptionPane.showInputDialog("Enter the Name of Student - ");
output = gradeCounter + "\t" + s1 + "\t" + gradeString + "\n";
while (grade != -1) {
total = total + grade;
gradeCounter = gradeCounter + 1;
gradeString = JOptionPane.showInputDialog("Enter Integer Grade or -1 to Quit:" + s1);
grade = Integer.parseInt(gradeString);
if (grade == -1) {
break;
}
s1 = JOptionPane.showInputDialog("Enter the Name of Student - " + gradeCounter);
output += gradeCounter + "\t" + s1 + "\t" + gradeString + "\n";
}
}
DecimalFormat twoDigits = new DecimalFormat("0.00");
if (gradeCounter != 0) {
average = (double) total / gradeCounter;
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea, "Analysis of Exam Marks", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Class average is " + twoDigits.format(average), "Class Average",
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "No grades were entered", "Class Average",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

Related

How to end the while loop with either player1 or player2 entering "Q"?

I am stuck at a part where in a game, I use while loop and to end the loop and get the results of the game, I want either "player1" or "player2" to enter "Q", and so i tried doing it like this:
if (player1.equals("Q") || player2.equals("Q")){
go = false; //go is a boolean variable
}
This doesn't seem to work as I have to enter "Q" for both player1 and player2 for the game to end, but instead I just want either of them to enter "Q" and the game would stop.
Code:
import java.util.Scanner;
public class Team {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Soccer Game Between 2 Teams");
System.out.println("Win is 2 points" + "\n" + "Loss is worth 0 points" + "\n" + "Overtime is worth 1 point");
System.out.println("Type W, O, or L" + "\n" + "Type Q to end the game");
int pointsw = 0;
int pointsl = 0;
int pointso = 0;
int pointsw2 = 0;
int pointsl2 = 0;
int pointso2 = 0;
int totalpoints = 0;
int totalpoints2 = 0;
int counter = 0;
int counter2 = 0;
boolean go = true;
System.out.println("\n" + "Enter team one:");
String phrase = keyboard.next();
System.out.println("\n" + "Enter team two:");
String phrase2 = keyboard.next();
System.out.println();
while (go) {
System.out.println("Enter " + phrase + " Result:");
String team1 = keyboard.next();
System.out.println("Enter " + phrase2 + " Result");
String team2 = keyboard.next();
if (team1.equals("W") || team1.equals("w")) {
pointsw += 2;
} else if (team1.equals("O") || team1.equals("o")) {
pointso += 1;
} else if (team1.equals("L") || team1.equals("l")) {
pointsl += 0;
}
counter++;
if (team2.equals("W") || team2.equals("w")) {
pointsw2 += 2;
} else if (team2.equals("O") || team2.equals("o")) {
pointso2 += 1;
} else if (team2.equals("L") || team2.equals("l")) {
pointsl2 += 0;
}
counter2++;
totalpoints = pointsw + pointso + pointsl;
totalpoints2 = pointsw2 + pointso2 + pointsl2;
if (team1.equals("Q") || team2.equals("Q")) {
go = false;
if (totalpoints > totalpoints2) {
System.out.println(phrase + " wins with " + totalpoints + " points");
System.out.println("It took " + phrase + " " + counter + " rounds to win");
} else if (totalpoints < totalpoints2) {
System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
} else if (totalpoints == totalpoints2) {
int totalrounds = counter + counter2;
System.out.println("It is tie game between " + phrase + " and " + phrase2);
System.out.println("The game lasted till " + totalrounds + " rounds");
}
}
}
}
}
You should reorganize your code:
while (true) {
System.out.println("Enter " + phrase + " Result:");
String team1 = keyboard.next().toLowerCase();
if ("q".equals(team1)) {
break;
}
System.out.println("Enter " + phrase2 + " Result");
String team2 = keyboard.next().toLowerCase();
if ("q".equals(team2)) {
break;
}
if (team1.equals("w")) {
pointsw += 2;
} else if (team1.equals("o")) {
pointso += 1;
} else if (team1.equals("l")) {
pointsl += 0;
}
counter++;
if (team2.equals("w")) {
pointsw2 += 2;
} else if (team2.equals("o")) {
pointso2 += 1;
} else if (team2.equals("l")) {
pointsl2 += 0;
}
counter2++;
totalpoints = pointsw + pointso + pointsl;
totalpoints2 = pointsw2 + pointso2 + pointsl2;
} // loop completed
if (totalpoints > totalpoints2) {
System.out.println(phrase + " wins with " + totalpoints + " points");
System.out.println("It took " + phrase + " " + counter + " rounds to win");
} else if (totalpoints < totalpoints2) {
System.out.println(phrase2 + " wins with " + totalpoints2 + " points");
System.out.println("It took " + phrase2 + " " + counter2 + " rounds to win");
} else if (totalpoints == totalpoints2) {
int totalrounds = counter + counter2;
System.out.println("It is tie game between " + phrase + " and " + phrase2);
System.out.println("The game lasted till " + totalrounds + " rounds");
}
I'm not completely sure, but I think the issue is that after player 1 / player 2 says 'Q'
the scanner is still waiting for the next line to read.
String phrase = keyboard.next();
System.out.println("\n"+"Enter team two:");
String phrase2 = keyboard.next();//if player 1 types q this next() method must be resolved before it will continue to the logic
so add an if statement before play 2 goes asking if player 1 typed 'Q' , if so calculate scores and end game, if player 1 did not type 'Q' use else statement to continue on to player 2's turn

Adding more classes to a prewritten program

I am being tasked with adding 2 more classes to a prewritten program, one that does all calculations and one that prints the total bill. I understand how to add other classes but im a bit confused since the program already looks like it does the calculations inside of the main class.
Ive tried just adding in the classes but it throws errors because its missing information obviously.
public static void main(String[] args)
{
String squantity, snumber, output, line_output = "";
String [] item = new String [5];
double [] cost = new double [5];
double [] quantity = new double [5];
double [] amount = new double [5];
int number, i;
double grandtotal = 0;
String costout, amountout, grandtotalout;
DecimalFormat df2 = new DecimalFormat("$##,###.00");
for(i=0;i<=4;++i)
{
output = " Acme Grocery Store" + "\n" +
"1-Green Beans $0.35 per pound" + "\n" +
"2-Yellow Beans $0.40 per pound" + "\n" +
"3-Head Lettuce $0.79 per pound" + "\n" +
"4-Leaf Lettuce $1.98 per pound" + "\n" +
"5-Hot House Tomatoes $0.99 per pound" + "\n" +
"6-Hydro Tomatoes $3.98 per pound" + "\n" + "\n" +
"Please make your selection ";
snumber = JOptionPane.showInputDialog(null,
output, "Input Data", JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(snumber);
squantity = JOptionPane.showInputDialog(null,
"Enter Quantity", "Input Data", JOptionPane.QUESTION_MESSAGE);
quantity[i] = Double.parseDouble(squantity);
//code for the calculation
if(number == 1)
{
cost[i] = 0.35;
item[i]="Green Beans";
}
else if(number == 2)
{
cost[i] = 0.4;
item[i]="Yellow Beans";
}
else if(number == 3)
{
cost[i] = 0.79;
item[i]="Head Lettuce";
}
else if(number ==4)
{
cost[i] = 1.98;
item[i]="Leaf Lettuce";
}
else if (number==5)
{
cost[i] = 0.99;
item[i]="Hot House Tomatoes";
}
else
{
cost[i] = 3.98;
item[i]="Hydro Tomatoes";
}
amount[i]=cost[i]*quantity[i];
costout=df2.format(cost[i]);
amountout=df2.format(amount[i]);
line_output=line_output+item[i]+" "+costout+" "+amountout+"\n";
grandtotal=grandtotal + amount[i];
}//for loop
grandtotalout=df2.format(grandtotal);
output=line_output+"\n"+ "The total grocery bill = "+grandtotalout;
JOptionPane.showMessageDialog(null, output, " ", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}//main
We are expected to add the classes
class grocery {
}
class printbill{
}
I tried messing with the extends function but I dont think that is correct either
As a starter, your grocery class would look something like this:
final class Grocery
{
Grocery(String InputItem, double InputCost, double InputQuantity)
{
Item = InputItem;
Cost = InputCost;
Quantity = InputQuantity;
}
public final String GetItem()
{
return Item;
}
public final double GetCost()
{
return (double)InputQuantity * Cost;
}
private final String Item;
private final double Cost;
private final int Quantity;
}
The PrintBill class may go like this:
final class PrintBill
{
PrintBill(Grocery [] InputGroceries)
{
Groceries = InputGroceries;
}
public final void Print()
{
double Cost, TotalCost = 0.0;
String Line = "";
String Item;
for (int i = 0; i < Groceries.length; i++)
{
Cost = Groceries[i].GetCost();
Item = Groceries[i].GetItem();
TotalCost += Cost;
// I'll leave it up to you to print this info out.
Line = Item + " " + Cost +" "+ TotalCost +"\n";
}
JOptionPane.showMessageDialog(null, output, " ",
JOptionPane.INFORMATION_MESSAGE);
}
private final Grocery [] Groceries;
}
Your main() would then be:
public static void main(String[] args)
{
String squantity, snumber, output, line_output = "";
Grocery [] Groceries = new Grocery [4];
PrintBill TheBill;
int number;
for (int i = 0; i < Groceries.length; i++)
{
output = " Acme Grocery Store" + "\n" +
"1-Green Beans $0.35 per pound" + "\n" +
"2-Yellow Beans $0.40 per pound" + "\n" +
"3-Head Lettuce $0.79 per pound" + "\n" +
"4-Leaf Lettuce $1.98 per pound" + "\n" +
"5-Hot House Tomatoes $0.99 per pound" + "\n" +
"6-Hydro Tomatoes $3.98 per pound" + "\n" + "\n" +
"Please make your selection ";
snumber = JOptionPane.showInputDialog(null,
output, "Input Data", JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(snumber);
squantity = JOptionPane.showInputDialog(null,
"Enter Quantity", "Input Data", JOptionPane.QUESTION_MESSAGE);
quantity = Double.parseDouble(squantity);
if(number == 1)
{
Groceries[i] = new Grocery("Green Beans", 0.35, quantity);
}
else if(number == 2)
{
Groceries[i] = new Grocery("Yellow Beans", 0.4, quantity);
}
// etc...
}
TheBill = new PrintBill(Groceries);
TheBill.Print();
}
More work is required to actually complete this but it should give you a starting point anyway.
Happy Days :-)

how do I pass a user inputted integer through methods?

Essentially i've isolated the issue, the int numpersons begins as 0. I take a user input to make it a particular number which is the array size, when the second method begins it takes the 0 again and then the array has an out of bounds exception. I want to pass it from one method to the next, or make them more successive, idk how to do this
thanks in advance
import java.util.Scanner;
public class BankApp {
Scanner input = new Scanner(System.in);
int numpersons = 0;
private SavingsAccount[] clients = new SavingsAccount[numpersons];
public BankApp() {
while (numpersons < 1) {
System.out.println("How many people are there?");
numpersons = input.nextInt();
if (numpersons < 1 || 2147483647 < numpersons) {
System.out.println("invalid number, please enter again");
}
}
input.nextLine();
}
public void addClients() {
int i = 0;
while (i < numpersons) {
System.out.println("enter account id " + (i + 1));
String AccountID = input.nextLine();
System.out.println("enter account name " + (i + 1));
String AccountName = input.nextLine();
System.out.println("enter account balance " + (i + 1));
Double AccountBalance = input.nextDouble();
clients[i] = new SavingsAccount(AccountID, AccountName, AccountBalance);
input.nextLine();
i++;
}
}
public void displayClients() {
int i = 0;
while (i < numpersons) {
System.out.println("======================================");
System.out.println("Account ID " + (i + 1) + ": " + clients[i].getID());
System.out.println("Account Name " + (i + 1) + ": " + clients[i].getName());
System.out.println("Account Balance " + (i + 1) + ": " + clients[i].getBalance());
System.out.println("======================================");
i++;
}
}
public static void main(String args[]) {
BankApp ba = new BankApp();
ba.addClients();
ba.displayClients();
}
}

Looping program back to ''menu''

I just writed this program, it is to train myself for the upcomming exam this monday.
A thing i would like to add is: after a user is done with one of the exchange options 1/2/3 i would like to give the option to let the user return to the beginning welcome to the money exchange! etc.....
i have tried some a for loop and a while loop but i couldn't get it to work.
Would be cool if after the money exchange process that the user get the option to return to the beginning by typing y or n is this possible?
/* This program is written as a excercise to prep myself for exams.
* In this program the user can:
* 1. Select a currency (other than euro's)
* 2. Input the amount of money
* 3. transfer the amount of currency to euro's
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n ");
System.out.print("Input : ");
DecimalFormat df = new DecimalFormat() ;
df.setMaximumFractionDigits(2);
int choice = input.nextInt() ;
double transfee = 2.41 ;
double USrate = 0.9083 ;
double GBrate = 1.4015 ;
double YENrate = 0.0075 ;
if (choice > 3 || choice < 1) {
System.out.println("Invalid input!...... Please try agian\n");
} else {
if(choice == 1) {
System.out.println("You have choosen for US dollar \n");
System.out.print("Please enter amount US dollar: ");
double USamount = input.nextDouble() ;
double deuros = USamount * USrate ;
double ddisburse = deuros - transfee ;
System.out.print("\nInput amount US dollar:. " + USamount + "\n");
System.out.print("Worth in euro's:........ " + df.format(deuros) + "\n");
System.out.print("Transfer cost:.......... " + transfee + "\n");
System.out.print("Amount to disburse:..... " + df.format(ddisburse) + "\n" );
}else {
if(choice == 2){
System.out.println("You have choosen for GB pounds");
System.out.print("Please enter amount GB ponds: ");
double GBamount = input.nextDouble();
double geuros = GBamount * GBrate ;
double gdisburse = geuros - transfee;
System.out.print("\nInput amount GB pound:. " + GBamount + "\n");
System.out.print("Worth in euro's........ " + df.format(geuros) + "\n");
System.out.print("Transfer cost:......... " + transfee + "\n");
System.out.print("Amount to disburse:.... " + df.format(gdisburse) + "\n");
}else {
if(choice == 3){
System.out.println("You have choosen for Yen");
System.out.print("Please enter amount Yen: ");
double YENamount = input.nextDouble();
double yeuros = YENamount * YENrate ;
double ydisburse = yeuros - transfee ;
System.out.print("\nInput amount Yen:... " + YENamount + "\n");
System.out.print("Worth in euro's..... " + df.format(yeuros) + "\n");
System.out.print("Transfer cost:...... " + transfee + "\n");
System.out.print("Amount to disburse:. " + df.format(ydisburse) + "\n");
}
}
}
}
}
}
You could wrap your program with a while loop, which checks if the user entered 'y' at the end like this:
import java.text.DecimalFormat;
import java.util.Scanner;
class YourClassName
{
public static void main(String[] args)
{
boolean askAgain = true;
while (askAgain)
{
Scanner input = new Scanner(System.in);
System.out.println(
" Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n ");
System.out.print("Input : ");
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
int choice = input.nextInt();
double transfee = 2.41;
double USrate = 0.9083;
double GBrate = 1.4015;
double YENrate = 0.0075;
if (choice > 3 || choice < 1)
{
System.out.println("Invalid input!...... Please try agian\n");
} else
{
if (choice == 1)
{
System.out.println("You have choosen for US dollar \n");
System.out.print("Please enter amount US dollar: ");
double USamount = input.nextDouble();
double deuros = USamount * USrate;
double ddisburse = deuros - transfee;
System.out.print(
"\nInput amount US dollar:. " + USamount + "\n");
System.out.print("Worth in euro's:........ "
+ df.format(deuros) + "\n");
System.out.print(
"Transfer cost:.......... " + transfee + "\n");
System.out.print("Amount to disburse:..... "
+ df.format(ddisburse) + "\n");
} else
{
if (choice == 2)
{
System.out.println("You have choosen for GB pounds");
System.out.print("Please enter amount GB ponds: ");
double GBamount = input.nextDouble();
double geuros = GBamount * GBrate;
double gdisburse = geuros - transfee;
System.out.print(
"\nInput amount GB pound:. " + GBamount + "\n");
System.out.print("Worth in euro's........ "
+ df.format(geuros) + "\n");
System.out.print(
"Transfer cost:......... " + transfee + "\n");
System.out.print("Amount to disburse:.... "
+ df.format(gdisburse) + "\n");
} else
{
if (choice == 3)
{
System.out.println("You have choosen for Yen");
System.out.print("Please enter amount Yen: ");
double YENamount = input.nextDouble();
double yeuros = YENamount * YENrate;
double ydisburse = yeuros - transfee;
System.out.print("\nInput amount Yen:... "
+ YENamount + "\n");
System.out.print("Worth in euro's..... "
+ df.format(yeuros) + "\n");
System.out.print(
"Transfer cost:...... " + transfee + "\n");
System.out.print("Amount to disburse:. "
+ df.format(ydisburse) + "\n");
}
}
}
}
System.out.println("Do you want to do another calculation? (y/n)");
String againAnswer = input.next();
askAgain = againAnswer.equalsIgnoreCase("y");
}
}
}
Setting the boolean variable to true first lets you enter the loop. The user will be asked as long as he types an y at the end. Every other character would exit the loop:
String againAnswer = input.next();
askAgain = againAnswer.equalsIgnoreCase("y");
You could also check for explicit n, but that is up to you.
Put the code inside a while loop (while(true)). At the end of each if block
add one nested if.
System.out.print(Do you want to continue?");
if(in.next().equals("Y")) {
continue;
}
And you have add one extra menu(4th) for exit :
if(choice == 4){
break;
}

if statement never executed

Why doesn't this code enter into the if statement?
public class GradeCalculator {
public static void main(String[] args) {
/*
* A 900 - 1000(90% to 100%) B 800 - 899(80% to 89%)
* C 700 - 799(70% to 79%) D 600 - 699(60% to 69%)
* F 599 and below (Below 60%)
*/
String Name = JOptionPane.showInputDialog("Please enter your name: ");
String pointsEarned = JOptionPane.showInputDialog("Please enter the points earned: ");
String possiblePoints = JOptionPane.showInputDialog("Please enter the total points possible: ");
double pe = Double.parseDouble(pointsEarned);
double pp = Double.parseDouble(possiblePoints);
double grade = (pe/pp)*100;
char LetterGrade;
if(grade>=900){
LetterGrade = 'A';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you for an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
}
}
}
You have calculated percentage that will not be above 100.Therefore just change if condition to
if (grade>=90)
You can simply debug and see what happens there.Add else block and see value of grade.
if(grade>=900){
LetterGrade = 'A';
// ....
}else{
System.out.print(grade);
}
Your grade variable is a percentage and you are comparing it as if it were the score you calculated the percentage on. So it looks like what you want is just to use pe instead of the grade variable.
if(pe>=900){
then only calculate percentage when displaying
double percentage = (pe/pp)*100
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + percentage + "%" + " and you for an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
Either that or consider grade variable as percentage when testing it
if (grade>=90) //90%
Also
Variables should always start with a lowercase letter so LetterGrade should be letterGrade
That's how I solved it.
import javax.swing.JOptionPane;
public class GradeCalculator
{
public static void main(String[] args)
{
boolean exitLoop = false;
while(exitLoop == false)
{
String Name = JOptionPane.showInputDialog("Please enter your name: ");
String pointsEarned = JOptionPane.showInputDialog("Please enter the points earned: ");
String possiblePoints = JOptionPane.showInputDialog("Please enter the total points possible: ");
double pe = Double.parseDouble(pointsEarned);
double pp = Double.parseDouble(possiblePoints);
double grade = (pe*100)/pp;
double Lgrade = pe;
char LetterGrade;
if(Lgrade >= 900)
{
LetterGrade = 'A';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <899 && Lgrade >=800)
{
LetterGrade = 'B';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <799 && Lgrade >=700)
{
LetterGrade = 'C';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if (Lgrade <699 && Lgrade >=600)
{
LetterGrade = 'D';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
} else if(Lgrade <599)
{
LetterGrade = 'F';
JOptionPane.showMessageDialog(null, Name + " your grade percentage you earned is " + grade + "%" + " and you got an " + LetterGrade, "Your Grades", JOptionPane.PLAIN_MESSAGE);
}
int selectedOption = JOptionPane.showConfirmDialog(null, "Do you want to run the program again?", "Yes or No", JOptionPane.YES_NO_OPTION);
if(selectedOption == JOptionPane.YES_OPTION)
{
exitLoop = false;
}
else
{
exitLoop = true;
}
}
}
}

Categories