“插入AssignmentOperator表达式”错误JAVA

时间:2013-07-04 03:28:58

标签: java

我不断收到错误“插入AssignmentOperator表达式来完成表达式”我的程序基本上是选择一个动物所在的随机位置。我正在写的是arrary和变量,对吧???

问题出现在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 );
                }







        }
    }

2 个答案:

答案 0 :(得分:0)

你实际上并没有在任何地方存储随机位置的查找,你想要这样的东西:

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

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

答案 1 :(得分: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 );
            }
    }