簡體   English   中英

由於我的if語句導致的編譯錯誤

[英]compilation errors due to my if statement

我顯然不明白我在這里做什么……我可以肯定我的錯誤僅在我包含的代碼中,但是我也有其他類。 如果我可以將其重命名為更好的標題,請告訴我,或者如果我可以包含更多信息,請告訴我。 從我在該網站上看到的信息來看,我相信你們會確切地知道編譯錯誤的含義,並且知道我做錯了什么。

這是我的代碼:

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中沒有objectof運算符,我認為您的意思是instanceof 此外,其中一種情況中存在分號放錯的地方。 重寫這樣的條件:

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

Java具有instanceof運算符,而不是objectof

在第21行中,將if (currentShape objectof TwoDimensionalShape)替換為if (currentShape objectof TwoDimensionalShape) if(currentShape instanceof TwoDimensionalShape){ //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