2013-07-04 31 views
0

我不斷收到錯誤「插入AssignmentOperator表達式以完成表達式」我的程序基本上是選擇一個動物所在的隨機位置。我正在寫arrary和變量,對吧? ??「插入AssignmentOperator表達式」錯誤JAVA

問題發生在if語句中;具體的代碼是giraffeLocation [r.nextInt(giraffeLocation.length)];

import java.util.Scanner; 
import java.util.Random; 

public class animaltracker { 
    public static void main(String[] args){ 
      Scanner s = new Scanner(System.in); 
      Random r = new Random(); 

      char giraffe; 
      char rhino; 
      char hipopotamus; 

      String[] giraffeLocation; 
      giraffeLocation = new String[3]; 
      giraffeLocation[0] = ("Africa"); 
      giraffeLocation[1] = ("Russia"); 
      giraffeLocation[2] = ("Germany"); 





      System.out.println("Welcome to the animal tracker!"); 
      try { Thread.currentThread().sleep(800); } 
      catch (Exception e) { } 

      System.out.println("Which animal shall we be tracking today?"); 
      try { Thread.currentThread().sleep(800); } 
      catch (Exception e) { } 

      System.out.println("\nGiraffe?\nRhino?\nHipopotamus?"); 
      String animal = s.nextLine(); 
       if(animal.equalsIgnoreCase("giraffe")){ 
        **giraffeLocation [r.nextInt(giraffeLocation.length)];** 

        System.out.println("Your giraffe is in, " + giraffeLocation); 
       } 







     } 
    } 
+0

它看起來像你忘記了實際_do_數組中的那個索引的值 –

+0

_giraffeLocation [r.nextInt(giraffeLocation.length)] _你想在這裏實現什麼? –

+0

@MattBall喜歡什麼? – user2548682

回答

0

你實際上並沒有存儲在隨機位置的查找任何地方,你想是這樣的:

String tmp = giraffeLocation [r.nextInt(giraffeLocation.length)]; 

System.out.println("Your giraffe is in, " + tmp); 
0

固定的代碼應該是這樣..

public static void main(String[] args){ 
     Scanner s = new Scanner(System.in); 
     Random r = new Random(); 

     String[] giraffeLocation; 
     giraffeLocation = new String[3]; 
     giraffeLocation[0] = ("Africa"); 
     giraffeLocation[1] = ("Russia"); 
     giraffeLocation[2] = ("Germany"); 

     System.out.println("Welcome to the animal tracker!"); 
     try { Thread.currentThread().sleep(800); } 
     catch (Exception e) { } 

     System.out.println("Which animal shall we be tracking today?"); 
     try { Thread.currentThread().sleep(800); } 
     catch (Exception e) { } 

     System.out.println("\nGiraffe?\nRhino?\nHipopotamus?"); 
     String animal = s.nextLine(); 
      if(animal.equalsIgnoreCase("giraffe")){ 
       String location = ""; 
       try { 
        location = giraffeLocation [r.nextInt(giraffeLocation.length)]; 
       } catch (Exception e) { 
        System.out.println("EXCEPTIION"); 
       }     
       System.out.println("Your giraffe is in, " + location); 
      } 
    }