简体   繁体   中英

Can't compare char

  boolean isGender = gender == "M";

this statement returns an error "incomparable types" the rest of the code before that is

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";

no idea why this doesn't work

"M" is not a character, it's String

Based on your code, you should be using gender == 'M'

As a side note, if gender was a String , you should be using gender.equals("M") or if don't care about the case, you could use gender.equalsIgnoreCase("M") instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM