这是我的代码:
public class Solution {
public static void main(String[] args) {
/*
* Enter your code here. Read input from STDIN.
* Print output to STDOUT.
* Your class should be named Solution.
*/
int num = 0;
double dou = 0.0;
String s = null;
Scanner in = new Scanner(System.in);
if (in.hasNextInt()) {
num = in.nextInt();
}
Scanner d = new Scanner(System.in);
if (d.hasNextDouble()) {
dou = d.nextDouble();
}
Scanner str = new Scanner(System.in);
if (str.hasNextLine()) {
s = str.nextLine();
}
System.out.println("String:" + s);
System.out.println("Double:" + dou);
System.out.println("Int:" + num);
}
}
我得到这个输出:
字符串:空
双倍:0.0
智力:42
但它应该看起来像这样:
String:欢迎来到 Hackerrank Java 教程!
双倍:3.1415
智力:42
谁能解释一下为什么我得到字符串的
null
值和双精度值 0.0
?
//你的答案
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
double d = in.nextDouble();
in.nextLine(); // finishes the previous line
String s = in.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
您不应该使用 3 个扫描仪,一台就足够了。请参阅扫描仪方法打开和关闭两次。
除此之外,当仅使用一个时,仍然可能出现如下问题: Java String Scanner 输入不等待信息,直接移至下一条语句。如何等待信息?
我测试的时候效果很好。你如何输入你的浮动? 3.1415 还是 3,1415 ?根据您当地的情况,一个将是红色的,另一个则不是。
如果希望结果在一行中:
String chaine= String.format("String: %s. Double: %.5f. Int: %d", s,dou,num);
System.out.println(chaine);
这个解决方案有效
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
double d = scanner.nextDouble();
scanner.nextLine();
String s = scanner.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
scanner.close();
}
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();
String s = scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
你可以做这样的事情。对于每个新行,应该调用 Scanner.nextLine() 。
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
String s="";
Double d=0d;
if(scan.hasNextLine()){
scan.nextLine();
if(scan.hasNextDouble()){
d=scan.nextDouble();
scan.nextLine();
s=scan.nextLine();
}else{
s=scan.nextLine();
scan.nextLine();
d=scan.nextDouble();
}
}
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);
}
我用这些修复了它,这是我的代码
int i = scan.nextInt();
Double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
嘿这个错误是因为java没有像C那样的fflush(),因此当您在双精度或整数值后按回车键时,缓冲区包含空的回车键,因此当您使用nextLine()打印时,它会打印该行是空的,因此发生错误,因此您必须使用 sc.nextLine() 函数将其调用到下一行,因此将显示正确的输出
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s=scan.next();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}