这里字符串的输入是“嗨,你好吗”,但我得到的输出仅是“嗨”。请让我知道错误在哪里。我也尝试过 nextLine() 。
import java.util.Scanner;
public class Stringintalter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter the num");
int a = sc.nextInt();
System.out.println("Enter the Double");
double b = sc.nextDouble();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
这只是基本错误。我浏览了很多仍然无法解决错误。
最佳答案
每次你这样做
int a = sc.nextInt();
double b = sc.nextDouble();
您的扫描仪未读取newLine token ,因此您应该执行以下操作:
System.out.println("enter the num");
int a = sc.nextInt();
sc.next();
System.out.println("Enter the Double");
double b = sc.nextDouble();
sc.next();
System.out.println("Enter the string");
String c = sc.next(); //tried out with nextLine() also.
关于java - 无法使用扫描仪打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38181653/