Java程序混乱不起作用的字符串

时间:2014-12-07 08:21:09

标签: java random

// java program to jumble a string
import java.util.*;
public class jumble
{
     public static void main()
     {
         Scanner s = new Scanner(System.in);
         String a ;
         System.out.println("Enter a word");
         a = s.nextLine();
         int length = a.length();
         Random r = new Random();
         String newstring = "" ;

         int array[] = new int[length];
         List l = Arrays.asList(array);
         int i = 1 ; 
         int arpos = 0 ; 
         while(i<= length)
         {
             int random = r.nextInt(length);
             if(!(l.contains(random)))
             {
                 newstring = newstring + a.charAt(random) ;
                 array[arpos] = random ;
                 l = Arrays.asList(array);
                 arpos ++ ;
                 i++ ;
                }
            }
            System.out.println(newstring); 
        }
    }

1 个答案:

答案 0 :(得分:1)

你的类中没有运行main method来运行class.your main方法不是一个有效的main方法,它只是一个名为main的方法。你可以在main方法中调用它或添加string arg[]到参数列表。可能你忘了添加参数

更改

public static void main()

public static void main(String arg[])

最后代码类应该是:

import java.util.*;
public class jumble
{
     public static void main(String arg[])//main method
     {
         Scanner s = new Scanner(System.in);
         String a ;
         System.out.println("Enter a word");
         a = s.nextLine();
         int length = a.length();
         Random r = new Random();
         String newstring = "" ;

         int array[] = new int[length];
         List l = Arrays.asList(array);
         int i = 1 ; 
         int arpos = 0 ; 
         while(i<= length)
         {
             int random = r.nextInt(length);
             if(!(l.contains(random)))
             {
                 newstring = newstring + a.charAt(random) ;
                 array[arpos] = random ;
                 l = Arrays.asList(array);
                 arpos ++ ;
                 i++ ;
                }
            }
            System.out.println(newstring);
        }
    }