2016-02-27 43 views
1

,所以我有這樣的代碼爲我投票系統做而不會工作

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時,它會回到它的菜單。這是菜單的代碼

String choice,choice2; String [] firstMenu = {「Register/Edit Information」,「Voters List」,「Voters Validation and Candidate Voting」,「Exit」};

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循環這裏工作,tho。只有註冊方法不起作用。

+0

這是關於Java還是JavaScrip? – Burkhard

+0

@Burkhard哦,它是爪哇hehehe對不起 – bruh

回答

2

您無法將字符串與==比較(這意味着!=也是錯誤的)。

改爲使用equals。

更換由!"2".equals(selectYN)selectYN!="2"(請注意,有一個前否定的結果!)

+0

哦,非常感謝你的作品!但爲什麼第一個菜單工作?我的意思是它也是一個字符串。我之前做過,並且工作。我甚至認爲它不起作用的原因是我使用了BufferedWriter – bruh

0

插入線:

alert(selectYN); 

就在去年,同時,以確保你所得到的值。