方法android工作室中的错误



*我在android Studio编码,方法"secretWord";在类内部生成一个错误,但我不知道为什么。。。该方法在类"之外实现;Myclass";。你能帮我吗

package com.example.logic;
public  class Myclass {
String []xxx={"abc","def","ghi"};
public MyClass(String[] xxx) {
this.xxx = xxx;
}
secretWord(xxx) ;
}
public  String secretWord(String [] xxx) {
int index;
index=(int) Math.floor(Math.random()*3);
return xxx[index];
}

检查代码风箱中的注释,要使此代码正常工作,需要修改许多内容。

class Myclass {
String []xxx={"abc","def","ghi"};
public Myclass(String[] xxx) { // first, constructor method should have same name with class name
this.xxx = xxx;
secretWord(xxx); // second, this statement should be inside a method
}

public  String secretWord(String [] xxx) { // third, this method should be put inside a class
int index;
index=(int) Math.floor(Math.random()*3);
return xxx[index];
}
}

更新

对于在多个类中使用,您应该将方法分离到另一个类中

class Myclass {
String []xxx={"abc","def","ghi"};
public Myclass(String[] xxx) { // first, constructor method should have same name with class name
this.xxx = xxx;
SecretUtil.secretWord(xxx); // second, this statement should be inside a method
}
}
static class SecretUtil {
public static String secretWord(String [] xxx) { // third, this method should be put inside a class
int index;
index=(int) Math.floor(Math.random()*3);
return xxx[index];
}
}

相关内容

最新更新