gpt4 book ai didi

java - 做而行不通

转载 作者:行者123 更新时间:2023-11-30 03:02:14 27 4
gpt4 key购买 nike

所以我的投票系统有这个代码

public void Register() throws IOException{
String [] menuGender={"Male", "Female"};
String [] menuStatus={"Single", "Married", "Widow(er)", "Legally separated"};


do{
FileWriter writeFile=new FileWriter("voters.txt", true);
BufferedWriter outFile=new BufferedWriter(writeFile);

age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
while(age<18){
JOptionPane.showMessageDialog(null, "Voter should be 18 or above");
age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
}
name=JOptionPane.showInputDialog("Full Name: ");
gender=(String)JOptionPane.showInputDialog(null, "Gender:", "Election 2765", 1, null, menuGender, menuGender[0]);
if(gender=="Male"){
gender="Male";
}
else{
gender="Female";
}
dBirth=JOptionPane.showInputDialog("Date of Birth: ");
pBirth=JOptionPane.showInputDialog("Place of Birth: ");
address=JOptionPane.showInputDialog("Address\n(Province, City/Municipality, Barangay, House No./Street: ");
status=(String)JOptionPane.showInputDialog(null, "Civil Status:", "Election 2765", 1, null, menuStatus, menuStatus[0]);
if(status=="Single"){
status="Single";
}
else if(status=="Married"){
spouse=JOptionPane.showInputDialog("Spouse Name: ");
status="Married(Spouse: "+spouse+")";
}
else if(status=="Widow(er)"){
status="Widow(er)";
}
else{
status="Legally Separated";
}
citizenship=JOptionPane.showInputDialog("Citizenship:");
job=JOptionPane.showInputDialog("Profession/Occupation: ");
tin=JOptionPane.showInputDialog("Tin Number: ");
father=JOptionPane.showInputDialog("Father's Full Name: ");
mother=JOptionPane.showInputDialog("Mother's Full Name: ");
votersNumber++;

vNumber=Integer.toString(votersNumber);

outFile.append(vNumber+"/"+name+"/"+age+"/"+gender+"/"+dBirth+"/"+pBirth+"/"+address+"/"+status+"/"+citizenship+"/"+job+"/"+father+"/"+mother);
outFile.newLine();

outFile.close();

selectYN=JOptionPane.showInputDialog("Continue?\n[1]Yes [2]No");
}while(selectYN!="2");
}

我的问题是我的 do while 循环不起作用。每次我输入 2 时,它仍然会返回到输入信息。我的目标是当我输入 2 时,它将返回到菜单。这是菜单的代码

字符串选择,选择2; String[]firstMenu = {"注册/编辑信息","选民名单","选民验证及候选人投票","退出"};

    do{
choice=(String)JOptionPane.showInputDialog(null, "Welcome! Please choose:", "National Election 2765", 1, null, firstMenu, firstMenu[0]);
switch(choice){
case "Register/Edit Information":
String [] secondMenu = {"Register", "Edit", "Delete", "Back"};

do{
choice2=(String)JOptionPane.showInputDialog(null, "Please choose:", "Election 2765", 1, null, secondMenu, secondMenu[0]);
switch(choice2){
case "Register":
Register();
break;
case "Edit":
break;
case "Delete":
break;
}
}while(choice2!="Back");

break;
case "Voters List":

break;
case "Voters Validation and Candidate Voting":

break;
}
}while(choice!="Exit");

我的 do while 循环在这里有效。只有 Register 方法不起作用。

最佳答案

您不能将字符串与 == 进行比较(这意味着 != 也是错误的)。

使用等于。

selectYN!="2" 替换为 !"2".equals(selectYN) (注意前面有一个 ! 来否定结果)

关于java - 做而行不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com