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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18583106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:14:19  来源:igfitidea点击:

Java - Assigning User Input to Variable / Change Counter

javauser-input

提问by user2665348

I'm quite new to java, although I have a fairly basic knowledge of C++. For my assignment I am counting change and sorting it into American currency (i.e., if you had 105 cents, it would divide it into one dollar and one dime).
Logically I understand how to do this, but I'm having some serious trouble understanding the java syntax. I'm having serious trouble to find a way to assign a user-inputted value to a variable of my creation. In C++ you would simply use cin, but Java seems to be a lot more complicated in this regard.

尽管我对 C++ 有相当基本的了解,但我对 Java 还是很陌生。对于我的作业,我计算零钱并将其分类为美国货币(即,如果您有 105 美分,它将分为一美元和一角硬币)。
从逻辑上讲,我明白如何做到这一点,但我在理解 java 语法时遇到了一些严重的问题。我很难找到一种方法来将用户输入的值分配给我创建的变量。在 C++ 中,您可以简单地使用 cin,但 Java 在这方面似乎要复杂得多。

Here is my code so far:

到目前为止,这是我的代码:

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);

    }

}

I would really appreciate some help with how to assign a user-input to my variable, Money. However, if you see another error in the code, feel free to point it out.

我非常感谢有关如何将用户输入分配给我的变量 Money 的帮助。但是,如果您在代码中看到另一个错误,请随时指出。

I know this is really basic stuff, so I appreciate all of your cooperation.

我知道这是非常基本的东西,所以我感谢你们的合作。

回答by Charaf JRA

Change this line :

改变这一行:

Scanner input new Scanner(System.in);

To :

到 :

Scanner input = new Scanner(System.in);

And this should be after line below not before:

这应该在下面的行之后而不是之前:

System.out.println("Enter the amount of money in cents.");

And as you did , the line below will read from input intvalue and assign it to your variable money :

正如您所做的那样,下面的行将从输入int值中读取并将其分配给您的变量 money :

int money = input.nextInt();