佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1247|回复: 8

关于java array

[复制链接]
发表于 8-9-2009 07:45 PM | 显示全部楼层 |阅读模式
我已经display最高的votes 但是 要怎样display最高的votes follow by他的name呢 请教请教
小弟在线等~

import java.util.*;
public class T8Q2 {
   
    public static void main(String[] args) {
            
            Scanner myScanner = new Scanner(System.in);
            
            String[] candidateNames = new String[5];
            int[] votesReceive = new int[5];
            int total = 0;
            int topVotesReceive = votesReceive[0];
               
            //to prompt user enter 5 names and 5 votes
            for (int i = 0; i < candidateNames.length; i++) {        
                    System.out.print("Enter candidate's name: ");
                    candidateNames = myScanner.next();
                    System.out.print("Enter candidate's vote receive: ");
                    votesReceive = myScanner.nextInt();
                    System.out.println("");
            }
               
                //to display all the names and votes
                for (int i = 0; i < votesReceive.length; i++) {                        
                        System.out.println(candidateNames + " has receive " +  votesReceive + " votes");
                }
                System.out.println("");
               
                //to find the largest votes only
                for (int i = 1; i < votesReceive.length; i++) {
                        if (votesReceive > topVotesReceive)
                                topVotesReceive = votesReceive;
                }
                System.out.println("The top vote is: " + topVotesReceive);

    }
}


[ 本帖最后由 卖女孩滴小火柴 于 8-9-2009 07:57 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 8-9-2009 10:20 PM | 显示全部楼层
  1. // to find the largest votes only
  2. int idx = 0;

  3. for (int i = 1; i < votesReceive.length; i++) {
  4.     if (votesReceive > topVotesReceive) {
  5.         topVotesReceive = votesReceive;
  6.         idx = i;
  7.     }
  8. }

  9. System.out.println("The top vote is: " + votesReceive[idx]);
  10. System.out.println("The candidate is: " + candidateNames[idx]);
复制代码

你可以考慮把candidate設計成一個class,如果他有很多properties的話。
回复

使用道具 举报

发表于 8-9-2009 11:35 PM | 显示全部楼层
尝试用 map。 candidate's name 是 key,vote number 是 value。
回复

使用道具 举报

 楼主| 发表于 9-9-2009 12:24 AM | 显示全部楼层
小弟初学= =
没有学map还有做别的class 而且老师固定要用array而已哦。。。
回复

使用道具 举报

发表于 9-9-2009 05:57 AM | 显示全部楼层
这样行吗?

  1. String topName = "";
  2.                 for (int i = 1; i < votesReceive.length; i++) {
  3.                         if (votesReceive[i] > topVotesReceive)
  4.                         {
  5.                                 topVotesReceive = votesReceive[i];
  6.                                 topName = candidateNames[i];
  7.                                }
  8.                 }
  9. System.out.println("The top vote is: " + topVotesReceive+" by "+topName);
复制代码
回复

使用道具 举报

发表于 11-9-2009 12:56 PM | 显示全部楼层
你的意思是要sorting吗?然后也要show名字吗??你可以用buble sort, 然后print出来
回复

使用道具 举报

Follow Us
发表于 13-9-2009 01:10 AM | 显示全部楼层
你可以尝试写一个candidate's class

Example :

public class Candidate{
private int votesReceive;
private String candidateName;

public setVotesReceive(int voteReceive){
this.votesReceive = voteReceive;

}

public getVotesReceive(){
return votesReceive;

}

public setCandidateName(String candidateName){

this.candidateName = candidateName;

}

public getCandidateName(){
return candidateName;

}


}

然后,在你的 main method 里头, create 一个candidate's class 的array。

Candidate[ ] candidateArray = new Candidate[5];


然后,在你的loop里头

for (int i = 0; i < candidateArray.length; i++) {        

                    System.out.print("Enter candidate's name: ";
                    candidateArray.setCandidateName(myScanner.next());
                    System.out.print("Enter candidate's vote receive: ";
                    candidateArray.setVotesReceived(myScanner.nextInt());
            System.out.println("";
            }


最后,你只需要用loop来骗人print out 就可以了

回复

使用道具 举报

发表于 13-9-2009 11:22 AM | 显示全部楼层
原帖由 jacklvang1984 于 13-9-2009 01:10 AM 发表
你可以尝试写一个candidate's class

Example :

public class Candidate{
private int votesReceive;
private String candidateName;

public setVotesReceive(int voteReceive){
this.votesReceive = vot ...


他还是学生,我想他应该不明白这个方法,学校讲师都不会教怎么写一个entity class来store object value
回复

使用道具 举报


ADVERTISEMENT

发表于 13-9-2009 11:29 AM | 显示全部楼层
原帖由 卖女孩滴小火柴 于 8-9-2009 07:45 PM 发表
我已经display最高的votes 但是 要怎样display最高的votes follow by他的name呢 请教请教
小弟在线等~


你可以试以下的方法,这是骗吃的方法,等你学会了怎么用hashmap和写entity class,VO,BO class来store object value,那么可以不用这个方法了。

import java.util.*;
public class T8Q2 {
   
    public static void main(String[] args) {
            
            Scanner myScanner = new Scanner(System.in);
            String[] candidateNames = new String[5];
            int[] votesReceive = new int[5];
            int total = 0;
            int count = 0;
            int topVotesReceive = votesReceive[0];
            String candiateName = "";
               
            //to prompt user enter 5 names and 5 votes
            for (int i = 0; i < candidateNames.length; i++) {        
                    System.out.print("Enter candidate's name: ");
                    candidateNames = myScanner.next();
                    System.out.print("Enter candidate's vote receive: ");
                    votesReceive = myScanner.nextInt();
                    System.out.println("");
            }
               
                //to display all the names and votes
                for (int i = 0; i < votesReceive.length; i++) {                        
                        System.out.println(candidateNames + " has receive " +  votesReceive + " votes");
                }
                System.out.println("");
               
                //to find the largest votes only
                for (int i = 1; i < votesReceive.length; i++) {
                        if (votesReceive > topVotesReceive){
                                topVotesReceive = votesReceive;
                            count = i;
                        } // it is better that you have open and close bracket even you only have 1 statement in if statement..this is good behaviour for progammer
                }
         
                if(candidateNames[count] != null){
                    candiateName = candidateNames[count].toString();
                }
               
                System.out.println("The top vote is: " + topVotesReceive);
                System.out.println("The top candidate name  is: " + candiateName );

    }
}

基本的idea是,用count来储存votes最高的i value,然后用count来拿candidatesnames array的名字。。因为你先key in candadates name,然后才是votes,所以也就是说,你的candidatesname array 和你的votes arrays是互相对应的。。。

[ 本帖最后由 fannwong 于 13-9-2009 11:31 AM 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 5-12-2025 11:58 PM , Processed in 0.144199 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表