gpt4 book ai didi

java - 格式化输出

转载 作者:行者123 更新时间:2023-11-30 08:20:49 25 4
gpt4 key购买 nike

我的输出有问题。只要最后一位不为零,它就会以正确的格式打印。换句话说,它将输出 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;
}
}

最佳答案

这应该可以解决问题:

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

关于java - 格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25667307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com