Java将扫描器传入方法



所以我要通过一些基本的编程思想来帮助我掌握Java的窍门,我创建了一个程序,将PI打印到小数点后10位(如果我想的话,我可以添加更多)。

然而,我决定采取额外的步骤,创建一个选项,让程序一遍又一遍地运行,直到用户告诉它停止。我创建了一个方法来返回true(再次运行)或false(退出程序)。最初,我在方法中创建了一个扫描器来接受用户输入,程序以这种方式运行得很好,但是它告诉我有资源泄漏,因为我没有关闭方法中的扫描器。

我刚刚将输入扫描器作为参数从main传递给方法,但是当我运行程序时,它不接受用户输入,并将打印出"对不起,有一个错误"(在我的方法中的if-else语句中的else{}选项)。现在,我可以回去创建一个单独的扫描器,但是我的OCD不希望Eclipse告诉我有资源泄漏(我认为input.close()关闭了两个扫描器,但我不确定)。

这是我的代码,我向那些被我不知道的任何坏做法所震惊和冒犯的Java爱好者道歉,我正在学习。

 import java.util.Scanner;
 import java.text.DecimalFormat;
 public class PiDecimalFormat {
     public static void main(String[] args) {
         Scanner input = new Scanner(System.in);
         DecimalFormat format = new DecimalFormat("#");
         int decPlace = 0;
         boolean runProgram = true;
         System.out.println("This program will print out PI to the decimal place of your choosing.");
         while (runProgram == true) {
             System.out.print("nEnter the number of decimal places (up to 10) that nyou would like to print PI to: ");
             decPlace = input.nextInt();
             switch (decPlace) {
                 case 0:
                     format = new DecimalFormat("#");
                     break;
                 case 1:
                     format = new DecimalFormat("#.#");
                     break;
                 case 2:
                     format = new DecimalFormat("#.##");
                     break;
                 case 3:
                     format = new DecimalFormat("#.###");
                     break;
                 case 4:
                     format = new DecimalFormat("#.####");
                     break;
                 case 5:
                     format = new DecimalFormat("#.#####");
                     break;
                 case 6:
                     format = new DecimalFormat("#.######");
                     break;
                 case 7:
                     format = new DecimalFormat("#.#######");
                     break;
                 case 8:
                     format = new DecimalFormat("#.########");
                     break;
                 case 9:
                     format = new DecimalFormat("#.#########");
                     break;
                 case 10:
                     format = new DecimalFormat("#.##########");
                     break;
             }
             System.out.println("nThe value of PI to " + decPlace + " decimal places is " + format.format(Math.PI) + ".");
             runProgram = AskRunAgain(input);
         }
         input.close();
     }
     static boolean AskRunAgain(Scanner askUser) {
         String userChoice;
         System.out.print("nWould you like to run the program again? [y/n]: ");
         userChoice = askUser.nextLine();
         if ((userChoice.equals("y")) || (userChoice.equals("Y")) || (userChoice.equals("yes")) || 
            (userChoice.equals("Yes")) || (userChoice.equals("YES"))) { 
             return true;
         }
         else if ((userChoice.equals("n")) || (userChoice.equals("N")) || (userChoice.equals("no")) || 
            (userChoice.equals("No")) || (userChoice.equals("NO"))) {
             System.out.println("nExitting the program. have a good day!");
             return false;
         }
         else {
             System.out.println("Sorry, there was an error.");
             return false;
         }
     }
 }

如果有人能告诉我为什么这样做,我会很感激。我是Java的新手(对C/c++/c#和Python还不错)。我没有看到关于这个特定问题的其他问题,如果我在方法中创建另一个扫描仪,这不是什么大问题。

我注意到你在做这个调用:

decPlace = input.nextInt();

返回字符没有被消耗,因此就Scanner而言,它仍然在缓冲区中。

这意味着,对于2n的输入,它将读取2作为下一个整数,但为调用nextLine()读取空字符串。

要解决这个问题,在读取下一个整数后使用input.nextLine()完成对该行的消耗。

decPlace = input.nextInt();
input.nextLine();

只要改一下就行了

askUser.nextLine();

askUser.next ();

static boolean AskRunAgain(Scanner askUser) {
        String userChoice;
        System.out.print("nWould you like to run the program again? [y/n]: ");
        userChoice = askUser.next();

最新更新