2016-10-08 6 views
-1

私のメソッド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() 
    }  


} 

答えて

0
int i = new Scanner(System.in).nextInt(); 
if (i == 1) { 
    //Do something 
} 
if (i == 2) { 
    //Do something 
} 
if (i == 3) { 
    //Do something 
} 
else return; 
+0

については、スイッチ構造を使用する方がよいでしょう。多分 –

+0

私はそれを更新する – Endorphinex

+0

しかし、私はそれが何を追加しますか?それにメソッドを呼びますか? – Lexi

0
 Scanner sc=new Scanner(System.in); 
     System.out.println("Type number for area calculation: 1 for Triangle, 2 for Circle, 3 for Rectangle, 0 for none of them\n"); 
     String m=sc.next(); 
     switch(m){ 
     case "0": 
      System.out.println("None of them"); 
      break; 
     case "1": 
      System.out.println("This is for Triangle"); //write your calculation method or call it here 
      break; 
     case "2": 
      System.out.println("This is for Circle"); //write your calculation method or call it here 
      break; 
     case "3": 
      System.out.println("This is for Rectangle"); //write your calculation method or call it here 
      break; 
     default: 
      System.out.println("Wrong selection!"); //write your calculation method or call it here 

これが最も簡単な方法です。

if(0==m) System.out.println("None of them"); 
else if(1==m) areaTriangle(n1,n2); 
else if(2==m) areaCircle(radius); 
else if(3==m) areaRectangle(m1,m2); 
else System.out.println("Wrong selection") 

これはif文の使用方法です。したがって、私はこの割り当てが練習のためのものであると仮定します。

+0

私は第二の部分があなたが望むものだと思います。それらをまとめる方法を学ばなければなりません。 – Evingle

0
import java.util.Scanner; 

public class Area { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     Scanner shapes = new Scanner(System.in); 

     System.out.println("Type number for area calculation: 1 for Triangle, 2 for Circle, 3 for Rectangle, 0 for none of them\n"); 
     String m = sc.next(); 
     switch (m) { 
      case "0": 
       System.out.println("None of them"); 
       break; 
      case "1": 
       System.out.println("This is for Triangle"); 

       System.out.println("Enter two values to calculate the area of a triangle: "); 
       int n1 = shapes.nextInt(); 
       int n2 = shapes.nextInt(); 

       areaTriangle(n1, n2); 
       break; 
      case "2": 
       System.out.println("This is for Circle"); 
       System.out.println("Enter a value to calculate the area of a circle: "); 
       double radius = shapes.nextInt(); 
       areaCircle(radius); 

       break; 
      case "3": 
       System.out.println("This is for Rectangle"); 
       System.out.println("Enter two values to calculate the area of a rectangle: "); 
       int m1 = shapes.nextInt(); 
       int m2 = shapes.nextInt(); 

       areaRectangle(m1, m2); 
       break; 
      default: 
       System.out.println("Wrong selection!"); 

     } 
    } 

    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); 

    } 
} 
関連する問題