2015-04-29 132 views
0

我正在尝试使我在网上学到的东西变成一个项目。招生系统

public static void main(String[]args){ 

    int balance, payment; 


    balance = 11310; 

    String new1 = "new"; 

    String old = "old"; 

    String partial = "partial"; 

    String full = "full"; 

    String no = "no"; 

    String yes = "yes"; 

    String First = "first year second sem"; 

    String Second = "second year first sem"; 

    String Third = "second year second sem"; 

    System.out.print("\nEnter Name: "); 

    Scanner st = new Scanner(System.in); 

    String name = st.nextLine(); 

    System.out.print("\nOld Student or New Student: "); 

    String student = st.nextLine(); 

    if(student.equalsIgnoreCase(new1)){ 

     System.out.print("\nGreetings, "+name); 

     System.out.print("\nPlease fill out our Registration Form."); 

     System.out.print("\nThank you!"); 

    try { 

    if(Desktop.isDesktopSupported()) { 

     Desktop.getDesktop().open(new File("C:\\Users\\toshiba-1\\Desktop\\Regform1styr.2ndsem - Copy.docx")); 

    } 

    } 

    catch (IOException ioe) { 

    ioe.printStackTrace(); 

    } 

    return; 

    } 


    System.out.print("\nGreetings, "+name); 

    System.out.print("\nWould you want to enroll for a new semester? "); 

    String enroll = st.nextLine(); 

    if(enroll.equalsIgnoreCase(no)) 
    { 
    System.out.print("\nThank you!"); 

    return; 
    } 

    if(enroll.equalsIgnoreCase(yes)) 
    { 

    } 

    System.out.print("\nWhich Semester? "); 

    String semester = st.nextLine(); 

    if(semester.equalsIgnoreCase(First)) // My error started here. 
    { 
    } 

    else if(semester.equalsIgnoreCase(Second)) 
    { 
    } 

    else if(semester.equalsIgnoreCase(Third)) 
    { 
    } 

    else 
    { 
    System.out.print("\nPlease choose a semester."); 
    } 

    System.out.print("\nThe Enrollment fee for this Semester is: "+ balance); 

    System.out.print("\nChoose your Payment term(Partial/Full): "); 

    String term = st.nextLine(); 

    if(term.equalsIgnoreCase(partial)) 
    { 

     System.out.print("\nHow much would you like to pay for this semester: "); 

     Scanner in = new Scanner(System.in); 

     payment = in.nextInt(); 


     balance = balance - payment; 

     System.out.print("\nYour Balance is: SR"+ balance); 

     if(balance > payment){ 

      System.out.print("\nYou have remaining balance of: SR" + balance); 

     }else if(balance == payment){ 

      System.out.print("\nYou are already paid"); 

     } 

    } 



} 
} 

我在用户选择三个字符串时遇到了麻烦。我替换了这组代码。

if(semester.equalsIgnoreCase(First)) 
    { 
    System.out.print("\nPlease choose a semester."); 
    } 

    System.out.print("\nThe Enrollment fee for this Semester is: "+ balance); 

    System.out.print("\nChoose your Payment term(Partial/Full): "); 

    String term = st.nextLine(); 

    if(term.equalsIgnoreCase(partial)) 
    { 

     System.out.print("\nHow much would you like to pay for this semester: "); 

     Scanner in = new Scanner(System.in); 

     payment = in.nextInt(); 


     balance = balance - payment; 

     System.out.print("\nYour Balance is: SR"+ balance); 

     if(balance > payment){ 

      System.out.print("\nYou have remaining balance of: SR" + balance); 

     }else if(balance == payment){ 

      System.out.print("\nYou are already paid"); 
    } 

    else if(semester.equalsIgnoreCase(Second)) 
    { 
    System.out.print("\nPlease choose a semester."); 
    } 

    System.out.print("\nThe Enrollment fee for this Semester is: "+ balance); 

    System.out.print("\nChoose your Payment term(Partial/Full): "); 

    String term = st.nextLine(); 

    if(term.equalsIgnoreCase(partial)) 
    { 

     System.out.print("\nHow much would you like to pay for this semester: "); 

     Scanner in = new Scanner(System.in); 

     payment = in.nextInt(); 


     balance = balance - payment; 

     System.out.print("\nYour Balance is: SR"+ balance); 

     if(balance > payment){ 

      System.out.print("\nYou have remaining balance of: SR" + balance); 

     }else if(balance == payment){ 

      System.out.print("\nYou are already paid"); 
    } 

    else if(semester.equalsIgnoreCase(Third)) 
    { 
    System.out.print("\nPlease choose a semester."); 
    } 

    System.out.print("\nThe Enrollment fee for this Semester is: "+ balance); 

    System.out.print("\nChoose your Payment term(Partial/Full): "); 

    String term = st.nextLine(); 

    if(term.equalsIgnoreCase(partial)) 
    { 

     System.out.print("\nHow much would you like to pay for this semester: "); 

     Scanner in = new Scanner(System.in); 

     payment = in.nextInt(); 


     balance = balance - payment; 

     System.out.print("\nYour Balance is: SR"+ balance); 

     if(balance > payment){ 

      System.out.print("\nYou have remaining balance of: SR" + balance); 

     }else if(balance == payment){ 

      System.out.print("\nYou are already paid"); 
    } 

    else 
    { 
System.out.print("Please choose a semester."); 

return; 
     } 

但结果不断混淆。请帮帮我。

+1

我不太明白这个问题。你能提供你目前的错误输出并告诉我们应该发生什么吗? – Sossenbinder

+0

对不起。在命令中... System.out.print(“\ nWhich Semester?”); String semester = st.nextLine(); if(semester.equalsIgnoreCase(First))//我的错误从这里开始。 {} 否则,如果(semester.equalsIgnoreCase(二)){ } 否则,如果(semester.equalsIgnoreCase(三)){ } 其他 { 是System.out.print(“\请选择一个学期。“); } 当用户输入“第一”,“第二”或“第三”时,它将继续“本学期的注册费用......”如果不是,则会说“请选择一个学期”,并且iw会返回。 – Albert

+0

当你想跳进你的第一个if子句时,输入是否正好是“第一年第二个sem”?因为这就是你所比较的。当你只输入“第一个”时,它不等于你保存在变量中的那个字符串 – Sossenbinder

回答

0

如果你希望你的代码工作,你需要添加环路问题:

System.out.print("\nWhich Semester? "); 
// changed code!! 
while(true){ 
    String semester = st.nextLine(); 
    if(semester.equalsIgnoreCase(First)){ 
     break; 
    }else if(semester.equalsIgnoreCase(Second)){ 
     break; 
    }else if(semester.equalsIgnoreCase(Third)){ 
     break; 
    }else{ 
     System.out.print("\nPlease choose a semester."); 
    } 
} 
//end of chaged code!! 
System.out.print("\nThe Enrollment fee for this Semester is: "+ balance); 

然而,这是非常糟糕的主意,对像你这样的字符串基本程序流控制。