import java.util.Scanner;
public class _08_03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMBER_OF_STUDENTS = 8;
char[][] answers = {
{'A','B','A','C','C','D','E','E','A','D'},
{'D','B','A','B','C','A','E','E','A','D'},
{'E','D','D','A','C','B','E','E','A','D'},
{'C','B','A','E','D','C','E','E','A','D'},
{'A','B','D','C','C','D','E','E','A','D'},
{'B','B','E','C','C','D','E','E','A','D'},
{'B','B','A','C','C','D','E','E','A','D'},
{'E','B','E','C','C','D','E','E','A','D'}};
int[] correctCount = new int [NUMBER_OF_STUDENTS];
char[] keys = {'D','B','D','C','C','D','A','E','A','D'};
for(int i=0;i<answers.length;i++){
int correctCountNumbers = 0;
for(int j=0;j<answers[i].length;j++){
if(answers[i][j]==keys[j])
correctCountNumbers++;
}
correctCount[i] = correctCountNumbers;
}
System.out.println("The sort is as follow:" );
sort(correctCount,NUMBER_OF_STUDENTS);
}
public static void sort(int[] m,int NUMBER_OF_STUDENTS ){
int[] flag=new int [NUMBER_OF_STUDENTS];
int k=0;
for(int i=0;i<m.length-1;i++){
for(int j=0;j<m.length-1-i;j++){
if(m[j]>m[j+1]){
flag[k] = j+1;
int temp = m[j];
m[j] = m[j+1];
m[j+1] = temp;
}
else
flag[k] = j;
}
k++;
}
for(k=0;k<8;k++)
System.out.println("Student " + flag[k]+"'s correct count is " +
m[k]);
}
}
正确答案是 Studeng 3's correct count is 4
Studeng 2's correct count is 5
Studeng 1's correct count is 6
Studeng 0's correct count is 7
Studeng 5's correct count is 7
Studeng 6's correct count is 7
Studeng 7's correct count is 7
Studeng 4's correct count is 8
我打算将flag按答题的正确个数由少到多的学生的编号排序,可答题整数数排序好后,问题中的flag是乱的
JAVA,按照正确答案个数升序显示学生?求大神指点,怎么将 flag一维数组跟踪到正确的下标?
答案:1 悬赏:0 手机版
解决时间 2021-02-21 15:09
- 提问者网友:听门外雪花风
- 2021-02-21 05:42
最佳答案
- 五星知识达人网友:笑迎怀羞
- 2021-02-21 06:13
思路:
方案一:
第一步: 可以考虑用一个二维数组,该数组第一个元素保存学生的位置, 第二个元素保存学生的成绩.
第二步:对二维数组的按照成绩部分进行排序
第三步:输出的排序后的二维数组,既保存了原始的学生位置,又保存了对应的成绩. 所以可以打印输出
其他方案:
后面学了面向对象的话,可以考虑把Student作为一个对象. 然后把位置下标作为一个属性保存到Student对象里,就稍微好解决一点了
参考代码如下
public class _08_03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMBER_OF_STUDENTS = 8;
char[][] answers = { { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' },
{ 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' },
{ 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' },
{ 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' } };
int[] correctCount = new int[NUMBER_OF_STUDENTS];
char[] keys = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' };
for (int i = 0; i < answers.length; i++) {
int correctCountNumbers = 0;
for (int j = 0; j < answers[i].length; j++) {
if (answers[i][j] == keys[j])
correctCountNumbers++;
}
correctCount[i] = correctCountNumbers;
}
System.out.println("The sort is as follow:");
//把数组原始下标和正确个数,组合成一个二维数组 {0,7}{1,6},{2,5}....
int[][] arr = new int[correctCount.length][2];
for (int i = 0; i < correctCount.length; i++) {
arr[i][0]=i;
arr[i][1]=correctCount[i];
}
sort(arr);
}
public static void sort(int[][] arr) {
//按照成绩排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j][1] > arr[j + 1][1]) { //按照成绩升序排列
int[] temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int[] ss : arr) {
// 数组第一个是原始下标, 第二个元素是成绩
System.out.println("Student "+ss[0]+"'s correct count is "+ss[1]);
}
}
}运行输出
The sort is as follow:
Student 3's correct count is 4
Student 2's correct count is 5
Student 1's correct count is 6
Student 0's correct count is 7
Student 5's correct count is 7
Student 6's correct count is 7
Student 7's correct count is 7
Student 4's correct count is 8
方案一:
第一步: 可以考虑用一个二维数组,该数组第一个元素保存学生的位置, 第二个元素保存学生的成绩.
第二步:对二维数组的按照成绩部分进行排序
第三步:输出的排序后的二维数组,既保存了原始的学生位置,又保存了对应的成绩. 所以可以打印输出
其他方案:
后面学了面向对象的话,可以考虑把Student作为一个对象. 然后把位置下标作为一个属性保存到Student对象里,就稍微好解决一点了
参考代码如下
public class _08_03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMBER_OF_STUDENTS = 8;
char[][] answers = { { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' },
{ 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' },
{ 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' },
{ 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
{ 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' } };
int[] correctCount = new int[NUMBER_OF_STUDENTS];
char[] keys = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' };
for (int i = 0; i < answers.length; i++) {
int correctCountNumbers = 0;
for (int j = 0; j < answers[i].length; j++) {
if (answers[i][j] == keys[j])
correctCountNumbers++;
}
correctCount[i] = correctCountNumbers;
}
System.out.println("The sort is as follow:");
//把数组原始下标和正确个数,组合成一个二维数组 {0,7}{1,6},{2,5}....
int[][] arr = new int[correctCount.length][2];
for (int i = 0; i < correctCount.length; i++) {
arr[i][0]=i;
arr[i][1]=correctCount[i];
}
sort(arr);
}
public static void sort(int[][] arr) {
//按照成绩排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j][1] > arr[j + 1][1]) { //按照成绩升序排列
int[] temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int[] ss : arr) {
// 数组第一个是原始下标, 第二个元素是成绩
System.out.println("Student "+ss[0]+"'s correct count is "+ss[1]);
}
}
}运行输出
The sort is as follow:
Student 3's correct count is 4
Student 2's correct count is 5
Student 1's correct count is 6
Student 0's correct count is 7
Student 5's correct count is 7
Student 6's correct count is 7
Student 7's correct count is 7
Student 4's correct count is 8
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯