Java - 用于菜单选择的循环(基于控制台的程序)

时间:2012-11-27 02:58:51

标签: java

更新 用户第一次做出诸如“1”的选择时,菜单再次显示。下次进行选择时,支付信息开始循环。循环完成后再次显示菜单,它可以正常工作。此外,当选择开始循环时,前两年输出而不是仅输出第一年,而是按预期一次输出一年。

//create scanner object for choosing a loan, then prompt for and accept input
    Scanner choose = new Scanner(System.in);
    String choice;
    System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
    choice = choose.next();

    //cycle loan 1 payment information
    //create scanner object to advance to the next year's payments

    //loop for cycling payment information
    //initialize loan principal to variable

    while (!"end".equals(choice)) {
        System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
        choice = null;
        choice = choose.next();
        if ("1".equals(choice)) {
           //calculation code
                    }
                if (j < 6) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
        if ("2".equals(choice)) {
            //calculation code
                }
                if (j < 14) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
        if ("3".equals(choice)) {
            //calculation code
                }
                if (j < 29) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = next.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
    }
    choose.close();
}

}

4 个答案:

答案 0 :(得分:4)

我可以预先看到三个问题:

  1. System.in流不需要两个扫描程序。删除此语句Scanner next = new Scanner(System.in);并使用choose实例。

  2. 如果您将输入扫描程序关闭为next.close();,它也会关闭您的输入流System.in,您可能无法再次读取该流。 请确保仅在完成程序后关闭流。

  3. 使用equals方法将while中的条件与while(!"end".equals(choice))进行比较。将文字"end"作为第一个参数将处理choice的空值。

  4. 编辑:

    您修改后的高级代码:

        Scanner choose = new Scanner(System.in);
        String choice= null;
        int j = 0;
        while (!"end".equals(choice)) {
            System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
            choice = choose.nextLine();
            if ("1".equals(choice)) {
               //calculation code
                   if (j < 6) {
                        System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                        choice = choose.nextLine();
                    } else {
                        System.out.println("Congratulations, your mortgage has been paid off.");
                    }
                choice = null;
            }
            if ("2".equals(choice)) {
                    if (j < 14) {
                        System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                        choice = choose.nextLine();
                    } else {
                        System.out.println("Congratulations, your mortgage has been paid off.");
                    }
                choice = null;
            }
            if ("3".equals(choice)) {
                    if (j < 29) {
                        System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                        choice = choose.nextLine();
                    } else {
                        System.out.println("Congratulations, your mortgage has been paid off.");
                    }
                choice = null;
            }
        }
        choose.close();
    

答案 1 :(得分:1)

一个错误是字符串相等与==不同。你应该使用.equals():

while (!choice.equals("end")) {

希望有所帮助!

答案 2 :(得分:1)

您将choice设置为null,因此choice != "end"始终为真。 将next.close(); choice = null移到外面 while循环。 另外,weolfe91说的是什么。

答案 3 :(得分:1)

import java.util.Scanner;

class Main

{

public static void main(String [] args)

  {

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter no.1:");

    int a=sc.nextInt();

    System.out.println("Enter no.2:");

    int b=sc.nextInt();

    boolean exit=false;

    do
    {
      System.out.println("1.addition");
      System.out.println("2.subtraction");
      System.out.println("3.multiplication");
      System.out.println("4.division");
      System.out.println("5.exit");
      System.out.println("choose one!");
      Scanner sd=new Scanner(System.in);
      System.out.println("enter your choice");
      int num=sd.nextInt();
      switch(num)
     {
       case 1:
       int add=a+b;
       System.out.println("addition="+add);
       System.out.println("\n");
       break;

       case 2:
       int sub=a-b;
       System.out.println("subtraction="+sub);
       System.out.println("\n");
       break;

       case 3:
       int mul=a*b;
       System.out.println("multilpication="+mul);
       System.out.println("\n");
       break;

       case 4:
       int div=a/b;
       System.out.println("division="+div);
       System.out.println("\n");
       break;

       case 5:
       exit=true;
       break;
      }
    }while(!exit);
}

}