简体   繁体   中英

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 .

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; into difference -= 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; , 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

and where you have

difference =- toBeSubtracted

it is the same as

difference = -1 * toBeSubtracted

thus you get -5

I suppose you want output of 5. Here is your code with one change

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. I set the difference equal to the first number, 15 in the first loop. Then it should work correctly because you do 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.

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. Therefore no program could ever produce that.

That said, Davis Herring is correct in the comment: there is no =- operator. 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. Next time this value is overwritten and -3 is set instead. In the final iteration the difference is set to -5, which “wins” and is output.

Instead I suggest that your program should always subtract 2 numbers, as you are also trying in your example. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM