import java.util.*;
public class Project5{
static Scanner console = new Scanner(System.in);
static final int ARRAY_SIZE = 5;
public static void main(String[] args){
Candidate[] candidateList = new Candidate[ARRAY_SIZE];
String name;
int votes;
int i = 0;
for (i = 0; i < ARRAY_SIZE; i++){
System.out.print("Enter Candidate #" + (i + 1) + " Name: ");
name = console.nextLine();
System.out.print("Please Enter the number of votes " + name + " Received: ");
votes = console.nextInt();
candidateList[i] = new Candidate(name, votes);
console.nextLine();
}
Candidate winner;
winner = candidateList[0];
数组的最大整数这是在那里我有我的问题,我有for循环每个五个字符串的计数我,但它似乎给Array不挑选得票最多的字符串作为赢家。我觉得这很简单,它简直就是飞过我的脑海。使用for循环,以找到5个整数
也是在这里简化此部分的最快方法是什么?
System.out.print(" Candidate Votes Recieved % of Total Votes");
System.out.print("\n");
System.out.print("\n");
一两件事,是有办法只是有这样的,但在一个格式的代码?因此,像{System.out.print(candidateList[i].getCandidateInfo());}
System.out.print(candidateList[0].getCandidateInfo());
System.out.print(candidateList[1].getCandidateInfo());
System.out.print(candidateList[2].getCandidateInfo());
System.out.print(candidateList[3].getCandidateInfo());
System.out.print(candidateList[4].getCandidateInfo());
System.out.print("The winner is: " + winner);
}
}
删除'else winner = candidateList [0];'语句。 – Berger
这些实际上是3个问题。 – Henry
'这里最简单的方法是什么?' - 以什么方式简化?较少的代码?更好的布局? - 有没有一种方法可以简单地得到这样的代码,但采用一种格式? - 有什么区别或问题?将语句放入循环如何? – Thomas