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 展开
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 展开
1个回答
展开全部
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]);
}
}
}
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]);
}
}
}
追问
是我没有复制完全啊不好意思
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询