import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=new String();
int x=sc.nextInt();
double y=sc.nextDouble();
s = sc.next();
System.out.println("String:"+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
}
它只扫描一个单词,请提出任何建议...
答案 0 :(得分:8)
您可以尝试以下代码:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
因为Scanner对象将读取其先前读取停止的行的其余部分。
如果该行没有任何内容,它只会消耗换行符并移动到下一行的开头。
双重声明后,你必须写:scan.nextLine();
答案 1 :(得分:5)
s = sc.next();
它只扫描一个单词,请提出任何建议...
这是next();
的目的。如果您想阅读多个单词,最简单的解决方案是阅读一行文字。 e.g。
String s = sc.nextLine();
如果您需要阅读多行文字,您需要制定一个策略来执行此操作,例如在循环中读取所有内容,直到您有一个空行。
注意:尽管答案类似于Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods,但区别在于您不会丢弃行的其余部分,而是使用{{}返回的字符串1}}
如果您希望输入为
nextLine();
然后你想使用上面的建议,但是如果输入是在这样的多行
1 2.0 Hello World
如上面的链接所示,你需要将额外的课程丢弃到 1 2.0
Hello World
。
答案 2 :(得分:5)
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
double y = sc.nextDouble();
sc.nextLine();
String s = sc.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + y);
System.out.println("Int: " + x);
}
}
答案 3 :(得分:1)
为什么不按以下步骤跳过
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
double d = scan.nextDouble();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
答案 4 :(得分:0)
我建议尝试使用这段代码
Scanner scan = new Scanner();
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
我希望在输入双整数或整数后,不会跳过读取字符串输入(或只是读取跳过其余部分的单个单词)。
关注fb / ayur.sharma
答案 5 :(得分:0)
如果在nextInt()方法之后立即使用nextLine()方法,请记住nextInt()读取整数标记;因此,该行整数输入的最后一个换行符仍在输入缓冲区中排队,下一个nextLine()将读取整数行的剩余部分(为空)。
答案 6 :(得分:0)
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: "+ i);
}
}
答案 7 :(得分:0)
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
scan.nextLine();
String s = scan.nextLine();
scan.close();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: "+ i);
}
}
当在读取输入令牌和读取整行输入之间切换时,您需要再次调用nextLine(),因为Scanner对象将读取其先前读取停止的行的其余部分。
如果该行没有任何内容,它只会消耗换行符并移动到下一行的开头。
双重声明后,你必须写:scan.nextLine();
答案 8 :(得分:0)
这是您的答案
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
//if you want to skip the unnecessary input
//scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
scan.close();
}