| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 395 人关注过本帖
标题:心塞,对象数组排序的问题怎么解决……?
只看楼主 加入收藏
小月饼
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2013-11-3
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:2 
心塞,对象数组排序的问题怎么解决……?
编写了一个投票程序,只差投票后排序输出取得最高票的同学这段程序没写了。代码如下位置已标注出求解答,不明白怎么按照票数排序
程序代码:
public class Student {
    private int No;
    private String name;
    protected int vote;
    public Student(int No,String name,int vote){
        this.setNo(No);
        this.setName(name);
        this.setVote(vote);
    }

    public int getNo() {
        return No;
    }
    public void setNo(int no) {
        No = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getVote() {
        return vote;
    }
    public void setVote(int vote) {
        this.vote = vote;
    }
}

程序代码:
import java.util.Arrays;
import java.util.Scanner;


public  class VotePart{
    private Student stu[]= {new Student(1,"张三",0),new Student(2,"李四",0),new Student(3,"王五",0),new Student(4,"赵六",0)};
    public void print(){
        for (int i=0;i<this.stu.length;i++){
            System.out.println(this.stu[i].getNo()+":"+this.stu[i].getName()+"【"+this.stu[i].getVote()+"】");
        }
    }
    public void Operate(){
        this.print();
        this.vote();
        this.print();
        this.getResult();
    }
    public void vote(){
        boolean flag = false;
        while(flag==false){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入班长候选人代号(数字0结束):");
        String id=sc.next();

        switch (id){
        case "0":{
            flag = true;
            break;
        }
        case "1":{
            this.stu[0].setVote(this.stu[0].getVote()+1);
            break;
        }
        case "2":{
            this.stu[1].setVote(this.stu[1].getVote()+1);
            break;
        }
        case "3":{
            this.stu[2].setVote(this.stu[2].getVote()+1);
            break;
        }
        case "4":{
            this.stu[3].setVote(this.stu[3].getVote()+1);
            break;
        }
        default:{
            System.out.println("次选票无效,请输入正确的候选人代号!");
        }}}
        
        
    }
        public void getResult(){
                //这里缺少将对象数组排序的代码
        System.out.println("投票最终结果:"+this.stu[0].getName()+"同学,最后以"+this.stu[0].getVote()+"票当选班长!");
        }
        }


程序代码:
public class Demo {

    public static void main(String[] args) {
        new VotePart().Operate();
    }

}
2014-11-26 01:31
liucao
Rank: 10Rank: 10Rank: 10
来 自:恶灵之城
等 级:贵宾
威 望:13
帖 子:538
专家分:1575
注 册:2014-8-6
收藏
得分:5 
去@下 静夜思
咱论坛投票管理专员啊,有关投票问问他,我没接触过这个,顺带学点

one car come one car go ,two car peng peng people die.
2014-11-26 10:15
编号1016
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:46
专家分:188
注 册:2014-5-8
收藏
得分:5 
回复 楼主 小月饼
程序代码:
import java.util.HashMap;
import java.util.Scanner;


public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new VotePart().Operate();
    }

}
class VotePart
{
    private HashMap<String, String> list=new HashMap<>();//以 姓名-分数的形式存放输入分数的数据
    
    
    private Student stu[]= {new Student(1,"张三",0),new Student(2,"李四",0),new Student(3,"王五",0),new Student(4,"赵六",0)};
    public void print(){
        for (int i=0;i<this.stu.length;i++){
            System.out.println(this.stu[i].getNo()+":"+this.stu[i].getName()+"【"+this.stu[i].getVote()+"】");
        }
    }
    public void Operate(){
        this.print();
        list.clear();
        this.vote();
//        this.print();
        this.getResult();
    }
    public void vote(){
        boolean flag = false;
        while(flag==false)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入班长候选人代号(数字0结束):");
        String id=sc.next();
        //添加的id只是让你用户输入班长候选人编号,但并不能输入投票数,没有投票数自然也不能判断谁的票数高了;所以我又添加一个记录分数的v
        System.out.println("请输入该班长候选人票数(数字0结束):");
        String v=sc.next();
        
        
        switch (id)
        {
            case "0":{
                flag = true;
                break;
            }
            case "1":{
    //            this.stu[0].setVote(this.stu[0].getVote()+1);
                //这里就是将对应的候选人编号跟分数对应保存起来,这里1-1,2-1。。。只是为了更清楚知道哪个编号对于哪个候选人而已
                list.put(stu[1-1].getName(),v);
                break;
            }
            case "2":{
    //            this.stu[1].setVote(this.stu[1].getVote()+1);
                list.put(stu[2-1].getName(),v);
                
                break;
            }
            case "3":{
    //            this.stu[2].setVote(this.stu[2].getVote()+1);
                list.put(stu[3-1].getName(),v);
                break;
            }
            case "4":{
    //            this.stu[3].setVote(this.stu[3].getVote()+1);           
                list.put(stu[4-1].getName(),v);
                break;
            }
            default:{
                System.out.println("次选票无效,请输入正确的候选人代号!");
            } 
       }
        
   }
       
        
}
        public void getResult()
{
                //这里缺少将对象数组排序的代码
            int temp=0;//存放最高分数
            int array[]=new int[stu.length];//定义一个数组来存放所以的分数便于下面数组的排序获取最高分数值
            
            for(int i=0;i<stu.length;i++)
            {
                 array[i]=Integer.parseInt(list.get(stu[i].getName()));
            }
            //排序获取最高分数是多少,保存到temp
            for(int i=0;i<array.length-1;i++)
            {     
                if(array[i] > array[i+1])
                {      
                    temp = array[i];      
                    array[i] = array[i+1];      
                    array[i+1] = temp;                    
                }
            }
            //根据最高分数通过if的判断到list查找该分数对于的同学姓名,并显示出来;不过在这里有一个小小问题你需要你自己来修改,我就不做修改了;如果至少有两人的票数是一样的要怎么判断并显示出来? 
            for(int i=0;i<list.size();i++)
            {
                
                if(Integer.parseInt(list.get(stu[i].getName()))==temp)
                {
                    System.out.println("投票最终结果:"+stu[i].getName()+"同学,最后以"+temp+"票当选班长!");
                }
            }
            //这for循环的性质跟print是一样的,不过我没有将数据赋值给Student,自然getvote()中的值也就为0;所以我用比较方便的方法就直接从list里取值
            for(int i=0;i<list.size();i++)
            {
                 System.out.println(this.stu[i].getNo()+":"+this.stu[i].getName()+"【"+Integer.parseInt(list.get(stu[i].getName()))+"】");
            }
        
}
class Student
{
     private int No;
        private String name;
        protected int vote;
        public Student(int No,String name,int vote){
            this.setNo(No);
            this.setName(name);
            this.setVote(vote);
        }


        public int getNo() {
            return No;
        }
        public void setNo(int no) {
            No = no;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getVote() {
            return vote;
        }
        public void setVote(int vote) {
            this.vote = vote;
        }
    }
}

看似修改的地方比较多,但是这为了你方便理解所以写的比较详细点,不过里面还是有些不够好的小细节,需要你自己来动动手

相互学习,让自己变得更强大!
2014-11-26 12:00
快速回复:心塞,对象数组排序的问题怎么解决……?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018397 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved