简体   繁体   English

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

[英]compilation errors due to my if statement

I clearly don't understand what I am doing here... I am pretty certain that my errors are only in the code I included, but I do have other classes also. 我显然不明白我在这里做什么……我可以肯定我的错误仅在我包含的代码中,但是我也有其他类。 If I could rename this to a better title, let me know, Or if I can include any more info let me know. 如果我可以将其重命名为更好的标题,请告诉我,或者如果我可以包含更多信息,请告诉我。 From what I see on this website, I believe you guys will know exactly what the compilation errors mean, and know what I did wrong. 从我在该网站上看到的信息来看,我相信你们会确切地知道编译错误的含义,并且知道我做错了什么。

Here is my code: 这是我的代码:

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

My compilation errors: 我的编译错误:

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

There's no objectof operator in Java, I think you meant instanceof . Java中没有objectof运算符,我认为您的意思是instanceof Also, there's a misplaced semicolon inside one of the conditions. 此外,其中一种情况中存在分号放错的地方。 Rewrite the conditions like this: 重写这样的条件:

if (currentShape instanceof TwoDimensionalShape)
if (currentShape instanceof ThreeDimensionalShape)

Java has an instanceof operator instead of objectof Java具有instanceof运算符,而不是objectof

In Line 21 Replace if (currentShape objectof TwoDimensionalShape) with if(currentShape instanceof TwoDimensionalShape){ //more code } 在第21行中,将if (currentShape objectof TwoDimensionalShape)替换为if (currentShape objectof TwoDimensionalShape) if(currentShape instanceof TwoDimensionalShape){ //more code }

In Line 33 if (currentShape objectof ThreeDimensionalShape;) with if(currentShape instanceof ThreeDimensionalShape){ //more code } 在第33行中, if (currentShape objectof ThreeDimensionalShape;)if(currentShape instanceof ThreeDimensionalShape){ //more code }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM