我应该建立一个程序来获取课程中的成绩并计算最小值和最大值以及平均值,然后询问您是否要继续。这是我使用的代码,但它仍然无法正常工作。你能看出错误在哪里吗?
public class calculateAvgMAxMin
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // declares scanner input
LinkedList<Integer> list = new LinkedList<Integer>(); // declare new LinkedList of variables which are the grades
int answer = 1; // declare variable answer =1
do { // start of do loop
System.out.print("Please enter the name of the course:"); // gives a prompt to enter the name of the course
// goes to the next line
String course=input.nextLine(); // declare course as string and sets the value as the input
System.out.println("Please enter the scores for " + course + " on a single line and type a -1 at the end"); //prompts user to enter scores for class in one line and -1 to stop
int max=-100; // declare max as integer = -100
int min=500;//declare min as integer = 500
int sum=0;// declare sum as intgetr = 0
int grade; // declare grade as integer
grade=input.nextInt(); // set grade as next user iput
if (grade==-1) // if statement if the sentinel value is set then stop
break;
list.add(grade); // adds the grade from the grade variable to the LinkedList
sum=sum+grade;// put the value of sum to sum and the new grade
if (grade>max) // if statement if grade is more than the max then sets max as the grade
max=grade;
if (grade<min) // if statement if grade is less than the min then sets min as the grade
min=grade;
System.out.println("The course name: "+ course); // prints message of the course name entered
System.out.println("Number of Scores :"+list.size()); // sits the number of scores using the size of the list
System.out.printf("The Average Score: %.2f" ,(double)sum/list.size()); // calculates average to 2 decimal values using the sum divided by the length of the list
System.out.println(); // goes to next line
System.out.println("The Minimum Score: "+min); // sets the minimum value to min
System.out.println("The Maximum Score :"+max);// sets the minimum value to min
answer = JOptionPane.showConfirmDialog(null," Would you like to continue?"); // sets the value of the variable answer to confrim dialog box
} while (answer == 0); // sets the condition for do loop to answer == o so that it would continue only if the yes is selected
}
}
答案 0 :(得分:0)
我认为以下几行不正确:
int max=-100; // declare max as integer = -100
int min=500;//declare min as integer = 500
他们应该是:
int max= 500; // declare max as integer = -100
int min=-100;//declare min as integer = 500
此外,他们应该移到do-while
之外。
答案 1 :(得分:0)
你需要另一个...每个分数的时间
<ul>
<li ng-repeat ="data in datas"> {{data.tags.name}}</li>
</ul>
答案 2 :(得分:0)
我首先要编程到List
接口(而不是具体的集合),我更喜欢菱形运算符<>
(可用Java 7+)。接下来,我会分别将max
和min
初始化为Integer.MIN_VALUE
和Integer.MAX_VALUE
(因为没有int
小于第一个,或者跟随,大于第二)。然后我更喜欢Math.max(int, int)
和Math.min(int, int)
。最后,我建议您在显示统计信息之前获取所有值。像,
List<Integer> list = new LinkedList<>();
int answer = 1;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
do {
System.out.print("Please enter the name of the course:");
String course = input.nextLine();
System.out.printf("Please enter the scores for %s on a single "
+ "line and type a -1 at the end%n", course);
int grade = input.nextInt();
if (grade == -1) {
break;
}
System.out.printf("Course %s, Grade %d%n", course, grade);
list.add(grade);
sum += grade;
max = Math.max(max, grade);
min = Math.min(min, grade);
answer = JOptionPane.showConfirmDialog(null,
"Would you like to continue?");
} while (answer == 0);
System.out.println("Number of Scores :" + list.size());
System.out.printf("The Average Score: %.2f%n",
sum / (double) list.size());
System.out.println("The Minimum Score: " + min);
System.out.println("The Maximum Score :" + max);
答案 3 :(得分:0)
将您的最大值和最小值设置为的位置和内容似乎是错误的。它应该是:
int min = -100;
int max = 500;