我正在尝试用我在网上学到的东西来制作一个项目。
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;
}
但结果仍然在混淆。请帮帮我。
答案 0 :(得分: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);
然而,基于像你这样的字符串程序流控制是非常糟糕的。