java programing: for the following program i need a modification that uses ( .le
ID: 3681896 • Letter: J
Question
java programing:
for the following program i need a modification that uses ( .lenght) to:
- declare array over 2 lines.
- change the code to find minimum score also.
- can be run with different numbers but no negative numbers.
import java.util.Scanner;
public class ArrayOfScores
{
/**
Reads in 5 scores and shows how much each
score differs from the highest score.
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double [] score = new double [5];
int index;
double max;
System.out.println("Enter 5 scores:");
score[0] = keyboard.nextDouble();
max = score[0];
for (index = 1; index < 5; index++)
{
score[index] = keyboard.nextDouble();
if (score[index] > max)
max = score[index];
//max is the largest of the values score[0],..., score[index].
}
System.out.println("The highest score is " + max);
System.out.println("The scores are:");
for (index = 0; index < 5; index++)
System.out.println(score[index] + " differs from max by " + (max - score[index]));
}
}
Explanation / Answer
import java.util.Scanner;
public class ArrayOfScores
{
/**
* Reads in 5 scores and shows how much each
*
* score differs from the lowest score.
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// declaring array
double[] score = null;
// reading N value
System.out.print("How many numbers you want to enter: ");
int N = keyboard.nextInt();
// instantiating array with size N
score = new double[N];
int index;
double min;
System.out.println("Enter "+N+" scores:");
score[0] = keyboard.nextDouble();
min = score[0];
// using length method
for (index = 1; index<score.length; index++)
{
score[index] = keyboard.nextDouble();
if (score[index] < min)
min = score[index];
// min is the largest of the values score[0],..., score[index].
}
System.out.println("The lowest score is " + min);
System.out.println("The scores are:");
for (index = 0; index < 5; index++)
System.out.println(score[index] + " differs from min by "
+ (score[index] - min));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.