由于我的if语句导致的编译错误

问题描述 投票:0回答:2

我显然不明白我在这里做什么......我很确定我的错误只出现在我所包含的代码中,但我也有其他类。如果我可以将其重命名为更好的标题,请告诉我,或者如果我可以包含更多信息,请告诉我。从我在这个网站上看到的,我相信你们会确切知道编译错误的意思,并知道我做错了什么。

这是我的代码:

import java.io.PrintStream;
public class Shape
{
   public static void init( String args[] )
   {
      Shape[] shapes = new Shape[ 4 ];
      shapes[ 0 ] = new Circle( 22, 88, 4 );
      shapes[ 1 ] = new Square( 71, 96, 10 );
      shapes[ 2 ] = new Sphere( 8, 89, 2 );
      shapes[ 3 ] = new Cube( 79, 61, 8 );

      for ( Shape currentShape : shape );
      {
         System.out.printf( "%s: %s", 
         currentShape.getName(), currentShape );

         Object localObject;

         if ( currentShape objectof TwoDimensionalShape )
         { 
             localObject = (TwoDimensionalShape)currentShape; 

            TwoDimensionalShape twoDimensionalShape = 
               ( TwoDimensionalShape ) currentShape;

            System.out.printf( "%s's area is %s\n", 
               currentShape.getName(), twoDimensionalShape.getArea() );
         } 

         if ( currentShape objectof ThreeDimensionalShape; )
         {
            ThreeDimensionalShape threeDimensionalShape = 
               ( ThreeDimensionalShape ) currentShape;

            System.out.printf( "%s's area is %s\n", 
               currentShape.getName(), threeDimensionalShape.getArea() );
            System.out.printf( "%s's volume is %s\n",
               currentShape.getName(), 
               threeDimensionalShape.getVolume() );
         } 

         System.out.println();
      } 
   } 
} 

我的编译错误:

ShapeTest.java:21: error: ')' expected
         if ( currentShape objectof TwoDimensionalShape )
                          ^
ShapeTest.java:21: error: ';' expected
         if ( currentShape objectof TwoDimensionalShape )
                                                       ^
ShapeTest.java:21: error: variable declaration not allowed here
         if ( currentShape objectof TwoDimensionalShape )
                                    ^
ShapeTest.java:33: error: ')' expected
         if (( currentShape objectof ThreeDimensionalShape; ))
                                    ^
ShapeTest.java:33: error: illegal start of expression
         if (( currentShape objectof ThreeDimensionalShape; ))
                                                          ^
ShapeTest.java:33: error: illegal start of expression
         if (( currentShape objectof ThreeDimensionalShape; ))
                                                             ^
6 errors
java if-statement compiler-errors compilation
2个回答
1
投票

Java有一个instanceof运算符而不是objectof

在第21行用if (currentShape objectof TwoDimensionalShape)替换if(currentShape instanceof TwoDimensionalShape){ //more code }

在第33行if (currentShape objectof ThreeDimensionalShape;)if(currentShape instanceof ThreeDimensionalShape){ //more code }


2
投票

Java中没有objectof运算符,我认为你的意思是instanceof。此外,其中一个条件中有一个错误的分号。重写这样的条件:

if (currentShape instanceof TwoDimensionalShape)
if (currentShape instanceof ThreeDimensionalShape)
© www.soinside.com 2019 - 2024. All rights reserved.