0

我正在解决一个问题,我有一个菜单选项 1. 随机播放单词,2 获取随机播放单词并尝试通过更改数组索引号来修复它。

我做了这部分if (input==1)洗牌。

我现在必须在if (input==2)部分中使用相同的 shuffle 单词并尝试修复它。谁能指导我如何在这个块中使用相同的值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]);
    }
} 
4

2 回答 2

2

在每个循环周期(处理单个用户输入)中,您声明变量,因此它们的范围(访问范围)仅限于该循环。

如果您在 while 循环之外声明变量,它们的范围将延伸到整个循环(直到方法结束):

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

还要确保初始化它们(例如使用默认值),否则程序将无法编译。

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

于 2013-03-09T22:25:49.530 回答
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");
}
于 2013-03-09T22:19:57.687 回答