【解決方法】system.out.println( を試してみると、コードの最後の行でエラーがわかりませんでした"ウィリアム・シェイクスピア "+生年月日+ " – "+ 年に死亡+ , )

プログラミングQA


+ displayHistoricalFigure():void

この英国の歴史上の人物が生まれた時期を返す getHistoricalPeriod メソッドを記述します。

1. 以下の期待される出力に示されているように、この歴史上の人物を要約した文を出力します。 出力の一部に対して getHistoricalPeriod メソッドを呼び出す必要があります。

私が試したこと:

Java
public class BritishHistoricalFigure{

      private String name;
      private int birthYear;
      private String occupation;
      private int yearOfDeath;
   
    /* replacement for default constructor*/
   public BritishHistoricalFigure(){
   }
      /*constructor method*/
   public BritishHistoricalFigure(String n, int bY, String o, int yOD){
      occupation = o;
      birthYear = bY;
      diedInYear = dIY;
      name =n;
   
   
   }

   public int getBirthYear(){
      return birthYear;
   }

   public String getOccupation(){
      return occupation;
   }

   public int getdiedInYear(){
      return diedInYear;
   }

   public void setOccupation(String O){
      occupation = O;
   }

   public String getHistoricalPeriod(){
      if (yearOfDeath < 1485){
      return "Pre-Tudor";
      }
      else if (yearOfDeath> 1485 || yearOfDeath <1603){
      return "Tudor";
      }
      else if (yearOfDeath >1603 || yearOfDeath <1714){  
      return "Stuart";
      }
      else if (yearOfDeath >1714 || yearOfDeath < 1837){  
      return "Georgian";
      }
      else if (yearOfDeath >1837 ||yearOfDeath < 1901){
      return "Victorian";
      }
      else if(yearOfDeath > 1901 || yearOfDeath< 1910){
      return "Edwardian";
      }
      else{
         return "Windsor";
      }
   }
   public static void displayHistoricalFigure(){
      if(birthYear >=1564 && yearOfDeath <=1616){
         System.out.print
      Sytem.out.println("William Shakespear, "+ birthYear+ " - "+ diedInYear+ ,  )
   }
}

解決策 1

次の行をよく見てください。

Java
Sytem.out.println("William Shakespear, "+ birthYear+ " - "+ diedInYear+ , )

そのはず System いいえ Sytem

最後にセミコロンが必要です
それはspを持っています[uriopus “+ ,” at the end of the string concatenation.

But the line above is wrong as well!
It needs “()” to show it’s a method call.
It needs a semicolon at the end.

You should expect to get syntax errors every day, probably many times a day while you are coding – we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors – and it’s a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong – it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner’s Guide Part 2: Syntax Errors[^] – 次回コンパイル エラーが発生したときに役立つはずです。

コードをインデントしてください。 ロード全体が読みやすくなります…

解決策 2

この方法を単純化することもできます。

Java
 public String getHistoricalPeriod(){
   if (yearOfDeath < 1485){
   return "Pre-Tudor";
   }
   else if (yearOfDeath> 1485 || yearOfDeath <1603){
   return "Tudor";
   }
   else if (yearOfDeath >1603 || yearOfDeath <1714){
   return "Stuart";
   }
   else if (yearOfDeath >1714 || yearOfDeath < 1837){
   return "Georgian";
   }
   else if (yearOfDeath >1837 ||yearOfDeath < 1901){
   return "Victorian";
   }
   else if(yearOfDeath > 1901 || yearOfDeath< 1910){
   return "Edwardian";
   }
   else{
      return "Windsor";
   }
}

Java
public String getHistoricalPeriod(){
    if (yearOfDeath < 1485){
        return "Pre-Tudor";
    }
    else if (yearOfDeath < 1603){ // you already know it is greater than 1485 from the previous test
        return "Tudor";
    }
    else if (yearOfDeath < 1714){  // as above
        return "Stuart";
    }
    else if (yearOfDeath < 1837){  
        return "Georgian";
    }
    else if (yearOfDeath < 1901){
        return "Victorian";
    }
    else if(yearOfDeath < 1910){
        return "Edwardian";
    }
    else{
        return "Windsor";
    }
}

コメント

タイトルとURLをコピーしました