这是我要求遵循的提示。
编写一个名为average的方法,该方法接收一组测试成绩并返回字母等级 平均水平。 等级范围如下:
平均值> = 90 - >甲
80 <=平均值&lt; 90 - &gt;乙
70 <=平均值&lt; 80 - &gt; ç
60 <=平均值&lt; 70 - &gt; d
平均&lt; 60 - &gt; ˚F
使用以下方法标题:public static char average(int [] grade)
这是我应该遵循的输出示例。
您想要输入多少年级? 10
输入1:70 输入2:87 输入3:95 进入4:80 输入5:80 输入6:78 输入7:85 输入8:90 进入9级:66 输入10:89
测试的平均等级是:B
这是我到目前为止所做的,但我不知道如何包含上述方法标题。 public static char average(int [] grade)
import java.util.Scanner;
public class ClassAverage{
//Main Method
public static void main(String[] args){
//Variables
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b >= 90)
average = 'A';
else if(b <= 90 && b >= 80)
average = 'B';
else if(b <= 80 && b >= 70)
average = 'C';
else if(b <= 70 && b >= 60)
average = 'D';
else if(b <= 60)
average = 'F';
else
average = '?';
System.out.println("The class average for the test is: " + average);
}
}
答案 0 :(得分:1)
public class MainClass {
//Main Method
public static void main(String[] args) {
//Variables
char avg;
int i;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
avg = average(array);
System.out.println("The class average for the test is: " + avg);
}
private static char average(int[] array) {
final int sum = Arrays.stream(array).sum();
final int b = sum / array.length;
if (b >= 90) {
return 'A';
} else if (b <= 90 && b >= 80) {
return 'B';
} else if (b <= 80 && b >= 70) {
return 'C';
} else if (b <= 70 && b >= 60) {
return 'D';
} else if (b <= 60) {
return 'F';
} else {
return '?';
}
} }
看到我接受所有标记到数组中并将该int数组传递给我计算总和和等级并返回成绩的方法
答案 1 :(得分:0)
您的教师希望您创建一个名为average的方法,可以从main中调用。
基本上,完成分配所需要做的就是将代码包装在该方法中。因此,主要会将int
传递给平均值。
答案 2 :(得分:0)
我已经编写了一些伪代码来帮助您获取现在的代码并将其封装在您请求的方法中。
您已经编写了这些部分,只需将它们从main
方法移动到这个新的average
方法,然后根据更改范围进行任何必要的更改(声明或重命名某些变量)新方法)。
然后,要获得所需的输出,可以使用main
并使用print语句中返回的char值从average(gradeArray)
内调用该方法。
public static char average(int[] gradeArray) {
// Find the sum of grades in gradeArray
// Use this sum and the size of the array to compute the average
// Use logic to determine which letter grade to print out based on the average
// Return the average
}
答案 3 :(得分:0)
这是非常简单的代码。我的回答是
import java.util.Scanner;
public class AverageMethod {
public static void main(String[] args) {
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b<=100 && b>=0){
if(b<=100 && b >= 90)
average = 'A';
else if(b >= 80)
average = 'B';
else if(b >= 70)
average = 'C';
else if(b >= 60)
average = 'D';
else
average = 'F';
System.out.println("The class average for the test is: " + average);
}else{
System.out.println("Not in range");
}
}
}
我也为你添加了我的github上传的男女同校。如果您有任何问题,请关注我的githublink。请进行研究以获得更多说明。
答案 4 :(得分:0)
另一种解决方案:
package test163;
import java.util.Scanner;
public class ClassAverage {
//Main Method
public static void main(String[] args) {
//Variables
char average;
int i;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
average = average(array);
System.out.println("The class average for the test is: " + average);
}
public static char average(int[] grades) {
int sum = 0;
for (int grade: grades) {
sum += grade;
}
int b = sum / grades.length;
if (b >= 90) {
return 'A';
} else if (b >= 80) {
return 'B';
} else if (b >= 70) {
return 'C';
} else if (b >= 60) {
return 'D';
} else {
return 'F';
}
}
}