虽然我对C++有相当基本的了解,但我对java还是很陌生的。在我的任务中,我计算零钱并将其分类为美元(即,如果你有105美分,它会将其分为一美元和一角硬币)
从逻辑上讲,我理解如何做到这一点,但在理解java语法时遇到了一些严重的问题。我很难找到一种方法将用户输入的值分配给我创建的变量。在C++中,您只需使用cin,但Java在这方面似乎要复杂得多。
这是我到目前为止的代码:
package coinCounter;
import KeyboardPackage.Keyboard;
import java.util.Scanner;
public class helloworld
{
public static void main(String[] args)
{
Scanner input new Scanner(System.in);
//entire value of money, to be split into dollars, quarters, etc.
int money = input.nextInt();
int dollars = 0, quarters = 0, dimes = 0, nickels = 0;
//asks for the amount of money
System.out.println("Enter the amount of money in cents.");
//checking for dollars, and leaving the change
if(money >= 100)
{
dollars = money / 100;
money = money % 100;
}
//taking the remainder, and sorting it into dimes, nickels, and pennies
else if(money > 0)
{
quarters = money / 25;
money = money % 25;
dimes = money / 10;
money = money % 10;
nickels = money / 5;
money = money % 5;
}
//result
System.out.println("Dollars: " + dollars + ", Quarters: " + quarters + ", Dimes: " + dimes + ", Nickels: " + nickels + ", Pennies: " + money);
}
}
我真的很感激关于如何将用户输入分配给我的变量Money的帮助。但是,如果您在代码中看到另一个错误,请随时指出。
我知道这是非常基本的东西,所以我感谢你们的合作。
更改此行:
Scanner input new Scanner(System.in);
收件人:
Scanner input = new Scanner(System.in);
这应该在下面的行之后而不是之前:
System.out.println("Enter the amount of money in cents.");
正如您所做的,下面的行将读取输入的int
值,并将其分配给您的可变货币:
int money = input.nextInt();