java - 我给定的代码不打印最后一个输入值。代码有什么问题吗?

标签 java

import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the film");
        String str = s.nextLine();
        System.out.println("Enter the film budget");
        int a = s.nextInt();
        System.out.println("Enter the running time");
        double b = s.nextDouble();
        System.out.println("Enter the film production house");
        String str1 = s.nextLine();
        System.out.println("Film being screened : " + str);
        System.out.printf("Budget : %d crores", a);
        System.out.printf("\nRunning time : %.2f hours", b);
        System.out.printf("\nProduction : %s", str1);
    }
}

我正在获取字符串、整数、 double 和字符串的输入。但是,我无法获取我输入的“生产”值。

最佳答案

System.out.println("Enter the film production house");
s.nextLine();
String str1 = s.nextLine();

这是因为 s.nextDouble() 不会消耗输入的最后一个换行符,因此 str1 被分配一个“\n”(换行)值

关于java - 我给定的代码不打印最后一个输入值。代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47573120/

相关文章:

java - 在 git update-index --assume-unchanged 之后 merge 的问题

java - 为什么不能在 android 中用工具栏覆盖操作栏?

java - 想要将 ArrayList<HashMap<String, Object>> 转换为字符串

Java Eclipse 操作系统错误

java - 如何将JFreeChart库添加到JDK?错误: package org. jfree.chart不存在

java - 如何停止并重新启动 Android 定位服务

java - 资源关闭异常

java - 你把java中外部库的.jar文件放在哪里

java - Activity 指示器和线程黑莓

java - Mockito NullPointerException 在 InvocableMatcher 上调用 equals