使用扫描仪获取用户输入
我试图让扫描仪在循环中输入。 一旦用户想要完成,他就可以退出这个循环。 我尝试了很多不同的方法,但总有一些问题。 这是代码:
private void inputEntries() { Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number = sc.nextInt(); Student student = new Student(name, surname, number); students.add(student); try { addToFile(student); } catch (Exception ex) { Logger.getLogger(TextReader.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Continue?[Y/N]"); } }
上面代码的问题,也发生在我尝试的不同方法上,当用户键入Y时 , Scanner
将跳过第一个名字输入,并跳转到姓氏。 如果用户键入N,则循环正确停止。 有人可以解释这种情况发生的原因,以及如何克服使用Scanner
类?
ps:做一些像while(sc.nextLine().equals("Y"))
这样的事情会导致循环终止,然后在第一次循环运行后从用户那里获得输入。
这是因为您正在使用Scanner#next
方法。 如果你查看该方法的文档,它将返回下一个标记读取。
因此,当您使用next
方法读取用户输入时,它不会在最后读取newline
。 然后由while
循环中的nextLine()
读取。 因此,您的firstName
包含一个不知道的newline
。
所以,你应该在你的while而不是next()
使用nextLine()
next()
。
与nextInt
方法类似。 它也不会读取换行符。 因此,您可以使用readLine
read
并使用Integer.parseInt
将其转换为int
。 如果输入值无法转换为int
,则可能抛出NumberFormatException
。 所以你需要相应地处理它。
您可以尝试以下代码: –
Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number = 0; try { number = Integer.parseInt(sc.nextLine()); } catch (IllegalArgumentException e) { e.printStackTrace(); } System.out.println("Continue?[Y/N]"); }
但是,请注意一件事,如果输入一个无法传递给Integer.parseInt
的值,您将获得一个exception,并且将跳过该输入。 对于这种情况,您需要使用while
循环来处理它。
或者,如果您不想进行exception处理 : –
你可以在empty sc.nextLine()
之后添加一个empty sc.nextLine()
,这将消耗剩下的newline
,如下所示: –
// Left over part of your while loop String surname = sc.nextLine(); System.out.println("Enter number"); int number = sc.nextInt(); sc.nextLine(); // To consume the left over newline; System.out.println("Continue?[Y/N]");
- 使用
equalsIgnoreCase(..)
来防止Y或N为小写,反之亦然。 - 不要使用
sc.nextLine()
sc.next()
而不是sc.nextLine()
来实现你想要的,因为next()
只读取下一个空格,而nextLine()
读取到下一个\n
中断。 - 不要使用
nextInt()
将所有数据读取为String
然后转换,因为nextInt()
就像next()
因此不会读取到下一个\ n 。
试试这样:
Scanner sc = new Scanner(System.in); System.out.println("Continue?[Y/N]"); while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here System.out.println("Enter first name"); String name = sc.nextLine(); System.out.println("Enter surname"); String surname = sc.nextLine(); System.out.println("Enter number"); int number=0; try { number = Integer.parseInt(sc.nextLine());//read int as string using nextLine() and parse }catch(NumberFormatException nfe) { nfe.printStackTrace(); } System.out.println("Continue?[Y/N]"); }
你可以用
String abc = "abc"; String answer = "null"; while (abc.equals("abc") { if (answer.equals("n") { break; } while (answer.equals("y")) { //Put your other code here System.out.print("Continue? [y/n]") answer = input.nextLine(); if (answer.equals("n") { break; } } }
这应该在输入时停止程序
"n"
您还必须像这样导入和创建扫描仪:
//This is how you import the Scanner import java.util.Scanner; //This is how you create the Scanner Scanner input = new Scanner(System.in); /** *The name */ "input" /** *is used because of */ input.nextLine
public void display() { int i, j; for (i = 1; i <= 5; i++) { for (j = i; j < 5; j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(i % 2); } for (j = 1; j < ((5 * 2) - (i * 2)); j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(j); } System.out.println(); } for (i = 4; i >= 1; i--) { for (j = i; j < 5; j++) { System.out.print(" "); } for (j = 1; j <= i; j++) { System.out.print(j); } for (j = i - 1; j >= 1; j--) { System.out.print(j); } for (j = 1; j < ((5 * 2) - (i * 2)); j++) { System.out.print(" "); } for (j = 1; j < (2 * i); j++) { System.out.print(j); } System.out.println(); } }