Question: The below is passing for all number, expect -2. If the input is 2 6 5 -2, the final value is showing as -3, when

The below is passing for all number, expect -2. If the input is 2 6 5 -2, the final value is showing as -3, when the expected value is 3. Not sure why it's continuing with the loop when -2 is entered, if my while loop begins with "while (valueIn >=0). Please help. What am I doing wrong?

import java.util.Scanner; public class ResultCalculator { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int valueIn; int finalVal; finalVal = 0; valueIn = scnr.nextInt(); while (valueIn >= 0) { if (valueIn % 2 == 0) { finalVal -= valueIn; } else { finalVal += valueIn; } valueIn = scnr.nextInt(); } System.out.println("Final value is " + finalVal); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!