mercredi 20 février 2019

Getting else without if? (yes i opened and closed with curlys and no there are no semi colons etc.)

Im trying to nest ifs and elses and I'm getting "else without if" errors on my else ifs. Previously answered questions similar to this have semicolons after the statements and or unclosed statements which I dont think are relevant in this case.

It's definitely possible that Im just nesting wrong and I think that might be the cause of the errors. It could also be the strings ive defined after each if statement but I dont see how that would cause the else without if error.

Ive tried rearranging the else ifs and the elses a couple times but i still get the same thing. If anyone can direct me to the correct way to code this it would be greatly appreciated.

String answer0;
answer0 = guess.nextLine();
answer0 = answer0.toUpperCase();
switch (answer0) 
{
   case "PLANT"   :  System.out.println("Is it a tree?"); 
      break;
  // case "LIVING :  System.out.println("Does it have feathers, fur, or neither"); break;
  // case "NON-LIVING" :   System.out.println("Is it man-made?"); break;
      default    :   System.out.println("Invalid"); 
}
//String answer1;
//answer1 = guess.nextLine();
//answer1 = answer1.toUpperCase();

if (answer0.equals("YES"))
{
   System.out.println("Is it deciduous?");    
}
      String answer2;
      answer2 = guess.nextLine();
      answer2 = answer2.toUpperCase();

      if (answer2.equals("YES"))
      {
         System.out.println("Is it  a fruit tree?");
      }
            String answer3;
            answer3 = guess.nextLine();
            answer3 = answer3.toUpperCase();

            if (answer3.equals("YES"))
            {  
               System.out.print("Does it have orange fruit?");
            }
                  String answer4;
                  answer4 = guess.nextLine();
                  answer4 = toUpperCase();

                  if (answer4.equals("YES"))
                  {
                     System.out.println("Are you thinking of a tangerine tree?");
                  }
                        String answer5;
                        answer5 = guess.nextLine();
                        answer5 = answer5.toUpperCase();

                        if (answer5.equals("YES"))
                        {
                           System.out.println("Correct");
                        }
                        else if (answer5.equals("NO"));
                        {
                           System.out.print("Wrong");
                        }
                        else //else without if error
                        {
                           System.out.print("Invalid");
                        }




         else if (answer4.equals("NO")) //else without if error
         {
            System.out.print("Are oyu tihnkinh of a banana tree?");
         }
         else
         {  
            System.out.print("Invalid");
         }
      else if (answer3.equals("NO")) //else without if
      {
         System.out.print("Is it a weeping tree?");
      }
      else
      {
          System.out.print("Invalid");
      }
   else if (answer2.equals("NO")); //else without if
   {
      System.out.println("Is it nativeto North America?")
   }
   else
   {
       System.out.print("Invalid");
   }
else if (answer0.equals("NO")); //else without if
{
   System.out.print("Does it have flowers?");
}
else
{
System.out.print("Invalid");
}

Aucun commentaire:

Enregistrer un commentaire