2014-09-04 4 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 -в-формате двойной-в-Java]). Пожалуйста, проверьте эту ссылку. – JosEduSol

ответ

3

Это должно сделать трюк:

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

Большое вам спасибо! Ты обалденный! – Dustin

+1

@Dustin Вы можете отметить этот ответ как «принятый». – JosEduSol

Смежные вопросы