2014-12-07 52 views
-4
// 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); 
     } 
    } 
+6

那麼究竟是什麼不工作? – home 2014-12-07 08:22:12

+0

有可能你的代碼會遇到無限循環。注意你在增加「我」的位置。 – Maroun 2014-12-07 08:26:42

回答

1

一個字符串中有你的類運行class.your主要方法沒有main method運行是不是隻是一個命名方法main.you可以調用它的主要方法有效的主要方法或添加string arg[]到list.probably參數,您忘了參數

更改添加

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

嗯,它不應該像這樣的代碼,因爲它只是修復了最明顯的問題,但留下了其他的錯誤。 – Tom 2014-12-17 23:48:31

+0

@Tom真的......? – 2014-12-17 23:51:30

+0

您對測試代碼有什麼看法? – Tom 2014-12-17 23:54:38