0
  boolean isGender = gender == "M";

此语句返回错误“无法比较的类型”之前的其余代码是

import java.util.Scanner;
public class BMR
{
  public static void main(String[] args)
  {
  //Scanner
  Scanner in = new Scanner(System.in);

  //Get info
  System.out.println("Enter your name: ");
  String name = in.nextLine();
  System.out.println("Gender (M or F): ");
  String genderString = in.next();
  char gender = genderString.charAt(0);
  System.out.println("Enter your age: ");
  int age = in.nextInt();
  System.out.println("Height in inches: ");
  int height = in.nextInt();
  System.out.println("Weight in pounds: ");
  int weight = in.nextInt();
  System.out.println();

  //calculate BMR
  boolean isGender = gender == "M";

不知道为什么这不起作用

4

2 回答 2

5

"M"不是人物,是String

根据您的代码,您应该使用gender == 'M'

作为旁注,如果gender是 a String,您应该使用,gender.equals("M")或者如果不关心这种情况,您可以使用gender.equalsIgnoreCase("M")

于 2013-10-14T03:15:01.613 回答
0

尝试

boolean isGender = gender == 'M';

http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html

于 2013-10-14T03:16:16.647 回答