Java question pls help

2007-08-15 6:50 am
a) Assume that a constant NO_CONTESTANTS which stores the value 5 has been declared.Using the constant NO_CONTESTANTS,declaare two arrays contestantNames and contestantScores to store the names and scores(from a scale of 0.0 to 10.0) for 5 contestants in a singing contest.

b)Write the segment of codes to prompt user to enter the name and score of the 5 contestantss ad store into arrays in (a).Assume BufferedReader stdin and String str have been declared.
更新1:

c) The contestants with the lowest score will be eliminated from the contest.Declare any additional variables needed and write the segment of code to find the lowest score among the contestants and print the name of this contestant be eliminated.

回答 (1)

2007-08-15 7:57 pm
✔ 最佳答案
a)
String[] contestantNames = new String[NO_CONTESTANTS];
double[] contestantScores = new double[NO_CONTESTANTS];

b)
//for each of the 5 contestant, get the name and score and put into the 2 arrays
for (int i=0; i<NO_CONTESTANTS; i++)
{
System.out.print( "Enter name of Contestant No " + (i+1) + ": " );
contestantNames[i] = stdin.readLine();
System.out.print( "Enter score of Contestant No " + (i+1) + ": " );
contestantScores[i] = Double.parseDouble(stdin.readLine());
}

c)
//default the lowest score and lowest index to be the first contestant
//then scan the rest of the contestants
//if any of them have a lower score, assign their index as the lowest
//after the for loop is over, the lowest score and index is found
//the name at the index is the name of the contestant to be eliminated
double lowestScore = contestantScores[0];
int lowestIndex = 0;
for (int i=1; i<NO_CONTESTANTS; i++)
{
if (contestantScores[i] < lowestScore)
{
lowestIndex = i;
lowestScore = contestantScores[i];
}
}
System.out.println( "Contestant " + contestantNames[lowestIndex] + " has the lowest score of " + lowestScore + " and is eliminated. " );


收錄日期: 2021-04-23 17:12:30
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070814000051KK06412

檢視 Wayback Machine 備份