[英]I am trying to call my Scanner but its not working. What should i do?
import java.util.Scanner;
public class Words
{
public static void main (String[] args)
{
Scanner myScan = new Scanner(System.in);
String s1;
int myAge;
int time = 6;
System.out.print("What is your name? ");
s1 = myScan.nextLine();
System.out.print("How old are you? ");
myAge = myScan.nextInt();
System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
s1 = myScan.nextLine();
}
}
//最后一條命令執行完后,我將無法在終端窗口中輸入任何內容。 請幫忙。
在之間添加nextLine()
System.out.print("How old are you? ");
myAge = myScan.nextInt();
myScan.nextLine(); // add this
System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
s1 = myScan.nextLine();
這是必需的,因為nextInt()
僅消耗其讀取的int
值,而不消耗其后的任何行尾字符。
nextLine()
將使用\\r\\n
(或行尾/定界符的任何結尾),而下一個標記將可供另一個nextLine()
。
在鍵入int之后鍵入回車。 nextline將以enter作為一行。 您需要在nextint之后添加一個額外的nextline調用,如下所示:
import java.util.Scanner;
public class Words
{
public static void main (String[] args)
{
Scanner myScan = new Scanner(System.in);
String s1;
int myAge;
int time = 6;
System.out.print("What is your name? ");
s1 = myScan.nextLine();
System.out.print("How old are you? ");
myAge = myScan.nextInt();
s1 = myScan.nextLine();
System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
s1 = myScan.nextLine();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.