Java 使用开关/大小写时常量字符无效?
我似乎无法使我的代码正常工作。我需要找到正方形的面积,并根据用户使用的英寸、米、厘米和英尺来添加测量单位Java 使用开关/大小写时常量字符无效?,java,switch-statement,case,Java,Switch Statement,Case,我似乎无法使我的代码正常工作。我需要找到正方形的面积,并根据用户使用的英寸、米、厘米和英尺来添加测量单位 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.pr
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+);**
}
}
}
publicstaticvoidmain(字符串[]args)
{
//创建扫描仪以读取输入
扫描仪sc=新的扫描仪(System.in);
//提示用户输入长度的一侧
System.out.println(“输入正方形长度的一侧:”);
双面1=sc.nextDouble();
而(侧1<0)
{
//提示用户再次输入正值
System.out.println(“错误,没有负数。请再次输入:”);
side1=sc.nextDouble();
}
煤焦单位测定;
//提示用户输入测量单位
字符单位=Character.toUpperCase(status.charAt(0));
字符串单位测量=”;
**{
开关(单位)
{
案例“in”:
单位测量=“英寸”断裂;
案例“cm”:
单位测量=“厘米”断裂;
案例“ft”:
单位测量=“英尺”断裂;
案例“m”:
单位测量=“米”中断;
默认值:System.out.println(“Invaild单位”);中断;
}**
//正方形面积=边*边
双面积=侧1*侧1;
**System.out.println(“正方形面积为:“+面积,+单位测量+”**
}
}
}
您的主要问题是,您在字符
上使用switch case语句,而所有的case都基于字符串
。这是不可能的。
其他一些问题是,状态
从未定义,因此单位
根本不能有值
我不太确定您想要实现什么,但我假设:
用户用单位(缩写)输入正方形的长度。程序计算正方形的面积,并将其与单位一起输出(未修正)
样本输入:
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");
}
publicstaticvoidmain(字符串[]args){
//创建扫描仪以读取输入
扫描仪sc=新的扫描仪(System.in);
//提示用户输入长度的一侧
System.out.println(“输入正方形长度的一侧:”);
字符串输入=sc.nextLine();
//删除除数字以外的任何内容
double side1=double.parseDouble(input.replaceAll(“\\D+”,”);
//删除所有数字
字符串单位=input.replaceAll(“\\d”和“);
系统输出打印LN(第1侧);
系统输出打印项次(单位);
而(侧1<0){
//提示用户再次输入正值
System.out.println(“错误,没有负数。请再次输入:”);
输入=sc.nextLine();
//删除除数字以外的任何内容
side1=Double.parseDouble(input.replaceAll(“\\D+”,”);
}
开关(单位){
案例“in”:
单位=“英寸”;
打破
案例“cm”:
单位=“厘米”;
打破
案例“ft”:
单位=“英尺”;
打破
案例“m”:
单位=“米”;
打破
违约:
System.out.println(“入侵单位”);
打破
}
双面积=侧1*侧1;
System.out.println(“正方形的面积为:“+面积+”+单位+“^2”);
}
请在问题文本中包含您遇到的错误。确保它是正确的错误消息。从标题来看,我认为您可能没有导入java包。尝试添加import java.lang.*;