【问题标题】:Invalid constant character when using switch/case?使用 switch/case 时常量字符无效?
【发布时间】:2017-09-06 15:24:03
【问题描述】:

我似乎无法让我的代码正常工作。我需要找到正方形的面积并添加测量单位,具体取决于用户使用的单位,英寸为英寸,米为米,厘米为厘米,英尺为英尺。

public static void main (String[] args)
{

// create scanner to read the input

        Scanner sc = new Scanner(System.in);

//prompt the user to enter one side of the length

       System.out.println("Enter one side of lenght of the square:");

        double side1 = sc.nextDouble();

        while (side1 < 0)

        {
//prompt the user to enter the input again in a positive value

            System.out.println("Error, No Negative Number. Enter again:");

            side1 = sc.nextDouble();
        }
char unitsMeasurement;
// prompt the user to enter the measurement unit
char units = Character.toUpperCase(status.charAt(0));
String unitsMeasurement = "";

    **{
        switch(units)
        {
        case "in":
            unitsMeasurement = "inch"; break;
        case "cm":
            unitsMeasurement = "centimeter"; break;
        case "ft":
            unitsMeasurement = "feet"; break;
        case "m":
             unitsMeasurement = "meter"; break;

        default:System.out.println("Invaild unit"); break;
                         }**


//Area of Square = side*side

          double area = side1*side1; 


        **System.out.println("Area of Square is: "+area, +unitsMeasurement+);**

      }

    }
}

【问题讨论】:

  • 请在问题文本中包含您遇到的错误。确保它是确切的错误消息。从标题来看,我认为您可能错过了导入 java 包。尝试添加 import java.lang.*;

标签: java switch-statement case


【解决方案1】:

您的主要问题是,您在 char 上使用 switch-case-statement,而您的所有案例都基于 String。这不能一起工作。 其他一些问题是 status 从未定义,因此 units 根本没有值。

我不太确定您要达到的目标,但我假设如下: 用户输入带有单位(缩写)的正方形的长度。程序计算正方形的面积并将其与单位(未缩写)一起输出。

示例输入:

5cm

样本输出:

Area of square is: 25 centimeter^2

请记住,面积有一个平方长度单位!

基于此,这里有一些工作代码:

public static void main (String[] args) {

    // create scanner to read the input
    Scanner sc = new Scanner(System.in);

    //prompt the user to enter one side of the length
    System.out.println("Enter one side of lenght of the square:");
    String input = sc.nextLine();

    //Remove anything but digits
    double side1 = Double.parseDouble(input.replaceAll("\\D+",""));
    //Remove all digits
    String unit = input.replaceAll("\\d","");
    System.out.println(side1);
    System.out.println(unit);

    while (side1 < 0) {
        //prompt the user to enter the input again in a positive value
        System.out.println("Error, No Negative Number. Enter again:");
        input = sc.nextLine();

        //Remove anything but digits
        side1 = Double.parseDouble(input.replaceAll("\\D+",""));
    }

    switch(unit) {
        case "in":
            unit = "inch";
            break;
        case "cm":
            unit = "centimeter";
            break;
        case "ft":
            unit = "feet";
            break;
        case "m":
            unit = "meter";
            break;

        default:
            System.out.println("Invaild unit");
            break;
    }

    double area = side1*side1;
    System.out.println("Area of Square is: " + area + " " + unit + "^2");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多