Java – 将用户输入分配给变量/更改计数器

我对java很新,虽然我对C ++有相当基本的了解。 对于我的任务,我正在计算变化并将其分类为美国货币(即,如果你有105美分,则将其分为1美元和1美分)。
逻辑上我理解如何做到这一点,但我在理解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();