英文:
Create an interactive program that asks the user to provide a string, a char, an int, and a floating point value. Answers print on different lines
问题
class Interactive {
public static void main(String[] args) {
final Scanner console = new Scanner(System.in);
System.out.println("请输入您的身高(以英尺为单位):");
final String s = console.next();
System.out.println("请输入一个字符:");
// 无法直接使用 console.nextChar(); 来读取字符,需要使用 console.next().charAt(0) 来获取字符
final char c = console.next().charAt(0);
System.out.println("请输入您的年龄:");
final int i = console.nextInt();
System.out.println("请输入您的体重(以磅为单位):");
final float f = console.nextFloat();
System.out.println("您的身高为:" + s);
System.out.println("您输入的字符为:" + c);
System.out.println("您的年龄为:" + i);
System.out.println("您的体重为:" + f);
}
}
错误信息:无法执行目标 org.codehaus.mojo:exec-maven-plugin:1.5.0:exec(default-cli),项目 JavaExercise:命令执行失败。:进程退出并带有错误:1(退出值:1)-> [帮助 1]
要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。
使用 -X 开关重新运行 Maven 以启用完整的调试日志记录。
有关错误和可能解决方案的更多信息,请阅读以下文章:
[帮助 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
英文:
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);
}
}
How would I go about adding char? When I add final char c = console.nextChar();
it gives me an error. Thanks for your help.
The error: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (default-cli) on project JavaExercise: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
答案1
得分: 1
没有很好的方法可以直接使用 Scanner 来获取字符输入。然而,这里有一个小的解决方法:
final String s1 = console.next();
final char c = s1.charAt(0);
这段代码唯一的问题是你可以输入多个字符,但如果你知道你只会输入一个字符,这不应该成为问题。如果你输入多个字符,它将只打印第一个输入的字符。
英文:
There is no good way to take a char input directly using a Scanner. However, here is a little work around:
final String s1 = console.next();
final char c = s1.charAt(0);
The only problem with this code is that you could type in more than one character, but if you know you're only going to input one character it shouldn't be an issue. If you type in more than one character, it would print only the first one that is inputed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论