带有构造函数、修改器和访问器的 PiggyBank 程序
PiggyBank program with constructors, mutators, and accessors
有两件事我需要帮助。一个是输出中的舍入问题,另一个是在必要时找到一种更好的方法来编写输出相同结果的程序。
编写此程序最有效的方法是什么?尽管它能正常工作,但我知道它的设计并不是最好的。
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 [=11=].15 in it
Have entered 4 pennies
Bank has [=11=].19000001 in it
Have entered -18 pennies
No Pennies Added
Bank has [=11=].19000001 in it
Have entered 2 dimes
Bank has [=11=].39000002 in it
Have entered 3 quarters
Bank has .14 in it
Have entered -3 quarters
No Quarters Added
Bank has .14 in it
Broke the bank and got .14 from it
Bank has [=11=] in it
我确实使用了另一个站点来查找访问器和修改器的结构。我不认为我遗漏了什么太重要的东西,但我现在想不出我还需要做什么。
public final int breakTheBank()`
{
if(total >= 0)
{
total = 0;
}
return (int) total;
}
我只想将 if 语句更改为 if(total == 0)
,因为如果您要将其更改为 0,则无需检查它是否为 0。除此之外,您可以进行的更改并不多。
One is a problem with the rounding in the output
这里有一个方法,我比较喜欢用它来四舍五入小数,所以输出合计的时候直接调用就可以了。有关舍入数字的更多信息,请参见此处:
Round a double to 2 decimal places
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
find a better way to write my program that outputs the same results
如果我的任务是这样做,我会使用枚举,您可以在其中为每个硬币创建一个枚举值。如果您不确定如何使用枚举,请查看此处了解更多信息,https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
在您的案例中,您想要做的是创建一个名为 Coins 的枚举,其中它采用该硬币价值的浮点数。然后创建另一个方法,例如 addCoin,它将采用硬币枚举和要添加的数量。然后通过访问枚举中硬币的价值并将其乘以添加的数量来简单地计算结果。
find a better way to write my program that outputs the same results
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 class。这样您就可以将数字格式化为您的默认货币(在本例中为美元)。您也不需要将 float 强制转换为 total 变量,因为您将其声明为 float 实例变量。
bank.addPennies(4);
System.out.println("Bank has" + formatter.format(bank.getContents()) + " in it \n");
您可以像这样使用格式化程序来打印格式化的 $#.##,这样您就可以避免显式添加美元符号。
有两件事我需要帮助。一个是输出中的舍入问题,另一个是在必要时找到一种更好的方法来编写输出相同结果的程序。
编写此程序最有效的方法是什么?尽管它能正常工作,但我知道它的设计并不是最好的。
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 [=11=].15 in it
Have entered 4 pennies
Bank has [=11=].19000001 in it
Have entered -18 pennies
No Pennies Added
Bank has [=11=].19000001 in it
Have entered 2 dimes
Bank has [=11=].39000002 in it
Have entered 3 quarters
Bank has .14 in it
Have entered -3 quarters
No Quarters Added
Bank has .14 in it
Broke the bank and got .14 from it
Bank has [=11=] in it
我确实使用了另一个站点来查找访问器和修改器的结构。我不认为我遗漏了什么太重要的东西,但我现在想不出我还需要做什么。
public final int breakTheBank()`
{
if(total >= 0)
{
total = 0;
}
return (int) total;
}
我只想将 if 语句更改为 if(total == 0)
,因为如果您要将其更改为 0,则无需检查它是否为 0。除此之外,您可以进行的更改并不多。
One is a problem with the rounding in the output
这里有一个方法,我比较喜欢用它来四舍五入小数,所以输出合计的时候直接调用就可以了。有关舍入数字的更多信息,请参见此处: Round a double to 2 decimal places
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
find a better way to write my program that outputs the same results
如果我的任务是这样做,我会使用枚举,您可以在其中为每个硬币创建一个枚举值。如果您不确定如何使用枚举,请查看此处了解更多信息,https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
在您的案例中,您想要做的是创建一个名为 Coins 的枚举,其中它采用该硬币价值的浮点数。然后创建另一个方法,例如 addCoin,它将采用硬币枚举和要添加的数量。然后通过访问枚举中硬币的价值并将其乘以添加的数量来简单地计算结果。
find a better way to write my program that outputs the same results
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 class。这样您就可以将数字格式化为您的默认货币(在本例中为美元)。您也不需要将 float 强制转换为 total 变量,因为您将其声明为 float 实例变量。
bank.addPennies(4);
System.out.println("Bank has" + formatter.format(bank.getContents()) + " in it \n");
您可以像这样使用格式化程序来打印格式化的 $#.##,这样您就可以避免显式添加美元符号。