gpt4 book ai didi

java - 无法获得输入 .11 的正确输出

转载 作者:行者123 更新时间:2023-12-01 23:09:39 25 4
gpt4 key购买 nike

我在学校有一项作业,我必须显示用户输入的小于 1.00 但大于 0 的金额的正确更改。除了带有 1 的两位数以外,每个金额都有效。或第十位上的 6。例如 .11、.16、.21、.26 等。

这是我的代码

import java.util.Scanner;

public class AmountChange
{
public static void main(String[] args)
{
//
double amt;
int cents, quarter, dime, nickle, penny;

Scanner keyboard = new Scanner(System.in);

//To get the users input
System.out.println("Change in Coins");
System.out.println("---------------");
System.out.println("Enter the amount less than $1.00, " +
"\nbut more than zero.");
System.out.print("\nEnter amount: ");
amt = keyboard.nextDouble();

//Loop for incorrect input
while ( amt < 0 || amt > 1.00 )
{
System.out.println("Please enter the amount less than $1.00,"
+ "\nbut more than zero.");
System.out.print("\nRe-enter amount: ");
amt = keyboard.nextDouble();
}

//
cents = (int)( amt * 100 + .1 );

quarter = cents/25;
cents %= 25;

dime = cents/10;
cents %= 10;

nickle = cents/5;
cents %= 5;

penny = cents;

// ----------------------------------------------------------
if (quarter > 1)
{
System.out.print("\nYou will need " + quarter + " quarters, ");
}
else if (quarter == 1)
{
System.out.print("\nYou will need " + quarter + " quarter ,");
}
else
{
System.out.print("\nYou will need no quarters, ");
}

// ----------------------------------------------------------
if (dime > 1)
{
System.out.print(dime + " dimes, ");
}
else if (dime == 1)
{
System.out.print(dime + " dime, ");
}
else
{
System.out.print("no dimes, ");
}

// ----------------------------------------------------------
if (nickle > 1)
{
System.out.print(nickle + " nickles, ");
}
else if (nickle == 1)
{
System.out.print(nickle + " nickle, ");
}
else
{
System.out.print("no nickles, ");
}

// ----------------------------------------------------------
if (penny > 1)
{
System.out.print("and " + penny + " pennies.");
}
else if (quarter == 1)
{
System.out.print("and " + penny + " penny.");
}
else
{
System.out.print("and no pennies.");
}
}
}

最佳答案

啊,剪切和粘贴的乐趣:-)

if (penny > 1)
{
System.out.print("and " + penny + " pennies.");
}
else if (quarter == 1) // <<<<< LOOK HERE !!!
{
System.out.print("and " + penny + " penny.");
}
else
{
System.out.print("and no pennies.");
}

那应该是便士,而不是四分之一

而且,事实上,它实际上确实适用于.26(尽管你有这样的断言),因为quarter设置为1 ,与便士相同。事实上,它适用于任何硬币数量等于便士数量的值(.26.52.78) ,但只是偶然。

<小时/>

顺便说一句,您可能需要考虑的另一件事是使用以下内容重构所有重复的代码:

import java.util.Scanner;

public class Test
{
static double getAmount(Scanner keyboard) {
System.out.println("Enter the amount between zero and $1.00.");
System.out.print("\nEnter amount: ");
return keyboard.nextDouble();
}

static String mkeTxt (int val, String prefix, String singular, String plural) {
if (val == 0)
return prefix + "no " + plural;
if (val == 1)
return prefix + "1 " + singular;
return prefix + val + " " + plural;
}

public static void main(String[] args)
{
double amt;
int cents, quarter, dime, nickle, penny;

Scanner keyboard = new Scanner(System.in);

System.out.println("Change in Coins");
System.out.println("---------------");
amt = getAmount(keyboard);
while ( amt < 0 || amt > 1.00 )
amt = getAmount(keyboard);

cents = (int)( amt * 100 + .1 );
quarter = cents/25;
cents %= 25;
dime = cents/10;
cents %= 10;
nickle = cents/5;
cents %= 5;
penny = cents;

System.out.print("\nYou will need ");
System.out.print(mkeTxt(quarter,"", "quarter", "quarters"));
System.out.print(mkeTxt(dime,", ", "dime", "dimes"));
System.out.print(mkeTxt(nickle,", ", "nickle", "nickles"));
System.out.print(mkeTxt(penny," and ", "penny", "pennies"));
System.out.println(".");
}
}

使用函数来输出提示并接受输入使用户输入代码更容易维护,因为您只需要在一处更改交互。

真正的保护者是 mkTxt() 函数,它为您提供一个可以自动根据硬币数量进行调整的字符串。它摆脱了 main() 中大量的 if/then/else block ,在一定程度上提高了可读性。

如果您发现自己多次执行类似的操作但具有不同的值,那么肯定需要将其更改为某种描述的函数或循环。

关于java - 无法获得输入 .11 的正确输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215873/

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