【发布时间】:2014-09-17 08:32:33
【问题描述】:
我目前正在尝试完成这个 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 转换为 Scanner” “类型不匹配。无法从 Scanner 转换为布尔值”
我不明白这些是什么意思,希望得到一些帮助。
【问题讨论】:
-
Ahhhhhhhh 我刚刚发现了我的问题。再次感谢大家的帮助。我现在完全明白了。
标签: java eclipse if-statement ide