Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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.util.InputMismatchException?_Java_Boolean_Java.util.scanner - Fatal编程技术网

为什么要获取java.util.InputMismatchException?

为什么要获取java.util.InputMismatchException?,java,boolean,java.util.scanner,Java,Boolean,Java.util.scanner,完全错误: //*************************************************************** //File: weight.java // //Purpose: Computes the ideal weight for both males and females. //*************************************************************** import java.util.Scanner

完全错误:

//***************************************************************
//File: weight.java
//
//Purpose: Computes the ideal weight for both males and females.
//***************************************************************
import java.util.Scanner;

public class weight
{
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    boolean answer1;
    System.out.println ("Are you a male (m) or female (f)?");
     boolean m=true;
    boolean f=false;
    answer1=scan.nextBoolean();

    int feet, inches;
    if (answer1=m)
    {
    System.out.println ("Enter your height in feet.  Inches will be asked later.");
    feet=scan.nextInt ();
    System.out.println ("Enter the remaining inches.");
    inches=scan.nextInt ();
    }
    if (answer1=f)
    System.out.println ("Enter your height in feet.  Inches will be asked later.");
    feet=scan.nextInt ();
    System.out.println ("Enter the remaining inches.");
    inches=scan.nextInt ();  
    }
    }
我不熟悉“如果”语句和布尔语,所以如果有人能解释一下,那就太好了!
基本上,我只想让它询问用户是男性还是女性,然后根据答案转发某些代码。

检查输入是否为
answer1=scan.nextBoolean()
使用
nextBoolean()
时,只允许
true
false

顺便说一下,你有
if(answer1=m)
if(answer1=f)
哪个是错误的。”
=
'是一个赋值运算符,'
=
'是布尔运算符

if(answer1==m)
if(answer1==f)
是正确的方法,可以帮助解决您的问题


可以考虑使用char而不是<代码>布尔< <代码>,当你要求用户输入男性女性时,你可以尝试一下,因为这是有意义的,但他必须输入

,他对此一无所知。

如果你想让用户输入性别的'm'或'f',那么使用
字符
输入是最好的选择。这样使用它:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextBoolean(Unknown Source)
at weight.main(weight.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
然后,对于比较,不要使用单个
=
,因为这是赋值运算符,请使用
=
进行比较。因此,请这样使用:

char answer1;
System.out.println ("Are you a male (m) or female (f)?");
answer1=scan.next().charAt(0);


您为“您是男性(m)还是女性(f)”输入了什么?OPP您应该输入
true
of
false
。男性也是如此。m不是一个布尔右图??不过我声明了它。布尔m=真;布尔f=假;这不起作用吗?不。
nextBoolean
希望您输入布尔值true或false。当您输入m时,它与布尔值不匹配。如果您要输入m或f,则使用nextcharIt实际上不是编译器错误;这种情况下,编译服务运行编译后的代码(“未知源”),生成运行时异常。编译方法类“JavaCompiler”作为副作用出现在堆栈上,即使编译已经成功完成。@AusrichterSchlachtfeld我已经更新了答案。尝试将输入读取为男性或女性/M或F,而不是布尔值,如果答案已解决,则标记答案(通过勾选)。
if (answer1=='m' || answer1=='M') //taking care of both capital or small input
if (answer1=='f' || answer1=='F')