&引用;插入AssignmentOperator表达式“;错误JAVA
我不断收到错误“Insert AssignmentOperator expression to complete expression”我的程序基本上是选择动物所在的随机位置。我也在写数组和变量,对吗 问题出现在if语句中;具体代码为girafflocation[r.nextInt(girafflocation.length)]&引用;插入AssignmentOperator表达式“;错误JAVA,java,Java,我不断收到错误“Insert AssignmentOperator expression to complete expression”我的程序基本上是选择动物所在的随机位置。我也在写数组和变量,对吗 问题出现在if语句中;具体代码为girafflocation[r.nextInt(girafflocation.length)] 实际上,您并没有将随机位置的查找存储在任何位置,您希望这样: String tmp = giraffeLocation [r.nextInt(giraffeLocati
实际上,您并没有将随机位置的查找存储在任何位置,您希望这样:
String tmp = giraffeLocation [r.nextInt(giraffeLocation.length)];
System.out.println("Your giraffe is in, " + tmp );
固定代码应该是这样的
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 );
}
}
看起来你忘记了对arraygiraffeLocation[r.nextInt(giraffeLocation.length)]中该索引处的值做些什么了。你想在这里实现什么?@PradeepSimha我正试图从giraffeLocation数组打印出一个随机位置到控制台。但每次都有不同的位置。@user2548682,下面的答案是正确的。您没有将其存储到任何变量,因此您得到的是上述错误。
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 );
}
}