java编程题 关于考试成绩排名
编写一个考试成绩排名,输入10个人的一门课程的考试分数,按分数从高到底把名次一一列出来。(需要输入)
补充:import java.util.Scanner;
public class test{
public static void main(String[] args){
test ts=new test();
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("输入10个同学的成绩,中间以空格隔开");
for (int i=0;i<a.length;i++) {
a[i]=sc.nextInt();
}
test.paixu(a);
for (int i=0;i<a.length;i++)
System.out.println(a[i]);}
public static void paixu(int array[]){
int i;
int k;
for (i=0;i<array.length;i++){
for (k=0;k<array.length-1-i;k++){
if (array[k]<array[k + 1]) {
int temp=array[k];array[k]=array[k+1];array[k+1]=temp;
}}}}}我只做了一部分,可是是一维数组,没有进行学号排名,那个高手能改进下,或者帮我编一个可以实现学号排名
追问:可不可以给我讲讲你的编程思路,非常感谢
答案:
import java.util.Scanner;
public class test{
public static void main(String[] args){
test ts=new test();
Scanner sc=new Scanner(System.in);
System.out.println("输入10个同学的学号和成绩(格式如“学号-成绩”),中间以空格隔开");
//测试数据 101-76 102-54 103-67 104-83 105-95 106-86 107-60 108-70 109-89 110-47
String s = sc.nextLine();
String[] m = s.split(" "); //split()拆分字符串
String[] n = ts.paixu(m);
if(n!=null){
System.out.println("下面是成绩排名(分数从高到低):");
System.out.printf("%-12s%-12s%-12s","排名","学号","分数");//printf()格式化输出
System.out.println();
for (int i=0;i<n.length;i++){
System.out.printf("%-12s%-12s%-12s",(i+1)," "+n[i].split("-")[0]," "+n[i].split("-")[1]);
System.out.println();
}
}
}
public String[] paixu(String str[]){
int i;
int k;
int[] array = new int[str.length];
try {
for (i=0;i<str.length;i++)
array[i] = Integer.parseInt(str[i].split("-")[1]);//获取字符串中的成绩部分并转换为整数
}
catch (Exception ex) {
System.out.println("您的输入格式不正确!正确格式为:“学号-成绩 学号-成绩”,且都为整数。");
return null;
}
for (i=0;i<array.length;i++){
for (k=0;k<array.length-1-i;k++){
if (array[k]<array[k + 1]) {
int temp=array[k];array[k]=array[k+1];array[k+1]=temp;
String tempStr=str[k];str[k]=str[k+1];str[k+1]=tempStr;
}
}
}
return str;
}
}
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] xuehaos = new String[10];
int[] chengjis = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("第"+(i+1)+"位同学:\n学号:");
xuehaos[i] = sc.next();
System.out.println("成绩:");
chengjis[i] = sc.nextInt();
}
String xuehao = "";
int chengji = 0;
for (int i = 0; i < chengjis.length; i++) {
for (int j = i + 1; j < chengjis.length; j++) {
if (chengjis[i] < chengjis[j]) {
chengji = chengjis[j];
chengjis[j] = chengjis[i];
chengjis[i] = chengji;
xuehao = xuehaos[j];
xuehaos[j] = xuehaos[i];
xuehaos[i] = xuehao;
}
}
}
System.out.println("学号\t成绩");
for (int i = 0; i < chengjis.length; i++) {
System.out.println(xuehaos[i] + "\t" + chengjis[i]);
}
}
}
上一个:求 java拼图的9 25方格 源码
下一个:给个Java接口回调的例子