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

时间:2018-12-22 04:31:20

标签: java for-loop user-input subtraction

我正在制作一个程序,接受用户输入以减去用户所需的所有数字

我首先让程序询问用户要减去多少个数字,然后初始化 int inputNum 中的值,然后将其传递给for循环 for(int Count = 1; Count <= inputNum; Count ++),以便程序根据 inputNum 循环供用户输入。

不幸的是,输出错误。我不明白这将如何正常工作。

我试图通过将 difference =-toBeSubtracted; 转换为 difference-= toBeSubtracted;

将操作符切换为 difference

对于 difference =-toBeSubtracted; ,这是示例输出

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

对于差异-= toBeSubtracted; ,这是示例输出

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

代码如下:

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

4 个答案:

答案 0 :(得分:1)

好吧,这可能会帮助您

difference = 0

比您拥有的

difference -= toBesubtracted

所以你在做什么:

difference = difference - toBeSubtracted

这是

difference = 0 - 10
difference = -10 - 5

因此您得到-15

以及您在哪里

difference =- toBeSubtracted

相同
difference = -1 * toBeSubtracted

因此您得到-5

我想您希望输出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);
    } 
}

答案 1 :(得分:1)

我做到了

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

您从0开始求差。因此,如果您减去两个数字15和3,那么您将得到0-15-3 = -18。我将差设置为等于第一个数字,在第一个循环中为15。然后它应该正确工作,因为您执行15-3 = 12。

答案 2 :(得分:1)

您需要了解运算符的缩写符号。您应为负速记写-=。速记等于差=差-要减去。由于您的初始值为0,因此它将变为0-10-5 = -15。

将第一个值分配为差,然后减去下一个值。

类似这样:

  difference = scanner.nextInt();

然后执行循环以将其余的值减去初始值。

答案 3 :(得分:1)

问题不在于您的程序运行不正常。

问题在于这些要求是胡说八道。您可以在两个数字之间求差。 19和8之间的差是11。3个或更多数字之间没有差。因此,没有程序可以产生这种结果。

也就是说,戴维斯·赫林(Davis Herring)在评论中是正确的:没有=-运算符。您尝试在该行中使用一个:

           difference =- toBeSubtracted;

但是这行被理解为:

           difference = -toBeSubtracted;

因此,您的程序只输出最后输入的数字的负数。我尝试输入三个数字11、3和5。第一次将循环差设置为-11。下次覆盖该值时,改为设置-3。在最后的迭代中,差异设置为-5,“获胜”并输出。

相反,我建议您的程序应始终减去2个数字,因为您也在示例中进行了尝试。因此,用户无需输入数字的数量,而只是被告知输入两个数字。然后,您也不需要任何循环。只需读取第一个数字,读取第二个数字,从第一个数字中减去第二个数字(或从第二个数字中减去第一个数字,或从较大的数字中减去较小的数字)并打印结果。我将代码留给您,以免破坏代码。