2013-03-09 93 views
0

我正在處理一個問題,其中有一個菜單選項1.要混洗單詞,2要洗牌單詞並嘗試通過更改數組索引號來修復它。在java中的單獨if語句中使用相同的值

我做了這部分if (input==1)洗牌的話。

我現在必須在if (input==2)部分採用相同的洗牌字,並嘗試修復它。任何人都可以指導我如何在這個塊if(input==1)中使用相同的值?

import java.util.Scanner; 
import java.io.*; 
import java.util.*; 

public class Project2 { 

    public static void main(String[] args) { 
     while (true) { 
      System.out.println("Select an item from below: \n"); 
      System.out.println("(1) Mix"); 
      System.out.println("(2) Solve"); 
      System.out.println("(3) Quit"); 

      int input; 
      Scanner scan = new Scanner(System.in); 
      input = scan.nextInt(); 

      String team; 
      String mix_word; 
      char orig_team[]; 
      char mix_team[]; 
      boolean Result; 
      // System.out.println(input); 
      if (input == 1) { 
       team = orig(); 
       System.out.println(team); 

       mix_word = mix(team); 
       System.out.println(mix_word); 

       orig_team = team.toCharArray(); 
       mix_team = mix_word.toCharArray(); 
       int arg_length = mix_team.length; 

      } 
      if (input == 2) { 

      } 
      if (input == 3) { 
       break; 
      } 

      if (input > 3 || input <= 0) { 

       System.out.println("input accurate numbers 1 or 2 or 3"); 
      } 
     } 
    } 

    static String mix(String Team) { 
     String word = shuffle(Team); 
     return word; 
    } 

    static String shuffle(String input) { 
     List<Character> characters = new ArrayList<Character>(); 
     for (char c : input.toCharArray()) { 
      characters.add(c); 
     } 

     StringBuilder output = new StringBuilder(input.length()); 
     while (characters.size() != 0) { 
      int randPicker = (int) (Math.random() * characters.size()); 
      output.append(characters.remove(randPicker)); 
     } 

     return output.toString(); 
    } 

    static String orig() 

    { 
     String[] lines = new String[1000];// Enough lines. 
     int counter = 0; 
     try { 
      File file = new File("input.txt");// The path of the File 
      FileReader fileReader1 = new FileReader(file); 
      BufferedReader buffer = new BufferedReader(fileReader1); 
      boolean flag = true; 
      while (true) { 
       try { 
        lines[counter] = buffer.readLine();// Store a line in the 
                 // array. 
        if (lines[counter] == null) {// If there isn't any more 
                // lines. 
         buffer.close(); 
         fileReader1.close(); 
         break;// Stop reading and close the readers. 

        } 
        counter++; 

       } catch (Exception ex) { 
        break; 
       } 
      } 

     } catch (FileNotFoundException ex) { 
      System.out.println("File not found."); 
     } catch (IOException ex) { 
      System.out.println("Exception ocurred."); 
     } 

     int pick; 
     Random rand = new Random(); 
     pick = rand.nextInt(counter) + 0; 
     return (lines[pick]); 
    } 
} 
+2

我建議你使用'switch'語句 – 2013-03-09 22:05:02

+0

@Peter Lawrey,我正在使用switch語句,仍然有問題。我在兩個塊中獲得了相同變量的不同值。它需要是一樣的。 – user1471980 2013-03-09 22:49:52

+0

我的猜測是你沒有在case語句之間放置'break'。如果你忘記這樣做,它將從一個案件運行到另一個案件。 – 2013-03-09 23:14:35

回答

1

這是switch聲明的,而病理性使用的情況下,但你可以利用下穿的和做到以下幾點:

switch(input) { 
case 1: 
    team = orig(); 
    System.out.println(team); 

    mix_word = mix(team); 
    System.out.println(mix_word); 

    orig_team = team.toCharArray(); 
    mix_team = mix_word.toCharArray(); 
    arg_length = mix_team.length; 

    // No break; here! 
case 2: 
    // do the rest of your thing as if it were case 2 
    break; 
case 3: 
    break; 
default: 
    System.out.println("input accurate numbers 1 or 2 or 3"); 
} 
+0

我做了這個switch語句。在案例2中,我需要能夠使用與case1相同的值。當我把2,值改變。 – user1471980 2013-03-09 22:31:56

+0

我這樣做,並在情況2,我有這個:System.out.println(mix_word);我得到這個錯誤:javac Project2.java Project2.java:41:變量mix_word可能未被初始化 \t \t \t \t System.out.println(mix_word); – user1471980 2013-03-09 22:43:49

+0

只是在'case 3'情況下初始化mix_word爲某個默認值。 – 2013-03-10 03:21:22

2

在每一個循環週期(它處理一個用戶輸入)你聲明你的變量,所以它們的範圍(訪問範圍)限於那個週期。

如果聲明的同時,外循環的變量,其範圍將整個循環伸過來(直到方法結束):

public static void main(String[] args) { 
    String team = ""; 
    String mix_word = ""; 
    char orig_team[] = null; 
    char mix_team[] = null; 
    boolean Result = false; 
    while (true) { 
     // ** your usual input handling here ** 
    } 
} 

此外,一定要對它們進行初始化(例如,使用默認值),否則程序將不能編譯。

另一種方法是創建成員變量或類變量,這會有自動初始化和更大範圍的優勢。