9

I am writing code to generate a random 3 letter strings using the letters a, b, and c. I am getting the error message "Syntax error on token ";", { expected after this token" after the line where i create the random variable (Random rand = new Random();). I do not know why I am getting this error when it looks fine to me.

I am also getting the error message: Syntax error, insert "}" to complete ClassBody, after the last bracket in the program. I am almost postive all my closing brackets match up so I do not know where this error is coming from. PLEASE HELP!!

 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);
}
4

5 回答 5

20

在 Java 中,您不能直接在类中编写可执行语句。您需要在方法中移动代码。在方法/块之外只允许声明变量。只是为了测试,将所有内容移至 main 方法。尝试这个:

  public class Orders {

        public static void main(String argsp[]) {
            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(list);
        }

}

注意system.out.println(arrayList); 会抛出错误,因为没有调用arrayList变量,我认为应该用变量替换list。也system应该是System

于 2013-09-16T20:08:42.750 回答
3

在java中,你不能简单地将循环和其他动作编码为类定义的一部分,而是作为类内的方法/构造函数/块定义

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);  

所以这段代码应该在方法/构造函数/块中。
例如在方法中

public void printList(){
 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);  
}

请参阅此链接了解更多详情。

于 2013-09-16T20:10:28.460 回答
3

您只需要在main 函数中编写代码。
你只是错过了主要功能

你的代码

import java.util.*;
 public class Orders {//Include main function

 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);
}

正确的代码

package testprob;
import java.util.*;
public class testprob {
public static void main(String arrg[]) {//you need to add main function 
     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(list);

}

}
于 2018-05-26T07:18:30.570 回答
1

当我编码如下时,我收到一个错误“无效的令牌'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"); }'

在我将我的代码移到一个方法之后,它起作用了

//后

 public class test{
    public void setup(){
    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");
    }}
于 2015-07-15T16:10:21.067 回答
-2

您在该代码中收到语法错误的原因是您有一个额外的括号。到上面的代码错误!

于 2018-03-22T17:09:10.707 回答