Java将扫描器传递给方法 [英] Java passing scanner into method
问题描述
因此,我正在研究一些基本的编程思想,以帮助我掌握Java的知识,我创建了一个程序,该程序可以将PI打印到小数点后十位(如果需要,我可以添加更多内容). /p>
但是,我决定走一个额外的步骤,并创建一个选项来使程序不断运行,直到用户告诉它停止为止.我创建了一个返回true(再次运行)或false(退出程序)的方法.最初,我在该方法中创建了一个扫描仪以接受用户输入,并且程序以这种方式运行良好,但是它告诉我我没有在该方法中关闭扫描仪,导致资源泄漏.
我刚刚将输入扫描程序从main传递给方法作为参数,但是当我运行该程序时,它不接受用户输入,并且会打印出对不起,有一个错误"(else {}选项)在我的方法中的if-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
而言,它仍在缓冲区中.
这意味着,对于输入2\n
,它将读取2作为下一个整数,但是读取一个空字符串以调用nextLine()
.
要克服这一点,请在读取下一个整数后使用input.nextLine()
完成对行的使用.
decPlace = input.nextInt();
input.nextLine();
So I'm going through a bunch of basic programming ideas to help me get the hang of Java and I created a program that will print PI up to the 10th decimal place (and I can add more if I want).
However, I decided to go an extra step and create an option to keep running the program over and over until the user tells it to stop. I created a method to return either true (run again) or false (exit program). Originally I created a scanner in the method to take user input, and the program runs fine that way, but it tells me I have a resource leak because I didn't close the scanner in the method.
I have just passed the input scanner from main to the method as a parameter, but when I run the program it doesn't accept user input and will print out "Sorry, there was an error" (the else{} option in the if-else statement in my method). Now, I can go back and create a seperate scanner but my OCD doesn't want Eclipse telling me there is a resource leak (I think input.close() closes both scanners, but I am not sure).
Here is my code, I apologize to any Java enthusiasts who become smitten and offended at any bad practices I am unaware of, I am learning.
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;
}
}
}
If anyone could tell me why it is doing this, I would appreciate it. I'm new to Java (decent with C/C++/C# and Python). I didn't see other questions about this specific problem, and it's not a big deal if I just create another scanner in the method.
I notice that you're doing this call:
decPlace = input.nextInt();
The return character isn't consumed, so it is still on the buffer as far as the Scanner
is concerned.
This means, for an input of 2\n
, it will read the 2 as the next integer, but read an empty string for the call to nextLine()
.
To get past this, finish consuming the line by using input.nextLine()
after you read the next integer.
decPlace = input.nextInt();
input.nextLine();
这篇关于Java将扫描器传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!