为什么我们无法在以下代码中为字符串s1提供输入?

时间:2018-03-28 08:57:18

标签: java string java.util.scanner

import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name of the film");
    String name = in.nextLine();
    System.out.println("Enter the film budget");
    int bud = in.nextInt();
    System.out.println("Enter the running time");
    double time = in.nextDouble();
    System.out.println("Enter the film production house");
    String s1 = in.nextLine(); 
    System.out.println("Film being screened : "+name);
    System.out.println("Budget "+bud+" cores");
    System.out.println("Running time "+time+" hours");
    System.out.println("Production : "+s1);
}
}

在上述程序的输出中,它不接受String s1的输入。 我如何解决它? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

尝试添加以下附加行:

in.nextLine();

之后:

double time = in.nextDouble();

使用换行符号(当您接受带输入键的输入double时创建的>未使用.nextDouble()来电

请参阅 LenosV 提供的that question链接,以获取有关Scanner行为原因的详细信息。

答案 1 :(得分:0)

这将有效

import java.util.*;
public class MovieInfo
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name of the film");
    String name = in.nextLine();
    System.out.println("Enter the film budget");
    int bud = in.nextInt();
    System.out.println("Enter the running time");
    double time = in.nextDouble();
    System.out.println("Enter the film production house");
    String s1 = in.next(); 
    System.out.println("Film being screened : "+name);
    System.out.println("Budget "+bud+" cores");
    System.out.println("Running time "+time+" hours");
    System.out.println("Production : "+s1);
}
}