** the program gets an error saying Scanner input = new Scanner(System.in); is a
ID: 3646407 • Letter: #
Question
**the program gets an error saying Scanner input = new Scanner(System.in); is already defined and can't be used again so when i remove the Scanner input = new Scanner(System.in); where its listed more then once the code works but then when it ask how many number say i enter 3 i have to enter numbers n hit enter to continue and then it will ask me for the numbers need help to fix it thanks
**
Write an application program that reads in a list of integer numbers and print out the sum of all numbers, the average, the lowest and the highest value entered. The first input number indicates how many numbers the program is going to read. For example, if the first number entered is 5, then it means five integer numbers will be expected as input. Display your result in an output dialog box.
Run/test you program with at least 5 input numbers.
import java.util.*;
class Problem
{
public static void main(String[] args)
{
int i, n, sum = 0, max, min, num;
System.out.println("Enter how many numbers");
Scanner input = new Scanner(System.in);
n = input.nextInt();
System.out.println("Enter number");
Scanner input = new Scanner(System.in);
max = input.nextInt();
min = input.nextInt();
for(i = 1; i<n; i++)
{
System.out.println("Enter number");
Scanner input = new Scanner(System.in);
num = input.nextInt();
sum += num;
if (num > max) max = num;
if (num < min) min = num;
}
int avg = sum/n;
System.out.println("Sum = "+sum);
System.out.println("Average = "+avg);
System.out.println("Maximum = "+max);
System.out.println("Minimum ="+min);
}
}
Explanation / Answer
Sorry misread question the first time, here is correct version of your program. import java.util.*; class Problem { public static void main(String[] args) { int i, n, sum = 0, max, min, num; double avg=0; System.out.println("Enter how many numbers"); Scanner input = new Scanner(System.in); n = input.nextInt(); //Scanner input = new Scanner(System.in); //here you need to record max and min System.out.println("Enter number"); num = input.nextInt(); max = num; min = num; for(i = 1; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.