令牌上的语法错误&quot ;;",{在随机字符串创建者中的此令牌后预期 [英] Syntax error on token ";", { expected after this token in Random string creator

查看:255
本文介绍了令牌上的语法错误&quot ;;",{在随机字符串创建者中的此令牌后预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,使用字母a,b和c生成随机3个字母的字符串。我收到错误消息令牌上的语法错误;在我创建随机变量的行之后的{在此令牌后预期(Random rand = new Random();)。我不知道为什么当我看起来很好的时候我会收到这个错误。

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.

我也收到错误消息:语法错误,插入}以完成ClassBody,在程序的最后一个括号之后。我几乎是积极的所有我的结束括号匹配,所以我不知道这个错误来自哪里。请帮助!!

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


推荐答案

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

In Java, you cannot directly write the executable statements in class. You need to move your code in a method. Only variables declaration is allowed outside the method/blocks. Just for the sake of testing, ,move everthing to main method. Try this:

  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 的变量,i认为应该用变量 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.

这篇关于令牌上的语法错误&quot ;;&quot;,{在随机字符串创建者中的此令牌后预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆