Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建一个交互式程序,要求用户提供字符串、字符、int和浮点值。答案打印在不同的行上_Java_Character_Interactive - Fatal编程技术网

Java 创建一个交互式程序,要求用户提供字符串、字符、int和浮点值。答案打印在不同的行上

Java 创建一个交互式程序,要求用户提供字符串、字符、int和浮点值。答案打印在不同的行上,java,character,interactive,Java,Character,Interactive,我如何添加字符?当我添加final char c=console.nextChar()时它给了我一个错误。谢谢你的帮助 错误:无法在项目JavaExercise:Command execution上执行goal org.codehaus.mojo:exec maven plugin:1.5.0:exec(默认cli)失败。错误:进程退出:1(退出值:1)->[帮助1] 要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。 使用-X开关重新运行Maven以启用完全调试日志记录 有关错误和可

我如何添加字符?当我添加
final char c=console.nextChar()时它给了我一个错误。谢谢你的帮助

错误:无法在项目JavaExercise:Command execution上执行goal org.codehaus.mojo:exec maven plugin:1.5.0:exec(默认cli)失败。错误:进程退出:1(退出值:1)->[帮助1]

要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven。 使用-X开关重新运行Maven以启用完全调试日志记录

有关错误和可能的解决方案的更多信息,请阅读以下文章:
[帮助1]

使用扫描仪直接获取字符输入没有好方法。但是,这里有一些解决方法:

 
class Interactive {
    public static void main (String[] args) 
    {
        final Scanner console = new Scanner(System.in);
        System.out.println ("Please enter your height in feet: ");
        final String s = console.next();
        System.out.println ("Please enter a character: ");
        final char c = console.nextChar();
        System.out.println ("Please enter your age: ");
        final int i = console.nextInt();
        System.out.println ("Please enter your weight in lbs: ");
        final float f = console.nextFloat();
        System.out.println ("Your height is: " + s);
        System.out.println ("Your character is: " + c);
        System.out.println ("Your age is: " + i);
        System.out.println ("Your weight is: " + f);
    }
}

这段代码唯一的问题是您可以输入多个字符,但是如果您知道您只需要输入一个字符,那么这应该不是问题。如果您输入多个字符,它将只打印第一个输入的字符。

它会给您带来什么错误?另外,您确定要在请求浮点时使用
nextFloat()
吗?我将其更改为
nextFloat()
并在问题中添加了错误。
final String s1 = console.next();
final char c = s1.charAt(0);