私のメソッドcalcAreaでは、ユーザに計算を促す必要があります(Triangleは1、Circleは2、Rectangleは3、どれも0ではありません)。ユーザーが0を入力した場合は直ちに戻り、それ以外の場合は...条件式を使用して、計算する領域のタイプに基づいて、オブジェクトの寸法を入力するように求め、適切なエリアメソッドを呼び出します。これは宿題のための私の割り当てです、私は複数の異なる方法を試して、それを得るように見えることができません。事前に助けてくれてありがとう!これは私のコードです:ユーザベースの入力に条件を使用する方法
public class Area {
public static void main(String[] args) {
Scanner shapes = new Scanner (System.in);
System.out.println("Enter two values to calculate the area of a triangle: ");
int n1 = shapes.nextInt();
int n2 = shapes.nextInt();
System.out.println("Enter a value to calculate the area of a circle: ");
double radius = shapes.nextInt();
System.out.println("Enter two values to calculate the area of a rectangle: ");
int m1 = shapes.nextInt();
int m2 = shapes.nextInt();
areaTriangle(n1, n2);
areaCircle(radius);
areaRectangle(m1, m2);
}
public static void areaTriangle (int n1, int n2) {
int areat = (n1 * n2)/2;
System.out.println("The area of a triangle with your values is: " + areat);
}
public static void areaCircle (double radius) {
double areac = Math.PI * (radius * radius);
System.out.println("The area of the circle with your values is: " + areac);
}
public static void areaRectangle (int m1, int m2) {
int arear = (m1 * m2);
System.out.println("The area of a rectangle with your values is: " + arear);
}
public static void calcArea() {
System.out.println("Type 1 for the area of Triangle, 2 for area of Circle, 3 for Rectangle, and 0 for area of none of these.");
if()
}
}
については、スイッチ構造を使用する方がよいでしょう。多分 –
私はそれを更新する – Endorphinex
しかし、私はそれが何を追加しますか?それにメソッドを呼びますか? – Lexi