要求将整数输入到 int 列表中(不允许数组)
Asking for input of integers, into a list of int (no arrays allowed)
我如何在一个方法中获取用户输入的整数列表,并能够调用该方法并在不同的方法中计算最大值和最小值以及平均值?
问题是:要求用户在class中输入学生人数。使用循环,编写一个程序,将所有输入的学生的考试成绩一一获取。
创建一个菜单以显示以下选项:
1. 最低等级
2. 最高等级。
3.平均。
等等
不允许数组
使用不同的方法
import java.util.Scanner;
public class hw07_emena
{
public static double gradeEnter(double sum, double min, double max)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the amount of students in a class: ");
int amtStudents = keyboard.nextInt();
int i=0;
sum=0;
if(i<amtStudents)
{
System.out.println("Please enter the grade for student " + (i+1));
double stGrade = keyboard.nextDouble();
sum = sum+stGrade;
i++;
return sum;
}
}
/*public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the number of students in a class: ");
int amtStudents = keyboard.nextInt();
double sum = 0;
int i=0;
while(i<=amtStudents)
{
System.out.println("Please enter the grade of student " + (i+1));
double stGrade = keyboard.nextDouble();
i++;
double minGrade = 9999;
double maxGrade = -9999;
double classAvg = 0;
if(i==amtStudents)
{
System.out.println("");
}
}
}
*/
}
试试这个:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numStudents = sc.nextInt();
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int total = 0;
for (int i=0; i<numStudents; i++){
int grade = sc.nextInt();
max = Math.max(max, grade);
min = Math.min(min, grade);
total += grade;
}
double average = total/numStudents;
//Your menu will be specific to you. It will need to go here and return
//average, min, or max
}
您不需要存储成绩,只需存储结果。
我如何在一个方法中获取用户输入的整数列表,并能够调用该方法并在不同的方法中计算最大值和最小值以及平均值?
问题是:要求用户在class中输入学生人数。使用循环,编写一个程序,将所有输入的学生的考试成绩一一获取。 创建一个菜单以显示以下选项: 1. 最低等级 2. 最高等级。 3.平均。 等等
不允许数组 使用不同的方法
import java.util.Scanner; public class hw07_emena {
public static double gradeEnter(double sum, double min, double max)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the amount of students in a class: ");
int amtStudents = keyboard.nextInt();
int i=0;
sum=0;
if(i<amtStudents)
{
System.out.println("Please enter the grade for student " + (i+1));
double stGrade = keyboard.nextDouble();
sum = sum+stGrade;
i++;
return sum;
}
}
/*public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the number of students in a class: ");
int amtStudents = keyboard.nextInt();
double sum = 0;
int i=0;
while(i<=amtStudents)
{
System.out.println("Please enter the grade of student " + (i+1));
double stGrade = keyboard.nextDouble();
i++;
double minGrade = 9999;
double maxGrade = -9999;
double classAvg = 0;
if(i==amtStudents)
{
System.out.println("");
}
}
}
*/
}
试试这个:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numStudents = sc.nextInt();
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int total = 0;
for (int i=0; i<numStudents; i++){
int grade = sc.nextInt();
max = Math.max(max, grade);
min = Math.min(min, grade);
total += grade;
}
double average = total/numStudents;
//Your menu will be specific to you. It will need to go here and return
//average, min, or max
}
您不需要存储成绩,只需存储结果。