簡體   English   中英

如何在方法中重定向到主程序?

[英]How to redirect to main program in methods?

我正在嘗試創建一種方法,如果用戶輸入 Y,它會將他們定向到主程序。 問題是當用戶輸入 Y 時,它會再次打印介紹。 當用戶輸入 N 時,它會打印出“好吧,再見,書呆子”。 以及主程序。

我試過使用 main(); 和主要(空);

//scanning for input
    userInput = scan.next().charAt(0);

    //switch for user input
    switch (userInput){

    case 'Y': main(null);
        break;

    case 'N': System.out.println("Alright then, goodbye you NERD.");
        break;

    default: System.out.println("Invalid input, dude. Run this program again and");
    System.out.println("either type a capital Y for yes or a capital N for no.");
        break;

    }//end of switch for user input

使用 Scanner 的 nextLine() 方法,或者在每個輸入請求中創建一個新的 Scanner,因為 scan 使用的是之前的輸入

userInput = new Scanner(System.in).nextLine().charAt(0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM