Question
Intro to JAVA Programming: The code is below the problem is that if I type in -1,-2,-4,32,-55,-17,75,-6,26,20,x it gives me 20 as the lowest which
Intro to JAVA Programming: The code is below the problem is that if I type in -1,-2,-4,32,-55,-17,75,-6,26,20,x it gives me 20 as the lowest which is not true, -55 is the lowest. What is wrong what needs to be fixed so that it includes negative numbers also? thank you.
import java.util.Scanner; /** * */ public class AmazonNetIncome { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter net income or Q to quit: ");
int income = 0; int count = 0; double sum = 0; int numberCount = 0; double average = 0; int lowest = 0 ;
if(scan.hasNextInt()) { while(scan.hasNextInt()) { income = scan.nextInt(); numberCount++; lowest = income; sum = sum + income; if(income > 0) { count++; } else if (income < lowest) { lowest = income; } System.out.print("Enter net income or Q to quit: ");
} average = sum/numberCount; System.out.println(count); System.out.println(lowest); System.out.println(average);
} else { System.out.println("No values entered"); }
} }
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