0

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);
    } 
}
4
  • 2
    =- is not an operator: it’s just = and then -. Commented Dec 22, 2018 at 4:34
  • 1
    As for every question and bug report: what is the behavior you want? Commented Dec 22, 2018 at 4:36
  • 1
    I don’t understand, sorry. I know the difference between two numbers. The difference between 2 and 5 is 3, What is the difference between 11, 3 and 5? Commented Dec 22, 2018 at 4:51
  • 1
    It seems to me that your program is working correctly with -=. When I enter three numbers, 11, 3 and 5, it prints The difference of those numbers is -19. All the numbers have been subtracted from the initial value of 0. Commented Dec 22, 2018 at 4:56

4 Answers 4

1

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);
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.