Why is it after I compile the program sucessfully, the options will not work? Only one out of my four case statements actually execute.

hi

may be you have used your statements in that way to only print the last statement e.g. the first three are false and last becomes true.

Take out the break statements if you want all them to execute, but that probably means your logic is flawed.

I tried to correct the program and am still having the same problem. Can you please take a look to see if anything jumps out at you. Thanks. Here is the program:

import java.util.*;


public class GraduationMoney


{
public static void main (String[] args) // start main method
{
// class method to call gift class


Gift mygifts = new Gift ();


// set up input stream
Scanner stdin1 = new Scanner (System. in);


// define varaibles
int UnitOfCurrency = 0;
double EnteredAmount = 0;
double answer = 0;
int currency = -1;


// Description of Program
System.out.print ("This program will convert dollars, euros, and yen");
System.out.println (" to dollars.");
System.out.println ();



// Users options for selection
System.out.println ("Press 1 to convert USDollars to dollars.");
System.out.println ("Press 2 to convert Euros to dollars.");
System.out.println ("Press 3 to convert Yen to dollars.");
System.out.println ("Press 4 to exit the program.");
System.out.println ("Please make a selection");
System.out.println ( );
UnitOfCurrency = stdin1.nextInt();


while (UnitOfCurrency != 4) // set up while statement


{


switch (currency) //use a switch statement to switch currency
{


case 1:
mygifts.USDollarsEntered();
break;


case 2:
mygifts.EurosEntered();
break;


case 3:
mygifts.YenEntered();
break;


case 4: // total collected
mygifts.TotalSavings();
break;



}


}
}
}


Gift class


import java.util.*;



public class Gift
{


Scanner stdin = new Scanner (System.in);


// define variables
double EnteredDollars, EnteredEuros, EnteredYen, TotalSaving = 0;
double ConvertedDollars, ConvertedEuros, ConvertedYen = 0;
double total = 0;


// define constants
final double EURO_CONVERSION_RATE = 1.24 ;
final double YEN_CONVERSION_RATE =  0.0092 ;
final double DOLLARS_CONVERSION_RATE = 1.0 ;


//method to convert Dollars to Dollars
public void USDollarsEntered ()
{
System.out.println ("Please enter the number of Dollars.") ;
EnteredDollars = stdin.nextDouble() ; // read in the user input
ConvertedDollars = EnteredDollars * DOLLARS_CONVERSION_RATE ; // Dollar to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredDollars + " dollars equals " + ConvertedDollars + " dollars.") ;
total += ConvertedDollars;


}


//method to convert Euros to Dollars
public void EurosEntered ()
{
System.out.println ("Please enter the amount of Euros.") ;
EnteredEuros = stdin.nextDouble() ; // read in the user input
ConvertedEuros = EnteredEuros * EURO_CONVERSION_RATE ; // Euro to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredEuros + " euros equals " + ConvertedEuros + " dollars.") ;
total += ConvertedEuros;


}


//method to convert Yen to Dollars
public void YenEntered ()
{
System.out.println ("Please enter the number of Yen.") ;
EnteredYen = stdin.nextDouble() ; // read in the user input
ConvertedYen = EnteredYen * YEN_CONVERSION_RATE ; // Yen to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredYen + " yen equals " + ConvertedYen + " dollars.") ;
total += ConvertedYen;


}


//method to calculate Total Savings
public void TotalSavings()
{
TotalSaving = total;
System.out.println ( ) ;
System.out.println ("You have choosen to quit the program.") ;
System.out.println ("Your total amount of savings is " + TotalSaving) ;


}


}

I actually figured it out. I needed the stdin in the while loop. Simple. Thanks everyone for your help. I really appreciate it more than you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.