-1

I am new to java. i created a program to find the area of polygons. I wanted it to ask the type of polygon an then find the area. i used if, else if and else statements but whenever i type the name of the polygon nothing happens.

Here is the script

 import java.util.Scanner ;
  public class Area

   {
      static Scanner sc = new Scanner(System. in );

      public static void main(String[] args) {

          System.out.print("Enter the type of polygon: ");
          String polygon = new String("polygon");
          polygon = sc.next();
          String square = new String("square");
          String rectangle = new String("rectangle;");
          String triangle = new String("triangel");



          polygon = sc.next();
          if (polygon == square) {

              double side;
              System.out.print("Enter the side: ");
              side = sc.nextDouble();
              double area;
              area = side * side;
              System.out.print("The Area is: " + area);
          } else if (polygon == rectangle) {
              double length;
              double breadth;
              System.out.print("Enter the length: ");
              length = sc.nextDouble();
              System.out.print("Enter the breadth: ");
              breadth = sc.nextDouble();
              double area;
              area = length * breadth;
              System.out.print("The Area is : " + area);
          } else if (polygon == triangle) {

              double base;
              double height;
              System.out.print("Enter the base: ");
              base = sc.nextDouble();
              System.out.print("Enter the height: ");
              height = sc.nextDouble();
              Double area;
              area = base * height / 2;
              System.out.print("The Area is: " + area);
          } else {
              System.out.print("ERROR it is not a polygon");
          }

      }
  }

Please help me out thank you

4

2 回答 2

0

好的,我在您的代码中看到了一些更好的方法。
您不需要创建一个 String 对象来将您的输入与字符串进行比较。你可以只使用:
if(polygon="square")

或者更好的是,您可以使用 switch 语句。
我认为您遇到问题的原因是因为您使用的是 .next() 而不是 .nextLine()。
我会使用 switch 语句并执行以下操作:

Scanner sc = new Scanner(System. in );
myInput = sc.nextLine() // Get the next line from the keyboard
switch (myInput.toLowerCase()) // Make the inputted text to lower case, do switch 
{
    case "square":
        // DO SQUARE LOGIC HERE
        break;

    case "rectangle":
        // DO RECT LOGIC HERE
        break;

    case "triangle":
        // DO TRIANGLE LOGIC HERE
        break;
    default:
        // Did not recognize the type of polygon entered.
}
于 2013-07-21T03:55:26.423 回答
0

看来您正在将变量多边形(始终包含字符串“多边形”)与下一行进行比较。

尝试这个:

import java.util.Scanner;

public class Area

{
static Scanner sc = new Scanner(System.in);

 public static void main (String [] args)
 {

   System.out.println("To find the area of a polygon type,");
   System.out.println("\"1\" for a Square");
   System.out.println("\"2\" for a Rectangle");
   System.out.println("\"3\" for a Triangle");
   System.out.println("Enter the number to select the type of polygon: ");
   int p = sc.nextInt();

    if (p == 1 ) {

     double side ;
     System.out.print("Enter the squaer side: ");
     side =sc.nextDouble() ;
     double area;
     area =side * side;
     System.out.print("The Area is: " + area);}

     else if (p == 2)
      {     
      double length;
      double breadth;
      System.out.print("Enter the rectangle length: ");
      length =sc.nextDouble(); 
      System.out.print("Enter the breadth: ");      
      breadth =sc.nextDouble();   
      double area;     
      area =length * breadth;
      System.out.print("The Area is : " + area); }


      else if (p == 3)
      {

     double base;
     double height;  
     System.out.print("Enter the triangle's base: ");
     base =sc.nextDouble();
     System.out.print("Enter the height: ");
     height =sc.nextDouble();       
     Double area ;
     area = base * height / 2 ;
     System.out.print("The Area is: " + area) ;   }       


    else
     {
     System.out.println("ERROR it is not a polygon");
     System.out.print("Please enter a number: 1, 2 or 3.");
     }

   }
 }

我已经尝试了几次并且效果很好。希望这对您的编码看起来更好,并且在我看来它是“用户友好的”,因为程序员会要求用户输入数字而不是字符串(这可能会导致拼写错误)。祝你好运。

于 2013-07-21T04:16:29.213 回答