Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you help me fix my code? and Explain what went wrong. Attempting to do a do while loop for sentinel input. The loop for

Could you help me fix my code? and Explain what went wrong. Attempting to do a do while loop for sentinel input. The loop for some reason does not work and would not repeat to ask the user to enter another integer. Please help.

/***************************************************

Program name: SentinelWhileLoop

Description: Uses sentinel input to calculate sum of entered intergers

Class: CS101 sec. 5

Name: Bryan Aman

Date: 09/25/18

****************************************************/

import java.util.Scanner;

public class SentinelWhileLoop {

public static void main(String[] args) {

//Create Scanner

Scanner input = new Scanner(System.in);

//Set up Variables for use

int MAX_VALUE = 0 ;

int MIN_VALUE = 0 ;

int sum = 0 ;

int data;

//Set up do while loop

do {

//Read in input from user

System.out.print(" Enter an integer: (Enter 0(Zero) to End) ") ;

data = input.nextInt() ;

//Replace Max and Min values if needed

if (data > MAX_VALUE) {

data = MAX_VALUE ;

}

else if (data < MIN_VALUE) {

data = MIN_VALUE ;

}

//Add to sum of data

sum += data;

} while (data != 0);

// close scanner

input.close() ;

//print out neccesary information

System.out.println(" Sum = " + sum) ;

System.out.println(" Largest Number =" + MAX_VALUE) ;

System.out.println(" Smallest Number =" + MIN_VALUE) ;

// find if sum is even or not and tell user

if (sum % 2 == 0) {

System.out.println("The sum is even") ;

}

else {

System.out.println("The sum is off") ;

}

}

}

Desired Output with Example input:

Example output for the following inputs: 2, 20, 2, 0

Sum = 24

Largest number = 20

Smallest number = 2

The sum is even

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

ISBN: 1285427106, 978-1285427102

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago