在java中的单独if语句中使用相同的值

问题描述:

我正在处理一个问题,其中有一个菜单选项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

这是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

在每一个循环周期(它处理一个用户输入)你声明你的变量,所以它们的范围(访问范围)限于那个周期。

如果声明的同时,外循环的变量,其范围将整个循环伸过来(直到方法结束):

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 ** 
    } 
} 

此外,一定要对它们进行初始化(例如,使用默认值),否则程序将不能编译。

另一种方法是创建成员变量或类变量,这会有自动初始化和更大范围的优势。