我需要帮助两件事。一个是输出中的舍入问题,另一个是找到一个更好的方法来编写我的程序,如果有必要,输出相同的结果。
编写此程序的最有效方法是什么?尽管它的工作方式应该如此,但我知道它的设计并不是最好的。
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
我确实使用了另一个站点来查找访问器和mutator的结构。我不认为我错过任何太重要的东西,但我想不出我现在还需要做什么。
答案 0 :(得分:0)
public final int breakTheBank()`
{
if(total >= 0)
{
total = 0;
}
return (int) total;
}
我只是将if语句更改为if(total == 0)
,因为如果您要将其更改为0,则不需要检查它是否为0。除此之外,你可以做出很多改变。
答案 1 :(得分:0)
一个是输出中的舍入问题
这是一种在舍入十进制数时我倾向于使用的方法,因此当您输出总数时,只需调用它。有关舍入数字的更多信息,请点击此处: Round a double to 2 decimal places
chrome.tabs.executeScript
找到一种更好的方法来编写输出相同结果的程序
如果我的任务是执行此操作,我会使用枚举,您可以在其中为每个硬币创建枚举值。如果您不确定如何使用枚举,请查看此处以获取更多信息,https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
在你的情况下,你想要做的是创建一个名为Coins的枚举,其中浮点数是该硬币的值。然后创建另一个方法,例如addCoin,其中将采用硬币枚举和要添加的金额。然后通过访问枚举中的硬币值并将其乘以添加的数量来简单地计算结果。
答案 2 :(得分:0)
找到一种更好的方法来编写输出相同结果的程序
import java.util.*;
import java.text;
class PiggyBank
{
Scanner console = new Scanner(System.in);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
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 = 0.00;
}
我会在Text包中使用NumberFormat类。这样您就可以将数字格式化为默认货币(在这种情况下为美元)。您也不需要将float转换为总变量,因为您将它声明为float实例变量。
bank.addPennies(4);
System.out.println("Bank has" + formatter.format(bank.getContents()) + " in it \n");
您使用格式化程序来打印格式化的$#。##,这样可以避免显式添加美元符号。