if statement never executed - java

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;
}
}
}
}

Related

Increase and decrease "money amount" based on win or loss of game

I have made a dice roll game and i'm trying to increase your "cash" by whatever the inputted bet is when you win and remove it when you lose.
import java.util.*;
import javax.swing.JOptionPane;
public class Joption10 {
public static void main(String[] args) {
Random randomNumber = new Random();
//Variables
String name, bet;
int num1, num2;
int cash = 100;
int convertbet;
name = JOptionPane.showInputDialog(null, "Enter Your First Name");
JOptionPane.showMessageDialog(null, "Greetings " + name + ", welcome to roll the dice!" +"\n\nYou will start with " + cash + " euros in your wallet!" + "\n\nThe game ends when you are broke, or when you have doubled your money to 200 euros." + "\n\nGood Luck!");
while (cash > 0 && cash < 200) {
//Generate random numbers for player and dealer
num1 = randomNumber.nextInt(10) + 1; //player
num2 = randomNumber.nextInt(10) + 1; //dealer
bet = JOptionPane.showInputDialog(null, "Place your bet (1 - 100): ");
convertbet = Integer.parseInt(bet);
//Rolling
JOptionPane.showMessageDialog(null, "Rolling the dice...");
if (num2 > num1) {
JOptionPane.showMessageDialog(null, "You Win!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
cash + 10
} else if (num2 < num1) {
JOptionPane.showMessageDialog(null, "You Lose!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
} else {
JOptionPane.showMessageDialog(null, "No Winner!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
}
JOptionPane.showMessageDialog(null, "You have " + cash + " euros left...");
}
JOptionPane.showMessageDialog(null, "You have won games!");
System.exit(0);
}//Close Main
}//Close Class
If I understand it you should add the convertbet amount when you win and substract it when you loose to the cash.
if (num2 > num1) {
JOptionPane.showMessageDialog(null, "You Win!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
cash += convertbet
} else if (num2 < num1) {
JOptionPane.showMessageDialog(null, "You Lose!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
cash -= convertbet
} else {
JOptionPane.showMessageDialog(null, "No Winner!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
}
Watch out you where adding 10 to cash but assigning the returned value nowhere. You can use += operator to add and also assign the value to the variable.

How to Fix: "Must be an array type but is resolved to a string"

Creating a program that uses a menu to call separate modules of a simple health tracker for a beginner programming course.
Would appreciate some help concerning the exact reason why the array isn't working properly and is "resolved to a string"
I have a lot more to add before i can submit the program but this is holding me up.
It is in Module 3, the line attempting to recall the array
I'm leaving the entire code so far here because I don't understand what I've done wrong and am hoping this place is more helpful than the useless forums at uni.
public class HealthMate {
double bmi, bmr, heightM, weightKG;
int age, week = 7, days = 1;
int calories[] = new int[days];
int menuChoiceInt;
char genderChar;
boolean male;
public static void main(String[] args) {
HealthMate firstObj = new HealthMate();
firstObj.menu();
}
public void menu() {
while (menuChoiceInt != 4) {
String menu = "HealthMate Alpha 0.1 \n " + "Please make a numerical selection \n";
menu += "[1] Enter or Update your Details\n";
menu += "[2] Return BMI and BMR \n"; // menu options call different modules
menu += "[3] Weekly Tracker and Advice \n";
menu += "[4] Exit \n";
String menuChoiceString = JOptionPane.showInputDialog(menu);
menuChoiceInt = Integer.parseInt(menuChoiceString);//
if (menuChoiceString != null) {
if (menuChoiceInt == 1) {
genderChar = JOptionPane.showInputDialog("Please Enter your Gender as either M or F").charAt(0);
heightM = Double.parseDouble(
JOptionPane.showInputDialog("Enter Height in Meters,\n eg 1.73 for 173 cm.: "));
if (heightM <= 0) {
heightM = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
}
weightKG = Double.parseDouble(JOptionPane.showInputDialog("Enter Weight in Kilograms"));
if (weightKG <= 0) {
weightKG = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
}
bmi = weightKG / Math.pow(heightM, 2.0);
male = genderChar == 'M';
if (male) {
bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) + 5;
} else {
bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) - 161;
JOptionPane.showMessageDialog(null,"Your Specific BMI and BMR have been ");
menuChoiceInt = Integer.parseInt(menuChoiceString);// recall menu
}
}
if (menuChoiceInt == 2) if (bmi < 18.5) {
JOptionPane.showMessageDialog(null,
"Your BMI is " + bmi + ", You are underweight.\n" + "Your BMR is " + bmr);
} else if (bmi < 25) {
JOptionPane.showMessageDialog(null, "Your BMI is " + bmi
+ ", You are within the healthy weight range.\n" + "Your BMR is " + bmr);
} else if (bmi < 30) {
JOptionPane.showMessageDialog(null,
"Your bmi is " + bmi + ", You are overweight\n" + "Your BMR is " + bmr);
} else {
JOptionPane.showMessageDialog(null,
"Your bmi is " + bmi + ", You are Obese" + "Your BMR is " + bmr);
}
JOptionPane.showMessageDialog(null,
"This module is supposed to recall your BMI and BMR \n"
+ "and give general advice on health.");
{
menuChoiceInt = Integer.parseInt(menuChoiceString);
}
if (menuChoiceInt == 3) {
while (days > week) {
calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day"[days]);// employee salary
days = days + 1;
JOptionPane.showMessageDialog(null,
"This module is supposed to store data in an array over the course \n"
+ "of a week to show you your pattern of intake vs output.");
}
{
menuChoiceInt = Integer.parseInt(menuChoiceString);
}
} else if (menuChoiceInt == 4) {
}
}
}
}
}
I'm trying to get the calorie input to be saved over the course of 7 days so I can average it out, compare it to BMR and Activity level and give general advice on whether you are in surplus or deficit of calorie intake.
PS: Maybe if you have years of experience don't start your reply with "Well its obvious that..." and continue your mockery of someone who started programming less than a month ago as you people so often seem to on this website.
You have int days = 1, but you use it as [day] - this is incorrect in java:
calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day " + days));

How to do, Do while loop for this code?

I have this java code:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
String name;
int age;
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
do{
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
if(age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
while(age > 300);
System.out.println("It's not correct..Try Again, " + name +
".");
}
}
and I need that user write the wrong answer, he will get the question again "how old are you?", and after he get another tries..
what I goona do?
Thanks for the help!! :)
One the while block is not correct you have to use :
System.out.println("It's not correct..Try Again, " + name + ".");
do {
if (age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
if (age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25) {
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25) {
System.out.println("You can do anything, "
+ name + ".");
}
//while(age > 300);<<----------wrong position
System.out.println("It's not correct..Try Again, " + name + ".");
} while (age > 300);//<<----------correct position
Two
The while condition seams not correct, you have to check with min and max age, instead of just max, so you can use :
while (age < minAge || age > maxAge );
So if the age is less then the min OR great then the max repeat again
Three this if will not ever executed :
if (age < 18);//<<----------------note the ; it mean your if is end
System.out.println("You can't visit in the park, " + name + ".");
Four
Instead you can read the age inside your loop :
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
do {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
....
}while(..);
Hope this can gives you an idea about your problem.
1)You should use this condition : if (age >= 25 && age <= 300){ instead of
if (age >= 25){.
Otherwise the condition is true even if age is over 300 and you don't want to:
while(age > 300);
2)You should also allow the user to enter a new input if the value is not correct.
age = keyboard.nextInt(); should be in the loop.
3) You should allow the loop to finish.
In the while statement, you could specify the condition that requires a new input from the user :
while (age > 300)
This gives the code (not tested) :
int age;
do{
age = keyboard.nextInt();
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
}
else if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
else if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25 && age <= 300){
System.out.println("You can do anything, " +
name + ".");
}
else{
System.out.println("It's not correct..Try Again, " + name + ".");
}
}
while (age > 300);
Try this, please:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
int age;
System.out.print("Hey, what's your name? ");
String name = keyboard.next();
System.out.println();
while(true) {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
if (age <= 300) {
break;
}
System.out.println("It's not correct..Try Again, " + name +
".");
}
if(age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
}
if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
}
}

Java application output data are missing something

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);
}
}

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;
}

Categories