有兩件事我需要幫助。一個是在輸出中四捨五入的問題,另一個是找到一個更好的方式來編寫我的程序,如果有必要,輸出相同的結果。PiggyBank程序與構造函數,增變器和訪問器
什麼是寫這個程序最有效的方法?儘管它的工作原理應該如此,但我知道它並不是最好的。
package program2;
import java.util.*;
class PiggyBank
{
Scanner console = new Scanner(System.in);
private int numPennies, numNickles, numDimes, numQuarters;
private float total;
public PiggyBank(int pennies, int nickles, int dimes, int quarters)
{
numPennies = pennies;
numNickles = nickles;
numDimes = dimes;
numQuarters = quarters;
total = (float) 0.00;
}
public void addPennies(int pennies)
{
System.out.println("Have entered " + pennies + " pennies");
if (pennies < 0)
{
System.out.println("No Pennies Added");
}
else
{
numPennies = numPennies + pennies;
total = (float) (total + pennies * 0.01);
}
}
public void addNickles(int nickles)
{
System.out.println("Have entered " + nickles + " nickles");
if (nickles < 0)
{
System.out.println("No Nickles Added");
}
else
{
numNickles = numNickles + nickles;
total = (float) (total + nickles * 0.05);
}
System.out.println("Bank has $" + total + " in it");
System.out.println();
}
public void addDimes(int dimes)
{
System.out.println("Have entered " + dimes + " dimes");
if (dimes < 0)
{
System.out.println("No Dimes Added");
}
else
{
numDimes = numDimes + dimes;
total = (float) (total + dimes * 0.10);
}
System.out.println("Bank has $" + total + " in it");
System.out.println();
}
public void addQuarters(int quarters)
{
System.out.println("Have entered " + quarters + " quarters");
if (quarters < 0)
{
System.out.println("No Quarters Added");
}
else
{
numQuarters = numQuarters + quarters;
total = (float) (total + quarters * 0.25);
}
}
public float getContents()
{
return total;
}
public final int breakTheBank()
{
if (total >= 0)
{
total = 0;
}
return (int) total;
}
}
public class PiggyBankTester
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
System.out.print("Program By ");
String name = console.next();
System.out.println();
test();
}
public static void test()
{
PiggyBank bank = new PiggyBank(0, 0, 0, 0);
bank.addNickles(3);
bank.addPennies(4);
System.out.println("Bank has $" + bank.getContents() + " in it \n");
bank.addPennies(-18);
System.out.println("Bank has $" + bank.getContents() + " in it \n");
bank.addDimes(2);
bank.addQuarters(3);
System.out.println("Bank has $" + bank.getContents() + " in it \n");
bank.addQuarters(-3);
System.out.println("Bank has $" + bank.getContents() + " in it \n");
System.out.println("Broke the bank and got $" + bank.getContents() + " from it \nBank has $" + bank.breakTheBank() + " in it");
}
}
這是我的輸出示例。浮點數總計取得了一些結果,但我不知道如何讓它圍繞所有結果。
Program By JakeBrono46
Have entered 3 nickles
Bank has $0.15 in it
Have entered 4 pennies
Bank has $0.19000001 in it
Have entered -18 pennies
No Pennies Added
Bank has $0.19000001 in it
Have entered 2 dimes
Bank has $0.39000002 in it
Have entered 3 quarters
Bank has $1.14 in it
Have entered -3 quarters
No Quarters Added
Bank has $1.14 in it
Broke the bank and got $1.14 from it
Bank has $0 in it
我沒有使用另一個站點來查找訪問器和增變器的結構。我不認爲我錯過了太重要的東西,但我現在無法想到我還需要做什麼。
清理完代碼格式。主要是在那裏,只是錯過了一對「}」。小拼字。添加標點符號。刪除關於問題可以理解,不相關的行。 – mateuscb