编写一个Java程序,提示用户输入学生数量,学生姓名和他们的成绩,并按照成绩的降序打印学生姓名
importjava.util.Scanner;publicclassmyclass{publicstaticvoidmain(String[]args){Scanner...
import java.util.Scanner;
public class myclass
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.print("请输入学生个数,学生姓名以及学生成绩");
int number = input.nextInt();
String name = input.next();
double grade = input.nextDouble();
double[] mylist = new double[number];
String[] list = new String[ number];
for( int i = 0;i<number-1;i++)
{
mylist[i] = grade;
double max = mylist[i];
int maxindex = i ;
for(int j=i+1;j<number;j++)
{
if(mylist[j]>max)
{max = mylist[j];
maxindex = j;}
if(maxindex!= i)
{
mylist[maxindex] = mylist[i];
mylist[i] = max ;
}
}
for( i = 0; i< number;i++)
{
list[i] = name ;
}
System.out.println(mylist[i]);
System.out.println(list[i]);
}
}
}
求大神指导哪里有问题啊。。。 展开
public class myclass
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.print("请输入学生个数,学生姓名以及学生成绩");
int number = input.nextInt();
String name = input.next();
double grade = input.nextDouble();
double[] mylist = new double[number];
String[] list = new String[ number];
for( int i = 0;i<number-1;i++)
{
mylist[i] = grade;
double max = mylist[i];
int maxindex = i ;
for(int j=i+1;j<number;j++)
{
if(mylist[j]>max)
{max = mylist[j];
maxindex = j;}
if(maxindex!= i)
{
mylist[maxindex] = mylist[i];
mylist[i] = max ;
}
}
for( i = 0; i< number;i++)
{
list[i] = name ;
}
System.out.println(mylist[i]);
System.out.println(list[i]);
}
}
}
求大神指导哪里有问题啊。。。 展开
2个回答
展开全部
我刚写的
先定义一个类实现序列化
public class Students implements Comparable{
private String name;
private int grade;
public Students(String name, int grade) {
super();
this.name = name;
this.grade = grade;
}
public Students() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
return ((Students)obj).getGrade()-this.getGrade();
}
}
在另一个类里测试(刚试过可以)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生个数");
int number = input.nextInt();
Students[] ss=new Students[number];
for(int i=1;i<=number;i++){
System.out.print("请输入第"+i+"个学生的姓名");
String name = input.next();
System.out.print("请输入第"+i+"个学生的成绩");
int grade=input.nextInt();
ss[i-1]=new Students(name,grade);
}
Arrays.sort(ss);
for(int k=1;k<=number;k++){
System.out.println(ss[k-1].getName()+":"+ss[k-1].getGrade());
}
}
先定义一个类实现序列化
public class Students implements Comparable{
private String name;
private int grade;
public Students(String name, int grade) {
super();
this.name = name;
this.grade = grade;
}
public Students() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
return ((Students)obj).getGrade()-this.getGrade();
}
}
在另一个类里测试(刚试过可以)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生个数");
int number = input.nextInt();
Students[] ss=new Students[number];
for(int i=1;i<=number;i++){
System.out.print("请输入第"+i+"个学生的姓名");
String name = input.next();
System.out.print("请输入第"+i+"个学生的成绩");
int grade=input.nextInt();
ss[i-1]=new Students(name,grade);
}
Arrays.sort(ss);
for(int k=1;k<=number;k++){
System.out.println(ss[k-1].getName()+":"+ss[k-1].getGrade());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询