嗨,我正在为我的java类编写以下程序,但我无法弄清楚为什么我的数组也无法工作,即使它们在不同的类中,也不能使int计数。方法。
public static void main(String[] args) {
//char{}{} Vote = {{President, Karl Karlington, Bob Mcbob, Nope Noper},{Vice President, Not Optiono, Hap Pinino, Rad ish},{Person, Not Person, Also Person, Person Person}};
System.out.print("Welcome to voteathon 10,000!!\n");
System.out.print("We will be displaying the canidates shortly\n");
//display(Vote);
PresVote(Nope, Bob, Karl);
VPresVote(Not, Hap, Rad);
PersonVote(NotP, AlsoP, Person);
System.out.printf("For president Karl had: %d Bob had: %d Nope had: %d \n", Karl, Bob, Nope);
System.out.printf("For vice president Not had: %d Hap had: %d Rad had: %d \n", Not, Hap, Rad);
System.out.printf("For person Not had: %d Also had: %d Person had: %d \n", NotP, AlsoP, Person);
}
public static void display(int x[][]){
for(int row=0;row<4;row++){
for(int column=0;column<x[row] .length;column++){
System.out.print(x[row][column]+"\t");
}
System.out.println();
}
}
public static void PresVote(int Nope, int Bob, int Karl){
Scanner input = new Scanner(System.in);
System.out.print("To vote please enter 1 2 or 3 corresponding to the table above\n");
System.out.print("vote: ");
int A = input.nextInt();
if (A == 1){
System.out.print("\nYou voted for Karl!\n");
Karl++;
}
else if(A == 2){
System.out.print("You voted for Bob!\n");
Bob++;
}
else if (A == 3){
System.out.print("You voted for Nope!\n");
Nope++;
}
else{
System.out.print("No vote invalid answer\n");
}
}
public static void VPresVote(int Not, int Hap, int Rad){
Scanner input = new Scanner(System.in);
System.out.print("To vote please enter 1 2 or 3 corresponding to the table above ");
System.out.print("\n vote: \n");
int A = input.nextInt();
if (A == 1){
System.out.print("You voted for Not!\n");
Not++;
}
else if(A == 2){
System.out.print("You voted for Hap!\n");
Hap++;
}
else if (A == 3){
System.out.print("You voted for Rad!\n");
Rad++;
}
else{
System.out.print("No vote invalid answer\n");
}
}
public static void PersonVote(int NotP, int AlsoP, int Person){
Scanner input = new Scanner(System.in);
System.out.print("To vote please enter 1 2 or 3 corresponding to the table above ");
System.out.print("\n vote: \n");
int A = input.nextInt();
if (A == 1){
System.out.print("You voted for Not!\n");
NotP++;
}
else if(A == 2){
System.out.print("You voted for Also!\n");
AlsoP++;
}
else if (A == 3){
System.out.print("You voted for Person!\n");
Person++;
}
else{
System.out.print("No vote invalid answer\n");
}
}
我事先将init初始化了,但是我不知道这有什么作用
答案 0 :(得分:0)
您没有返回传入的参数。在这种情况下,局部变量的参数没有全局作用域,因此在返回函数调用时,它们不保存执行信息(例如在这种情况下的增量)。
例如,传递这样的数组:
public static void main(String[] args) {
int[] president = new int[3]; //president[0] indicates 'x',president[2] say 'z'
Scanner input = new Scanner(System.in);
System.out.print("To vote please enter 1 2 or 3 corresponding to the table above\n");
System.out.print("vote: ");
int A = input.nextInt();
presVote(president, A);
// rest of code
}
public static void presVote(int[] president, int A) {
if(A == 1) {
president[0]++; // vote count of 'x' increases by 1
}
// similar conditionals for other input
}
通过这种方式,总统席位将具有投票计数。
或其他方式是维护静态int变量以存储个人的投票计数。