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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started