简体   繁体   English

Java编程:如何使接受用户输入的for循环程序正确减去输入?

[英]Java programming: How do I make this for loop program that accepts user input subtract the input properly?

I am making a program that accepts user input to subtract all the numbers desired by the user 我正在制作一个接受用户输入以减去用户所需的所有数字的程序

I first made the program ask how many numbers the user wants to subtract, and initialized the value in int inputNum , which is then passed on to the for loop for (int Count=1; Count<=inputNum; Count++) , so that the program loops for user input, based on the inputNum . 我首先让程序询问用户要减去多少个数字,然后初始化int inputNum中的值,然后将其传递给for循环, 用于(int Count = 1; Count <= inputNum; Count ++) ,这样程序循环基于inputNum进行用户输入。

Unfortunately, the output is wrong. 不幸的是,输出错误。 I can't understand how this would work properly. 我不明白这将如何正常工作。

I've tried switching the operator in difference by making difference =- toBeSubtracted; 我试图通过使差=-toBeSubtracted来切换差值中的运算符 into difference -= toBeSubtracted; 差-= toBeSubtracted;

For difference =- toBeSubtracted; 对于差=-要减去; , here is a sample output ,这是示例输出

run:
How many numbers do you want to subtract? 
2
Input numbers you want to subtract: 
10
5
The difference of those numbers is -5

For difference -= toBeSubtracted; 对于差异-= toBeSubtracted; , here is a sample output ,这是示例输出

run:
How many numbers do you want to subtract? 
2
Input numbers you want to subtract: 
10
5
The difference of those numbers is -15

Here is the code: 这是代码:

import java.util.*;
public class ForLoops_Difference 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("How many numbers do you want to subtract? ");
        int inputNum = scan.nextInt();
        int difference = 0;

        System.out.println("Input numbers you want to subtract: ");
        for (int Count = 1 ;Count<=inputNum; Count++)
        {
            int toBeSubtracted = scan.nextInt();
            difference =- toBeSubtracted;
        }
        System.out.println("The difference of those numbers is " + difference);
    } 
}

Ok this might help you out: 好的,这可能会帮助您:

difference = 0

and than you have: 比你有:

difference -= toBesubtracted

so what you are doing is: 所以您正在做的是:

difference = difference - toBeSubtracted

which in terms is 这是

difference = 0 - 10
difference = -10 - 5

thus you get -15 这样你得到-15

and where you have 以及你在哪里

difference =- toBeSubtracted

it is the same as 它与

difference = -1 * toBeSubtracted

thus you get -5 这样你得到-5

I suppose you want output of 5. Here is your code with one change 我想您希望输出5。这是您的代码,只有一个更改

import java.util.*;
public class ForLoops_Difference 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("How many numbers do you want to subtract? ");
        int inputNum = scan.nextInt();
        int difference = scan.nextInt(); // so read in the first number here.

        System.out.println("Input numbers you want to subtract: ");
        for (int Count = 1;Count<inputNum; Count++) // go till from 1 to inputNum - 1 because you have already got one number above
        {
            int toBeSubtracted = scan.nextInt();
            difference -= toBeSubtracted;
        }
        System.out.println("The difference of those numbers is " + difference);
    } 
}

I did this 我做了这个

import java.util.*;
public class ForLoops_Difference 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("How many numbers do you want to subtract? ");

        int inputNum = scan.nextInt();
        int difference = 0;   
        int currentNumber = 0; //current number from scanner
        System.out.println("Input numbers you want to subtract: ");
        for (int Count = 1 ;Count<=inputNum; Count++)
        {   
            if(Count == 1)
            {
                //nothing to subtract if count is 1
                currentNumber = scan.nextInt();
                difference =  currentNumber;
            }
            else {
                currentNumber = scan.nextInt();
                difference = difference - currentNumber;    
            }     
       }
       System.out.println("The difference of those numbers is " + difference);
    }
}

You started your difference at 0. So if you subtracted two numbers, 15 and 3, then you would get 0 - 15 - 3 = -18. 您从0开始求差。因此,如果您减去两个数字15和3,那么您将得到0-15-3 = -18。 I set the difference equal to the first number, 15 in the first loop. 我将差设置为等于第一个数字,在第一个循环中为15。 Then it should work correctly because you do 15 - 3 = 12. 然后它应该正确工作,因为您执行15-3 = 12。

You need to understand the operator shorthand notation. 您需要了解运算符的简写形式。 You should write -= for minus shorthand. 您应为负速记写-=。 The shorthand is equal to difference =difference - tobesubstracted. 速记等于差=差-要减去。 Since your initial value is 0 it becomes 0-10-5= -15. 由于您的初始值为0,因此它变为0-10-5 = -15。

Assign the first value as difference and then do the substraction of next values. 将第一个值指定为差,然后减去下一个值。

So something like: 所以像这样:

  difference = scanner.nextInt();

And then do the loop for rest of the values to minus from initial value. 然后进行循环以将其余的值减去初始值。

The problem isn't that your program is working incorrectly. 问题不在于您的程序运行不正常。

The problem is that the requirements are nonsense. 问题在于这些要求是胡说八道。 You can have the difference between two numbers. 您可以在两个数字之间求差。 The difference between 19 and 8 is 11. There is no such thing as the difference between 3 or more numbers. 19和8之间的差是11。3个或更多数字之间没有差。 Therefore no program could ever produce that. 因此,没有程序可以产生这种结果。

That said, Davis Herring is correct in the comment: there is no =- operator. 就是说,戴维斯·赫林(Davis Herring)在注释中是正确的:没有=-运算符。 You tried to use one in the line: 您尝试在该行中使用一个:

           difference =- toBeSubtracted;

But the line is understood as just: 但是该行被理解为:

           difference = -toBeSubtracted;

So your program just outputs the negative of the last entered number. 因此,您的程序仅输出最后输入的数字的负数。 I tried entering three numbers, 11, 3 and 5. The first time through the loop difference is set to -11. 我尝试输入三个数字11、3和5。第一次将循环差设置为-11。 Next time this value is overwritten and -3 is set instead. 下次覆盖该值时,改为设置-3。 In the final iteration the difference is set to -5, which “wins” and is output. 在最后的迭代中,差异设置为-5,即“获胜”并输出。

Instead I suggest that your program should always subtract 2 numbers, as you are also trying in your example. 相反,我建议您的程序应始终减去2个数字,因为您也在示例中进行了尝试。 So the user needs not enter the number of numbers, but is just told to enter the two numbers. 因此,用户无需输入数字的数量,而只是被告知输入两个数字。 Then you also don't need any loop. 然后,您也不需要任何循环。 Just read the first number, read the second number, subtract the second from the first (or the first from the second, or the smaller from the larger, what you want) and print the result. 只需读取第一个数字,读取第二个数字,从第一个数字中减去第二个数字(或从第二个数字中减去第一个数字,或从较大的数字中减去较小的数字)并打印结果。 I am leaving the coding to you to avoid spoiling it. 我将编码留给您,以免破坏代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设计一个循环,接受用户输入的高尔夫球手姓名及其得分,然后显示列表? - how do I design a loop that accepts user input for a golfer name and their score then displays list? 如何使Java程序从上一个循环中获取结果并将其输入到同一过程中? - How do I make a java program take the results from a previous loop and input it in the same process? 如何循环,询问用户输入这个 Java 程序? - how to loop, to ask user input for this Java program? 如何在Java中以用户输入结束程序? - How do I end program with user input in java? 我如何循环直到用户在 java 上输入正确的输入? - How do I loop until the user put the correct input on java? 在用户在Java上进行正确输入之前,如何循环播放? - How do I loop until the user makes correct input on Java? Java:如何确保或使用户输入字符串中的逗号? - Java: How do I ensure, or make a user, input a comma in a string? 如何实现循环以使程序再次要求用户输入而不是结束程序 - How do I emplement a loop to get the program to ask the user for input again instead of ending the program 当程序位于for循环中时,如何使程序等待用户输入? - How to make the program wait for user input when it's inside a for loop? 如何让用户输入双倍? - how do i make the user input a double?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM