格式化输出

时间:2014-09-04 13:54:03

标签: java formatting

我的输出有问题。只要最后一位数不是零,它就会以正确的格式打印。换句话说,它将输出1.75美元但是1.50美元我得到1.5美元。我知道“%。2f”应该格式化它,但我无法弄清楚将它放在我的代码中的哪个位置。谢谢!

/**
   This Class is designed to work with an application program for a Vending Machine.

*/
public class VendingMachine
{
   private double money;
   private double amountDue; 
   private double change;
   private String selection;
   /**
      Constructor for the Class.
   */
   public VendingMachine()
   {  
      money = 0.00;
      amountDue = 0.00;
      change = 0.00;
      selection = "";
   }  
   /**
      Explicit Constructor for the class that gives variables more specific values.
   */
   public VendingMachine(double money, double change, double amountDue, String selection)
   {                   
      this.money = money;
      this.amountDue = amountDue;
      this.change = change;
      this.selection = selection;
   }
   /**
      Accesssor method for the selection.
      @return the selection of the user.
   */
   public String getSelection()
   {
      return selection;
   }  
   /**
      Accessor method to get the amount of money the user puts in the machine.
      @return the amount of money.
   */    
   public double getMoney()
   {
      return money;
   }
   /**
      Accessor method for the amount due.
      @return the amount due.
   */   
   public double getAmountDue()
   {
      return amountDue;
   }
   /**
      Accessor method for getting the user's change.
      @return the user's change.
   */   
   public double getChange()
   {
      return change;
   }
   /**
      Mutator method for the selection.
      @param the selection of the user.
   */         
   public void setSelection(String selection)
   {
      this.selection = selection;
   }
   /**
      Mutator method for money.
      @param the money put into machine.
   */      
   public void setMoney(double money)
   {
     this.money = money;
   }
   /**
      Mutator method for amount due.
      @param the selection of the user.
   */   
   public double setAmountDue(String selection)
   {
      if (selection.equals("A1") || selection.equals("A2") || selection.equals("A3"))
      {
         amountDue = 1.25;
      }
      else if (selection.equals("B1") || selection.equals("B2") || selection.equals("B3"))
      {    
         amountDue = 1.00;  
      }
      else if (selection.equals("C1") || selection.equals("C2") || selection.equals("C3"))
      {
         amountDue = 1.50;
      }  
      return amountDue;
   }
   /**
      Mutator method for the change.
      @param the money put into machine.
   */   
   public void setChange(double money)
   {
      change = money - amountDue;
      this.change = change;          
   }
   /**
      Method for converting all variables into a String statement.
      @return a String of the total transaction.
   */      
   public String toString()
   {
      return ("Your selection: " + selection + "\nYour amount due: " + amountDue + "\nYour change: " + change);
   }
} 

import java.util.*;
/**
   This program runs a Vending Machine and interacts with the user.
*/
public class VendingMachineTester 
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      String input;
      String[] choices = {"A1~~SNICKERS~~~~~~~~$1.25", "A2~~MILKY WAY~~~~~~~$1.25" , "A3~~TWIX~~~~~~~~~~~~$1.25",
                          "B1~~ORIGINAL CHIPS~~$1.00", "B2~~BBQ CHIPS~~~~~~~$1.00" , "B3~~PRETZELS~~~~~~~~$1.00",
                          "C1~~COKE~~~~~~~~~~~~$1.50", "C2~~SPRITE~~~~~~~~~~$1.50", "C3~~DR. PEPPER~~~~~~$1.50",
                         };
      displayItems(choices);                   
      VendingMachine user = new VendingMachine();
      do
      {
         System.out.print("Please make your selection: ");
         input = in.nextLine();
      }
      while (!(input.equals("A1") || input.equals("A2") || input.equals("A3") ||
            (input.equals("B1") || input.equals("B2") || input.equals("B3") ||
            (input.equals("C1") || input.equals("C2") || input.equals("C3")))));
      user.setSelection(input);
      user.setAmountDue(input);
      System.out.print("Enter the amount you put in the machine: ");
      double amount = in.nextDouble();
      user.setMoney(amount);
      user.setChange(amount);

      System.out.println(user); 
      System.out.print("Thank You for your purchase!");      
   }
   /**
      A method for displaying all of the options in the machine.
      @param an array of choices
      @return an array of choices.
   */   
   public static void displayItems(String[] choices)
   {
      for (int i = 0; i < choices.length; i++)
      {
         System.out.print(choices[i]);
         System.out.println();
      }
      return;
   }   
}        

1 个答案:

答案 0 :(得分:3)

这应该可以解决问题:

public String toString() {
    return String.format( "Your selection: %s: \nYour amount due: %.2f \nYour change: %.2f",
        selection,
        amountDue,
        change);
}