【发布时间】:2018-06-23 13:36:31
【问题描述】:
我的老师希望我们制作一个程序,计算来自可变数量选区的两名候选人的总票数。因此,用户将候选人的姓名作为字符串输入,然后在输入每个选区的选票之前被提示输入选区的数量。我遇到的麻烦是我必须使用一个数组来保持每个选区的投票总数。然后,在完成所有这些之后,我必须在每个选区完成后为每个候选人保留一个运行总票数,以及目前谁领先以及领先多少。我已经开始了我的程序,但老实说,我只是不知道从哪里开始,而且我知道到目前为止我的数组中的内容是不正确的。
import java.util.*;
public class Voting
{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
int rerun;
int rerun2;
while (rerun == 1)
{
System.out.print("Name of candidate 1: ");
candidate1 = scan.next();
System.out.print("Name of candidate 2: ");
candidate2 = scan.next();
System.out.print("\nPlease enter amount of precincts: ");
presincts = scan.nextInt();
System.out.print("\n Please enter amount of votes for candidate 1: ");
votes1 = scan.nextInt();
System.out.print("\n Please enter amount of votes for candidate 2: ");
votes2 = scan.nextInt();
while (rerun2 == 1 && precincts >= 0 && votes1 >= 0 && votes2 >= 0)
{
int[] votes1 = new int[precincts];
for(int i = 0; i < votes1.length; i++)
{
votes1[i] = int [i];
System.out.println ("\n" + votes1);
}
int[] votes2 = new int[precincts];
for(int i = 0; i < votes2.length; i++)
{
votes2[i] = int [i];
System.out.println ("\n" + votes2);
}
}
}
}
}
【问题讨论】:
标签: java arrays voting jcreator