2014-09-04 105 views
2

在輸出時遇到問題。只要最後一位數字不是零,它就會以正確的格式打印。換句話說,它會輸出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

的重複更多鈔票[的String.format()來格式化的java雙(http://stackoverflow.com/questions/4885254/string-format -to-格式雙合的java])。請檢查這個鏈接。 – JosEduSol 2014-09-04 13:59:19

回答

3

本應該做的伎倆:

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

非常感謝!你太棒了! – Dustin 2014-09-04 14:05:50

+1

@Dustin您可以將此答案標記爲「已接受」。 – JosEduSol 2014-09-04 14:21:00