我正在创建一个 VotingMachine,整个代码似乎没问题,但是当我尝试 运行 它时,我无法得到我想要的结果
I'm creating a VotingMachine, the whole code seems ok but when I try to run it, I can't get the result I want
我的代码:
import java.util.*;
public class VotingMachine {
private int WuDI;
private int HonST;
private int HawLB;
private int JanCS;
private int HanGU;
private int PanWG;
public VotingMachine() { WuDI = 0; HonST = 0; HawLB = 0; JanCS = 0; HanGU = 0; PanWG = 0; }
public VotingMachine(int WuDI, int HonST, int HawLB, int JanCS, int HanGU, int PanWG) {
this.WuDI = WuDI;
this.HonST = HonST;
this.HawLB = HawLB;
this.JanCS = JanCS;
this.HanGU = HanGU;
this.PanWG = PanWG;
}
public void showVote(){
System.out.println("WuDI = " + WuDI + "vote(s)");
System.out.println("HonST = " + HonST + "vote(s)");
System.out.println("HawLB = " + HawLB + "vote(s)");
System.out.println("JanCS = " + JanCS + "vote(s)");
System.out.println("HanGU = " + HanGU + "vote(s)");
System.out.println("PanWG = " + PanWG + "vote(s)");
}
public void clear() { WuDI = 0; HonST = 0; HawLB = 0; JanCS = 0; HanGU = 0; PanWG = 0; }
public void voteWuDI() { WuDI+=1; }
public void voteHonST() { HonST+=1; }
public void voteHawLB() { HawLB+=1; }
public void voteJanCS() { JanCS+=1; }
public void voteHanGU() { HanGU+=1; }
public void votePanWG() { PanWG+=1; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
VotingMachine v1 = new VotingMachine();
v1.clear();
for(;;){
System.out.print("Cast your vote by entering the candidate's name: ");
String name = in.next();
if(name == "WuDI")
v1.voteWuDI();
else if(name == "HonST")
v1.voteHonST();
else if(name == "HawLB")
v1.voteHawLB();
else if(name == "JanCS")
v1.voteJanCS();
else if(name == "HanGU")
v1.voteHanGU();
else if(name == "PanWG")
v1.votePanWG();
else System.err.println("The name your entered is not exist.");
System.out.print("Do you want to continue to vote? (Y/N): ");
char ans = in.next().charAt(0);
if(ans == 'N' || ans == 'n'){
System.out.println("The result of the election is: ");
v1.showVote();
System.out.println("Thank you for your vote!");
System.exit(0);}
else continue;
}
}
}
我的代码看起来没问题,编译器没有给我任何错误信息,但我一直收到这个:
Cast your vote by entering the candidate's name: WuDI
Do you want to continue to vote? (Y/N): The name your entered is not exist.
N
The result of the election is:
WuDI = 0vote(s)
HonST = 0vote(s)
HawLB = 0vote(s)
JanCS = 0vote(s)
HanGU = 0vote(s)
PanWG = 0vote(s)
Thank you for your vote!
我一直收到错误消息,投票无法累积!我真的想不通我哪里做错了!
请帮帮我,谢谢!
使用 name.equals("Wudi") 代替名称 == "Wudi".
当您编写 == 时,您使用的是标准对象比较器。这 returns 仅当对象完全相同时才为真。所以 name 和 "Wudi" 不一样,因为它们是不同的实例,即使它们包含相同的字符。
当您使用 equals 方法时,您是在比较字符串所代表的内容。
我的代码:
import java.util.*;
public class VotingMachine {
private int WuDI;
private int HonST;
private int HawLB;
private int JanCS;
private int HanGU;
private int PanWG;
public VotingMachine() { WuDI = 0; HonST = 0; HawLB = 0; JanCS = 0; HanGU = 0; PanWG = 0; }
public VotingMachine(int WuDI, int HonST, int HawLB, int JanCS, int HanGU, int PanWG) {
this.WuDI = WuDI;
this.HonST = HonST;
this.HawLB = HawLB;
this.JanCS = JanCS;
this.HanGU = HanGU;
this.PanWG = PanWG;
}
public void showVote(){
System.out.println("WuDI = " + WuDI + "vote(s)");
System.out.println("HonST = " + HonST + "vote(s)");
System.out.println("HawLB = " + HawLB + "vote(s)");
System.out.println("JanCS = " + JanCS + "vote(s)");
System.out.println("HanGU = " + HanGU + "vote(s)");
System.out.println("PanWG = " + PanWG + "vote(s)");
}
public void clear() { WuDI = 0; HonST = 0; HawLB = 0; JanCS = 0; HanGU = 0; PanWG = 0; }
public void voteWuDI() { WuDI+=1; }
public void voteHonST() { HonST+=1; }
public void voteHawLB() { HawLB+=1; }
public void voteJanCS() { JanCS+=1; }
public void voteHanGU() { HanGU+=1; }
public void votePanWG() { PanWG+=1; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
VotingMachine v1 = new VotingMachine();
v1.clear();
for(;;){
System.out.print("Cast your vote by entering the candidate's name: ");
String name = in.next();
if(name == "WuDI")
v1.voteWuDI();
else if(name == "HonST")
v1.voteHonST();
else if(name == "HawLB")
v1.voteHawLB();
else if(name == "JanCS")
v1.voteJanCS();
else if(name == "HanGU")
v1.voteHanGU();
else if(name == "PanWG")
v1.votePanWG();
else System.err.println("The name your entered is not exist.");
System.out.print("Do you want to continue to vote? (Y/N): ");
char ans = in.next().charAt(0);
if(ans == 'N' || ans == 'n'){
System.out.println("The result of the election is: ");
v1.showVote();
System.out.println("Thank you for your vote!");
System.exit(0);}
else continue;
}
}
}
我的代码看起来没问题,编译器没有给我任何错误信息,但我一直收到这个:
Cast your vote by entering the candidate's name: WuDI
Do you want to continue to vote? (Y/N): The name your entered is not exist.
N
The result of the election is:
WuDI = 0vote(s)
HonST = 0vote(s)
HawLB = 0vote(s)
JanCS = 0vote(s)
HanGU = 0vote(s)
PanWG = 0vote(s)
Thank you for your vote!
我一直收到错误消息,投票无法累积!我真的想不通我哪里做错了! 请帮帮我,谢谢!
使用 name.equals("Wudi") 代替名称 == "Wudi".
当您编写 == 时,您使用的是标准对象比较器。这 returns 仅当对象完全相同时才为真。所以 name 和 "Wudi" 不一样,因为它们是不同的实例,即使它们包含相同的字符。
当您使用 equals 方法时,您是在比较字符串所代表的内容。