java - 做而行不通

标签 java loops do-while

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

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/

相关文章:

java - ExecutorCompletionService 缺少 invokeAll 接口(interface)

java - fmt :message adding unintentional comma into number

python - 如何在猜谜游戏中创建猜测计数器

尝试在 xcode 中退出程序时出现 C++ 11db 错误。初学者级

JAVA:在 Do While 中使用 String 进行决策

java - 斯坦福解析器 : Get Integer value for CARD?

java - 访问 jar 文件中的报告时出现问题

java - 为什么无论输入是什么它都不会跳出循环

c - 当在循环中突然退出 C 程序时,为什么会发生额外的循环迭代?

javascript - 如何在 Javascript 循环中触发的回调函数中抛出一次错误?