Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Define affordance in the context of HCI. ( 2 points )

Answered: 1 week ago

Question

2. What should an employer do when facing an OSHA inspection?

Answered: 1 week ago