简体   繁体   English

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

[英]System.out.println() printing twice during while-loop

I created a while-loop to create a menu for my program, it will keep looping until the user types 'Quit' into the console. 我创建了一个while循环来为程序创建菜单,它将一直循环直到用户在控制台中键入“ Quit”为止。 My problem is that whenever I enter 'Transfer' into the console the if statement runs perfectly but when it goes back to the start of the loop the System.out.println(); 我的问题是,每当我在控制台中输入“ Transfer”时,if语句就会完美运行,但是当它返回循环的开头时, System.out.println(); message prints twice instead of once. 消息打印两次而不是一次。 This doesn't happen with any of the other options. 其他任何选项都不会发生这种情况。

Code: 码:

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++;
    }
}

EDIT: Some of you are commenting about the increment of x in the last else statement and saying that it should be put outside the else statement. 编辑:你们当中有些人在最后一个else语句中评论x的增量,并说应将其放在else语句之外。 It's there because when the user types 'Quit' into the console it will exit the loop, if it was put outside the else statement then the user won't be able to access the other options. 在那里是因为当用户在控制台中输入'Quit'时,它将退出循环,如果将其置于else语句之外,则用户将无法访问其他选项。

EDIT 2 (SOLUTION): I finally fixed the problem. 编辑2(解决方案):我终于解决了问题。 Turns out it was a scanner statement within the if-statement. 原来这是if语句中的扫描程序语句。 I should have shown the full code for you all to see but I thought it was just the if-else-statements themselves. 我应该向所有人展示完整的代码,但是我认为那只是if-else语句本身。

Several things to look out for: 需要注意的几件事:

  1. You should probably Menu.equalsIgnoreCase(Option1) etc. 您可能应该使用Menu.equalsIgnoreCase(Option1)等。
  2. YOu should put the increment of x right after the while loop. 您应该在while循环之后放置x的增量。 You are never incrementing x unless you type End. 除非键入End,否则您永远不会递增x。

I think you really want something more like 我想你真的想要更多类似的东西

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();
     }
}

This only happen on Transfer option right? 这仅在“转移”选项上发生吗? Could you show the code for Transfer options? 您可以显示转移选项的代码吗? probably there's instruction to print the message over there, so you got the message printed twice when loop is start over again. 可能有指令在那儿打印消息,所以当循环重新开始时,您得到了两次打印消息。

add: based on your 'transfer code'. 添加:根据您的“转移码”。 It seems the problem is related with missuse of java.util.Scanner but I myself not sure about this utility class. 看来问题与java.util.Scanner的滥用有关,但我自己不确定该实用工具类。 But the modified code below is work for me: 但是以下修改后的代码对我有用:

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");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM