|
查看: 1247|回复: 8
|
关于java array
[复制链接]
|
|
|
我已经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 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 8-9-2009 10:20 PM
|
显示全部楼层
- // to find the largest votes only
- int idx = 0;
- for (int i = 1; i < votesReceive.length; i++) {
- if (votesReceive > topVotesReceive) {
- topVotesReceive = votesReceive;
- idx = i;
- }
- }
- System.out.println("The top vote is: " + votesReceive[idx]);
- 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
|
显示全部楼层
这样行吗? 
- String topName = "";
- for (int i = 1; i < votesReceive.length; i++) {
- if (votesReceive[i] > topVotesReceive)
- {
- topVotesReceive = votesReceive[i];
- topName = candidateNames[i];
- }
- }
- System.out.println("The top vote is: " + topVotesReceive+" by "+topName);
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 11-9-2009 12:56 PM
|
显示全部楼层
|
你的意思是要sorting吗?然后也要show名字吗??你可以用buble sort, 然后print出来 |
|
|
|
|
|
|
|
|
|
|
发表于 13-9-2009 01:10 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 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 |
|
|
|
|
|
|
|
|
|
|
发表于 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 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|