Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter some number of positive integers,

ID: 3536046 • Letter: W

Question

Write a program that prompts the user to enter some number of positive integers, one at a time, and to enter a -1 to signal an end to the data. The exact number of entries is unknown, but there will be no more than 100 data values entered. Read in and save valid integers in an array, and compute and display the average of the integers in the array (remember the array may not be full).

Here is what I did:

public class Average{

import java.util*

public static void main (String[] args) {

Scanner input = new Scanner(System.in);

int[] numbers; int sum = 0;

System.out.println("Enter positive integers, one at a time, enter -1 to end:");

for (int i = 0; i < 100; i++)

{ numbers = input.nextInt(); }

for (int i = 0; i < numbers.length; i++)

{ sum = sum + numbers[i]; }

average = sum / numbers.length;

System.out.println("The average of the integers in the array is" + average);

}

}

I didn't get full credit because my instructor said I need to check for the user to enter the -1 to end because I never check that in the loop. How would I go about doing this? Thanks for your help!

Explanation / Answer

Here is the solution..do rate me high.. :) public class Average{ import java.util* public static void main (String[] args) { Scanner input = new Scanner(System.in); int[] numbers; int sum = 0; System.out.println("Enter positive integers, one at a time, enter -1 to end:"); for (int i = 0; i < 100; i++) { numbers = input.nextInt(); if(numbers[i]==-1) break; } for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote