做什么不行

时间:2016-02-27 08:23:50

标签: 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");
}

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

字符串选择,choice2;         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");

我做的while循环在这里工作,所以。只有Register方法不起作用。

2 个答案:

答案 0 :(得分:2)

你无法将字符串与==进行比较(这意味着!=也是错误的)。

改为使用equals。

selectYN!="2"替换为!"2".equals(selectYN)(注意有一个!之前取消结果)

答案 1 :(得分:0)

插入行:

    alert(selectYN);

在最后一次确定您所获得的价值观之前。