我正在编写代码以使用字母a,b和c生成随机的3个字母字符串。我在创建随机变量的行(Random rand = new Random();)行后收到错误消息“令牌“;”上的语法错误,{预期在此令牌之后”。我不知道为什么在我觉得很好时会收到此错误。
我还收到错误消息:语法错误,在程序中的最后一个括号之后插入“}”以完成ClassBody。我几乎认为我所有的右括号都匹配,所以我不知道此错误来自何处。请帮忙!!
import java.util.*;
public class Orders {
String alphabet = "abc";
ArrayList<String> list = new ArrayList<String>();
int n = alphabet.length();
Random rand = new Random();
for (int i = 0; i < 10000; i++){
char a = alphabet.charAt(rand.nextInt(n));
char b = alphabet.charAt(rand.nextInt(n));
char c = alphabet.charAt(rand.nextInt(n));
String s = Character.toString(a) + Character.toString(b) + Character.toString(c);
if(list.indexOf(s) == -1){
list.add(s);
}
}
system.out.println(arrayList);
}
在Java中,您不能直接在类中编写可执行语句。您需要在一种方法中移动代码。在方法/块之外仅允许声明变量。仅出于测试目的,将所有内容移至主要方法。尝试这个:
Note:
system.out.println(arrayList);
will throw an error because there is no varaible calledarrayList
, i think it should be replaced with variablelist
. Alsosystem
should beSystem
.在Java中,您不能简单地将循环和其他操作编码为类定义的一部分,而是作为类内的方法/构造函数/块定义
因此,此代码应位于method / constructor / block中。 例如方法
Please refer this link for more details.
您只需要在main函数中编写代码。 您只是错过了主要功能。
您的密码
正确的代码
当我按如下方式进行编码时,出现错误“无效令牌'manage()...”。那是当我直接将代码编写到类中时。
//之前
public class test{ WebDriver driver=new FirefoxDriver(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.get("http://http://www.extjs-tutorial.com/live-examples/extjs4/CRUD-in-Form-model/default.htm"); }
'在将代码移至方法后,它开始工作了
//后
在该代码中出现语法错误的原因是,您有一个额外的括号。 到上面的错误代码!