难以显示 java 中的输入字符串
Difficulty in displaying the input string in java
我在 Hackerrank 上解决一个问题,我必须输入一个整数、一个双精度数和一个字符串,然后使用标准输出打印结果。
输入
42
3.1415
Welcome to HackerRank's Java tutorials!
输出
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
我写了下面的代码,但无法通过我的测试用例:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String ss = s.nextLine();
int i = s.nextInt();
double d = s.nextDouble();
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
代码有什么问题吗?
该问题还指出:-
Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).
以下是我的输出:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Solution.main(Solution.java:8)
谁能解释一下?
诀窍是在获取 String
之前添加 s.nextLine();
因为它将读取带有数字的行的其余部分(我怀疑数字后没有任何内容)
如果您打算忽略该行的其余部分,请尝试在每个 nextInt()
之后放置一个 s.nextLine();
。
所以完整的代码就像:
Scanner s = new Scanner(System.in);
int i = s.nextInt();
double d = s.nextDouble();
s.nextLine();
String ss = s.nextLine(
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
我在 Hackerrank 上解决一个问题,我必须输入一个整数、一个双精度数和一个字符串,然后使用标准输出打印结果。
输入
42
3.1415
Welcome to HackerRank's Java tutorials!
输出
String: Welcome to HackerRank's Java tutorials!
Double: 3.1415
Int: 42
我写了下面的代码,但无法通过我的测试用例:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String ss = s.nextLine();
int i = s.nextInt();
double d = s.nextDouble();
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
代码有什么问题吗? 该问题还指出:-
Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).
以下是我的输出:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Solution.main(Solution.java:8)
谁能解释一下?
诀窍是在获取 String
之前添加 s.nextLine();
因为它将读取带有数字的行的其余部分(我怀疑数字后没有任何内容)
如果您打算忽略该行的其余部分,请尝试在每个 nextInt()
之后放置一个 s.nextLine();
。
所以完整的代码就像:
Scanner s = new Scanner(System.in);
int i = s.nextInt();
double d = s.nextDouble();
s.nextLine();
String ss = s.nextLine(
System.out.println("String: "+ss);
System.out.println("Double: "+d);
System.out.println("Int: "+i);