System.out.println()在while循环期间打印两次

时间:2015-01-09 00:38:23

标签: java if-statement while-loop

我创建了一个while循环来为我的程序创建一个菜单,它会一直循环,直到用户在控制台中输入'Quit'。我的问题是,无论何时我在控制台中输入'Transfer',if语句都会完美运行,但当它返回循环开始时,System.out.println();消息会打印两次而不是一次。任何其他选项都不会发生这种情况。

代码:

int x = 0;
while(x != 1) {
    System.out.println("To transfer funds, please type 'Transfer'\n" //Typical message
                + "To list recent transactions, please type 'Recent'\n"
                + "To display account details, please type 'Details'\n"
                + "To quit the application, please type 'Quit'");

    String Option1 = "Transfer"; //Variables
    String Option2 = "Recent"; 
    String Option3 = "Details";
    String Option4 = "Quit";
    String Menu = ConsoleInput.nextLine();

    if (Menu.equals(Option1)) {
            //Statements
    } else if(Menu.equals(Option2)){
            //Statements
    } else if(Menu.equals(Option3)){
            //Statements
    } else if(Menu.equals(Option4)){
            //End statement
            x++;
    }
}

编辑:有些人正在评论最后一个else语句中x的增量,并说它应该放在else语句之外。它就在那里,因为当用户在控制台中键入“Quit”时它将退出循环,如果它被放在else语句之外,那么用户将无法访问其他选项。

编辑2(解决方案):我终于解决了问题。原来它是if语句中的扫描程序语句。我应该已经显示了所有人都可以看到的完整代码,但我认为这只是if-else语句本身。

2 个答案:

答案 0 :(得分:0)

需要注意的几件事情:

  1. 您应该Menu.equalsIgnoreCase(Option1)等。
  2. 你应该在while循环之后加上x的增量。除非输入End,否则永远不会递增x。
  3. 我认为你真的想要更像

    的东西
    private static String[] options = {"Transfer","Recent","Details","Quit"};
    private static int C_Transfer = 0;
    private static int C_Recent = 1;
    private static int C_Details = 2;
    private static int C_Quit = 3;
    private static int[] optionCase = {C_Transfer, C_Recent, C_Details,C_Quit);
    private String menu;
    private String message = "To transfer funds, please type 'Transfer'\n" //Typical message
                    + "To list recent transactions, please type 'Recent'\n"
                    + "To display account details, please type 'Details'\n"
                    + "To quit the application, please type 'Quit'"
    
    System.out.println(message);
    menu = ConsoleInput.nextLine()
    while (!menu.equalsIgnoreCase(options[C_Quit]) {
        int case = -1;
        for (int i=0, i < options.length; i++) {
          if (menu.equalsEgnoreCase(options[i]) {
              case = i;
              break;
          }
          switch (case) 
            case C_Transfer: {
               break;
            } 
            case C_Transfer: {
               break;
            } 
            case C_Transfer: {
               break;
            } 
            case C_Transfer: {
               break;
            }
            default: { 
               System.out.println(message);
            }
            Menu = ConsoleInput.nextLine();
         }
    }
    

答案 1 :(得分:0)

这只发生在转移选项上吗?你能展示一下转移选项的代码吗?可能有指令在那里打印消息,所以当循环重新开始时你打印了两次消息。

添加: 根据您的转移代码&#39;。似乎这个问题与java.util.Scanner的错误使用有关,但我自己也不确定这个实用程序类。但是下面修改后的代码对我有用:

public static void main(String[] args){
        java.util.Scanner ConsoleInput=new java.util.Scanner(System.in);
        String Menu;
        String message="To transfer funds, please type 'Transfer'\n" //Typical message
                + "To list recent transactions, please type 'Recent'\n"
                + "To display account details, please type 'Details'\n"
                + "To quit the application, please type 'Quit'";
        System.out.println(message);
                String Option1 = "Transfer"; //Variables
        String Option2 = "Recent"; 
        String Option3 = "Details";
        String Option4 = "Quit";
        while(!((Menu=ConsoleInput.nextLine()).equalsIgnoreCase(Option4))){
            if(Menu.equalsIgnoreCase(Option1)){
                System.out.println("You have selected transfer");
                System.out.println("How much would you like to transfer?");
                float deduction;
                float total = 2500;
                deduction = ConsoleInput.nextFloat();
                total = total - deduction;
                System.out.println("£" + total + " left in account. Transaction recorded.");
                //Fileoutput.println(sf.format(date) + " - £" + deduction + " taken from account. £" + total + " left in account.\n");  
                System.out.println(message);
            }else if(Menu.equalsIgnoreCase(Option2)){
                System.out.println("here is your balance...blah, blah, bbla\n");
                System.out.println(message);
            }else if(Menu.equalsIgnoreCase(Option3)){
                System.out.println("here is the details...blah, blah, bbla\n");
                System.out.println(message);
            }
        }
        System.out.println("Thanks");
    }