令牌“;”上的语法错误,{在随机字符串创建者中此令牌之后

我正在编写代码以使用字母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中,您不能直接在类中编写可执行语句。您需要在一种方法中移动代码。在方法/块之外仅允许声明变量。仅出于测试目的,将所有内容移至主要方法。尝试这个:

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

    Note: system.out.println(arrayList); will throw an error because there is no varaible called arrayList, i think it should be replaced with variable list. Also system should be System.

  • 涂鸦
    涂鸦 回复

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

    因此,此代码应位于method / constructor / block中。 例如方法

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

    Please refer this link for more details.

  • 我爱RMB
    我爱RMB 回复

    您只需要在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);
    
    }
    
    }
    
  • isunt
    isunt 回复

    当我按如下方式进行编码时,出现错误“无效令牌'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");
        }}
    
  • 夜伴歌笙
    夜伴歌笙 回复

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