首页 > 解决方案 > Java if else 问题

问题描述

基本上我的代码假设通过使用高度来计算重量。用户必须在他/她是男性还是女性之间进行选择。之后,if-else 语句应该进行计算。问题是当用户键入男性或女性之间的选择时。代码忽略了选择并立即转到 else 语句来告诉您您的选择是错误的。

导入 java.util.Scanner;

公共课课{

public static void main(String[] args) {

    Scanner Scan = new Scanner(System.in);
    System.out.println("\nAre you Male or Female: ");
    String Choice = Scan.nextLine();
    if (Choice == "Male") {
        System.out.println("What is your hight in CMeters: ");
        int Height = Scan.nextInt();

        if (Height > 160) {
            int Your_weight = (Height - 100) * 90 / 100;
            System.out.println("Your_weight is: " + Your_weight);
        }

        else {
            int Your_weight = (Height - 100);
            System.out.println("Your_weight is: " + Your_weight);
        }
    }

    else if (Choice == "Female") {

        System.out.println("What is your hight in CMeters: ");
        int Height = Scan.nextInt();

        if (Height > 160) {
            int Your_weight = (Height - 100) * 90 / 100;
            System.out.println("Your_weight is: " + Your_weight);
        }

        else {
            int Your_weight = (Height - 100);
            System.out.println("Your_weight is: " + Your_weight);

        }

    }

    else {
        System.out.println("you Enter a worng choice");
    }

}

}

标签: java

解决方案


推荐阅读