Question
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
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