2016-12-14 1 views
0
import java.util.Scanner; 

public class WordScrambler { 

    public static void main(String[] args) { 

    Scanner in = new Scanner(System.in); 
    int chosenWord; 
    String[] words = {"hogwash","Rudolph","yule-log","Eggnog","CandyCane","Christmas","Fruitcake","gingerbread","Krampus","nutcracker"}; 

    System.out.println("Pick a number between 1-10"); 
    chosenWord=in.nextInt(); 

    String a=words[chosenWord].substring(0,words[chosenWord].length()/2); 
    String b=words[chosenWord].substring(words[chosenWord].length()/2); 
    String c=b+a; 
    int x=(int)(Math.random()*(words[chosenWord].length()-1))+1; 
    String d=c.substring(0, words[chosenWord].length()-x); 
    String e=c.substring(words[chosenWord].length()-x); 
    String f=e+d; 
    System.out.println(f); 
    } 
} 

これは私がこれまで行ってきたことです。 私はこれ以上スクランブルする方法を見つけることができません。 今のところ出力は:Hogwash:shhogwaです。 Scramblesが唯一の単語です。Math.randomで語をスクランブラーにする

答えて

0
public static void main(String[] args) { 

    Scanner in = new Scanner(System.in); 
    int chosenIndex; 
    String[] words = { "hogwash", "Rudolph", "yule-log", "Eggnog", "CandyCane", "Christmas", "Fruitcake", 
      "gingerbread", "Krampus", "nutcracker" }; 

    System.out.println("Pick a number between 1-10"); 
    chosenIndex = in.nextInt(); 

    String chosenWord = words[chosenIndex - 1]; 
    System.out.println(chosenWord); 

    StringBuilder scrambled = new StringBuilder(); 
    List<Character> letters = new ArrayList<Character>(); 
    for (char x : chosenWord.toCharArray()) { 
     letters.add(x); 
    } 
    while (letters.size() != 0) { 
     int random = (int) (Math.random() * letters.size()); 
     scrambled.append(letters.remove(random)); 
    } 

    System.out.println(scrambled.toString()); 
} 

使用意味のある変数名のが

を参照するものを、その明確なので、
関連する問題