如何使用条件进行基于用户的输入

时间:2016-10-08 21:07:03

标签: java

在我的方法calcArea中,我必须“提示用户计算(1表示三角形,2表示圆形,3表示矩形,0表示没有)。使用Java扫描程序读取输入。如果用户输入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 ()
    }    


}

3 个答案:

答案 0 :(得分:0)

var bmpTemp: Bitmap;
bmpTemp = new Bitmap();
bmpTemp = fConvertToBMP(mcCircle.mcCircleVector, 150, 150);
mcCircle.removeChild(mcCircle.mcCircleVector);
mcCircle.mcCircleVector = null;
mcCircle.addChild(bmpTemp);
bmpTemp = null;


private function convertBMP(pClip: MovieClip, pWidth: int, pHeight: int): Bitmap {
    var bd: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);
    bd.draw(pClip, null, null, null, null, true);
    var bmp: Bitmap = new Bitmap(bd, "auto", false);
    bmp.smoothing = true;
    return bmp;
}

答案 1 :(得分: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语句的方式。因此,我认为这个任务是为了练习,否则如果,否则。

答案 2 :(得分: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);

    }
}