【问题标题】:do while won't work [duplicate]做而不会工作[重复]
【发布时间】:2016-02-27 08:23:50
【问题描述】:

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

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 方法不起作用。

【问题讨论】:

  • 这是关于 Java 还是 JavaScrip?!
  • @Burkhard 哦,这是 java 呵呵,对不起

标签: java loops do-while


【解决方案1】:

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

改用等于。

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

【讨论】:

  • 哦,非常感谢它的工作!但为什么第一个菜单有效?我的意思是它也是一个字符串。我以前这样做过,它奏效了。我什至认为它不起作用的原因是我使用了 BufferedWriter
【解决方案2】:

插入行:

    alert(selectYN);

就在不久前确定你得到的价值。

【讨论】:

    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 2014-04-18
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多