I am writing a program that takes user input, converts euros or yen to u.s. dollars and should output the total dollars collected when the user chooses to exit the program. My program is converting everything okay. However, when I choose to exit the program and get the total it is only printing out the last value that was entered and not the total collected. Can someone give me a clue about what I am doing wrong???

import java.util.*;
import java.io.*;


class Gifts {
double dollars;
double euros;
double yen;
double dollarsRate;
double eurosRate;
double yenRate;
double dollarInDollars;
double eurosInDollars;
double yenInDollars;


double dollarInDollars(){
return dollars * dollarsRate;
}
double eurosInDollars(){
return euros *eurosRate;
}


double yenInDollars(){
return yen * yenRate;
}
}



public class GiftsDemo {


//application entry point
public static void main(String[] args) {


//Define  constants
int Input = 0;
double Amount = 0.0;
double totalCollected = 0.0;


//display out
System.out.println("Gifts Program");
System.out.println("");


while (Input != 4) {


System.out.println("Gifts Types:");
System.out.println("1. Enter the number of U.S. Dollars");
System.out.println("2. Enter the number of euros");
System.out.println("3. Enter the number of yen");
System.out.println("4. Exit the program and get total");
System.out.println("");
System.out.print("Please select an option from 1-4 ");


//set input stream
Scanner in = new Scanner(System.in);
Input = in.nextInt();


while ((Input < 1) || (Input > 4))
{
System.out.println(" You have entered an invalid selection, please try again");
System.out.print("Please select an option from 1-4 ");
in = new Scanner(System.in);
Input = in.nextInt();
}


if (Input == 4){
System.out.println("Total collected: " + totalCollected + "dollars\n");
}
else{
System.out.print("What is the amount to be converted ");
Amount = in.nextDouble();


if (Input == 1)
{
totalCollected = (Amount * 1);
System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
else if (Input == 2)
{
totalCollected = (Amount * 1.24);
System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
else if (Input == 3)
{
totalCollected = (Amount * 0.0092);
System.out.println("Conversion Complete: " + totalCollected + " dollars\n");


}
}
}
}
}

Recommended Answers

All 2 Replies

import java.util.*;
import java.io.*; 

class Gifts {
    double dollars;
    double euros;
    double yen;
    double dollarsRate;
    double eurosRate;
	 double yenRate;
    double dollarInDollars;
    double eurosInDollars;
    double yenInDollars;

double dollarInDollars(){
   return dollars * dollarsRate;
   }
double eurosInDollars(){
   return euros *eurosRate;
   }
	
double yenInDollars(){
   return yen * yenRate;
   }
}
   

public class GiftsDemo {

   //application entry point
   public static void main(String[] args) {

  //Define  constants
  int Input = 0;
  double Amount = 0.0;
  double totalCollected = 0.0;

  //display out
  System.out.println("Gifts Program");
  System.out.println("");

  while (Input != 4) {

  System.out.println("Gifts Types:");
  System.out.println("1. Enter the number of U.S. Dollars");
  System.out.println("2. Enter the number of euros");
  System.out.println("3. Enter the number of yen");
  System.out.println("4. Exit the program and get total");
  System.out.println("");
  System.out.print("Please select an option from 1-4 ");

  //set input stream
  Scanner in = new Scanner(System.in);
  Input = in.nextInt();

  while ((Input < 1) || (Input > 4))
{
   System.out.println(" You have entered an invalid selection, please try again");
   System.out.print("Please select an option from 1-4 ");
   in = new Scanner(System.in);
   Input = in.nextInt();
}

   if (Input == 4){
      System.out.println("Total collected: " + totalCollected + "dollars\n");
}
   else{
      System.out.print("What is the amount to be converted ");
      Amount = in.nextDouble();

   if (Input == 1)
{
      totalCollected [U]+=[/U] (Amount * 1);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
   else if (Input == 2)
{
      totalCollected [U]+=[/U] (Amount * 1.24);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
   else if (Input == 3)
{
      totalCollected [U]+=[/U] (Amount * 0.0092);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");

}
}
}
}
}

Note the corrections underlined. It's just a small little mistake, but you're doing fine! :cheesy:

import java.util.*;
import java.io.*; 

class Gifts {
    double dollars;
    double euros;
    double yen;
    double dollarsRate;
    double eurosRate;
	 double yenRate;
    double dollarInDollars;
    double eurosInDollars;
    double yenInDollars;

double dollarInDollars(){
   return dollars * dollarsRate;
   }
double eurosInDollars(){
   return euros *eurosRate;
   }
	
double yenInDollars(){
   return yen * yenRate;
   }
}
   

public class GiftsDemo {

   //application entry point
   public static void main(String[] args) {

  //Define  constants
  int Input = 0;
  double Amount = 0.0;
  double totalCollected = 0.0;

  //display out
  System.out.println("Gifts Program");
  System.out.println("");

  while (Input != 4) {

  System.out.println("Gifts Types:");
  System.out.println("1. Enter the number of U.S. Dollars");
  System.out.println("2. Enter the number of euros");
  System.out.println("3. Enter the number of yen");
  System.out.println("4. Exit the program and get total");
  System.out.println("");
  System.out.print("Please select an option from 1-4 ");

  //set input stream
  Scanner in = new Scanner(System.in);
  Input = in.nextInt();

  while ((Input < 1) || (Input > 4))
{
   System.out.println(" You have entered an invalid selection, please try again");
   System.out.print("Please select an option from 1-4 ");
   in = new Scanner(System.in);
   Input = in.nextInt();
}

   if (Input == 4){
      System.out.println("Total collected: " + totalCollected + "dollars\n");
}
   else{
      System.out.print("What is the amount to be converted ");
      Amount = in.nextDouble();

   if (Input == 1)
{
      totalCollected [U]+=[/U] (Amount * 1);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
   else if (Input == 2)
{
      totalCollected [U]+=[/U] (Amount * 1.24);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");
}
   else if (Input == 3)
{
      totalCollected [U]+=[/U] (Amount * 0.0092);
      System.out.println("Conversion Complete: " + totalCollected + " dollars\n");

}
}
}
}
}

Note the corrections underlined. It's just a small little mistake, but you're doing fine! :cheesy:

It is still not giving me the total of all the amounts added when I choose option 4.

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.