java while loop求解答!!!紧急!!!在线等!!!

WriteaprogramnamedGrades.javathatallowsausertoinputaseriesofexamscoresasintegers.Vali... Write a program named Grades.java that allows a user to input a series of exam scores as integers. Valid scores are 0 thru 100 (input validation is not required though).

Print out the largest and smallest score entered.

The number of scores is not limited, so use a sentinel (in this case -99) to terminate the input. This also means that NO scores may be entered if -99 is the first input from user.

Note: You must use a while-loop and user input to receive credit for this assignment.
Hint 1: The first score entered is both the smallest and the largest score.

Hint 2: Use a boolean flag inside the loop that 'remembers' whether the loop has been entered. Then check the flag to give user the correct output noting largest/smallest or no numbers were entered..

Here is first run of program (program output in italics and user input in bold/italics ):

Enter an integer, or -99 to quit: 80
Enter an integer, or -99 to quit: 95
Enter an integer, or -99 to quit: 65
Enter an integer, or -99 to quit: -99
Largest: 95
Smallest: 65
展开
 我来答
wadeMhglv
2014-03-12 · TA获得超过1255个赞
知道小有建树答主
回答量:1478
采纳率:60%
帮助的人:673万
展开全部
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Grades {
public static void main(String[] args) {
Boolean flag = true;
Scanner input = new Scanner(System.in);
List<String> numList = new ArrayList<String>();

while(flag)
{
System.out.println("Enter an integer, or -99 to quit:");
String numIn = input.next();
if(numIn.equals("-99"))
{
flag = false;
}
else
{
numList.add(numIn);
}
}

if(numList.size()==0)
{
System.out.println("NO scores may be entered if -99 is the first input!");
}
else
{
Object[] nums = numList.toArray();
for(int i=0;i<nums.length;i++)
{
for(int j=i+1;j<nums.length-1;j++)
{
if(Integer.valueOf(nums[i].toString()) < Integer.valueOf(nums[j].toString()))
{
Object temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

System.out.println("Largest: "+nums[0]);
System.out.println("Smallest: "+nums[nums.length-1]);
}
}
}
追问
是我没有复制完全啊不好意思
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式