我目前正在尝试完成这个Java编程实践项目而且陷入困境......我不确定我的问题是什么,所以我能得到一些帮助吗?
以下是完整的代码:
package AreaElseIf;
import java.io. *;
import java.util. *;
public class AreaElseIf {
public static void main(String[] args) {
final double PI = 3.14;
Scanner input = new Scanner(System.in);
System.out.println("This program uses an else/if statement.");
System.out.println("1 = circle, 2 = square, 3 = rectangle, 4 = triangle.");
int object = input.nextInt();
if (input = 1) {
System.out.println("Enter circle's radius: ");
double radius = input.nextDouble();
double cArea = radius * radius * PI;
System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
}
else if (input = 2) {
System.out.println("Enter length of square's sides: ");
double sSide = input.nextDouble();
double sArea = sSide * sSide;
System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
}
else if (input = 3) {
System.out.println("Enter length of rectangle's base: ");
double base = input.nextDouble();
System.out.println("Enter length of rectangle's height: ");
double height = input.nextDouble();
double rArea = base * height;
System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
}
else if (input = 4) {
System.out.println("Enter traingle's side length: ");
double tSide = input.nextDouble();
double tArea = tSide * tSide * tSide;
System.out.println("The area of a triangle with a side length of " + tSide + " is " + tArea + ".");
}
else {
System.out.println("Error: Please enter a number between 1-4 only.");
}
}//end main
}//end class
程序的目的是要求用户输入1到4之间的数字,并将每个数字分配给一个形状。如果单击该形状的数字,它会向您询问几个问题,以便计算所述形状的面积。这个程序的重点是使用else / if来完成它。
我得到的错误只是“(输入= 1,2,3,4 ......)”的行 input =#都是带下划线的红色。
错误的措辞如下: “类型不匹配。无法从int转换为扫描仪” “类型不匹配。无法从扫描仪转换为布尔值”
我不明白这些意思是什么,并希望得到一些帮助。
答案 0 :(得分:2)
问题的第一部分是您在每个if / else语句中使用了赋值运算符=
。要测试相等性,请使用==
运算符,因为它返回bool(如果相等则为true,否则为false)。
问题的后半部分是你根据你的int值测试你的扫描仪,你应该在那里测试扫描仪的结果(在你的情况下看起来像object
检索用户的int)。 / p>
最终你的if应该是这样的:
if(object == 1) {
...
答案 1 :(得分:0)
而不是做
if (input = 1)
你需要做
if (object == 1)
在您的代码中,input
是Scanner
对象,您正在尝试为其分配(=
)整数。相反,您希望比较从扫描仪获得的整数输入(object
),并将其(==
)与整数值进行比较。
答案 2 :(得分:0)
问题在这里if (input = 1)
首先你尝试将输入设置为1,而不是比较值,其次输入是扫描仪而不是整数
要修复它,请更改为if (object == 1)
,它将起作用object
是您从扫描仪读取的整数,==
是比较。
另外,您可以使用switch / case语句对您的简单问题进行排序,但这是不同的故事
答案 3 :(得分:0)
if (input == 1) {
System.out.println("Enter circle's radius: ");
double radius = input.nextDouble();
double cArea = radius * radius * PI;
System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
}
else if (input == 2) {
System.out.println("Enter length of square's sides: ");
double sSide = input.nextDouble();
double sArea = sSide * sSide;
System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
}
else if (input == 3) {
System.out.println("Enter length of rectangle's base: ");
double base = input.nextDouble();
System.out.println("Enter length of rectangle's height: ");
double height = input.nextDouble();
double rArea = base * height;
System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
}
else if (input == 4) {
System.out.println("Enter traingle's side length: ");
double tSide = input.nextDouble();
double tArea = tSide * tSide * tSide;
System.out.println("The area of a triangle with a side length of " + tSide + " is " + tArea + ".");
}
else {
System.out.println("Error: Please enter a number between 1-4 only.");
}
答案 4 :(得分:0)
Java中的等式比较器是==
,而不是=
。
您要检查的int
变量名为object
,而不是input
替换
if (input =
带
if (object ==
答案 5 :(得分:0)
基本上你有
Scanner input = new Scanner(System.in);
int object = input.nextInt();
if (input = 1) {
1 /你无法测试int的输入。 object 是一个int
2 /用' =='进行测试。不是' ='这是一种矫揉造作if (object == 1) {
应解决您的问题
答案 6 :(得分:0)
对于整数使用" ==" if(输入== 1){..这里..} " ="是一个分配运营商, ==对所有变量的行为相同:它测试这些变量的值是否相等。在Object obj的情况下,obj是对对象的引用。由于==测试两个对象引用是否具有相同的值,因此测试它们是否引用相同的对象(即引用是否相等)。 对于您的应用程序,我建议您使用switch语句http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
干杯, 伊德里斯。
答案 7 :(得分:0)
已经有很多答案可以指导您进行更改
if(input = 1)
到
if (object == 1)
我想建议另一个解决方案: switch()语句。 为什么? Switch()针对两个以上的分支进行了优化,通常更容易阅读(在我看来)。使用switch()的代码如下所示:
package AreaElseIf;
import java.io. *;
import java.util. *;
public class AreaElseIf {
public static void main(String[] args) {
final double PI = 3.14;
Scanner input = new Scanner(System.in);
System.out.println("This program uses an else/if statement.");
System.out.println("1 = circle, 2 = square, 3 = rectangle, 4 = triangle.");
switch( input.nextInt())
{
case 1:
System.out.println("Enter circle's radius: ");
double radius = input.nextDouble();
double cArea = radius * radius * PI;
System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
break;
case 2:
System.out.println("Enter length of square's sides: ");
double sSide = input.nextDouble();
double sArea = sSide * sSide;
System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
break;
case 3:
System.out.println("Enter length of rectangle's base: ");
double base = input.nextDouble();
System.out.println("Enter length of rectangle's height: ");
double height = input.nextDouble();
double rArea = base * height;
System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
break;
case 4:
System.out.println("Enter traingle's side length: ");
double tSide = input.nextDouble();
double tArea = tSide * tSide * tSide;
System.out.println("The area of a triangle with a side length of " + tSide + " is " + tArea + ".");
break;
default:
System.out.println("Error: Please enter a number between 1-4 only.");
}
}//end main
请注意,在此代码中,您不需要(int object)变量。
答案 8 :(得分:-1)
尝试在if语句中将input
更改为object
。和=
到==