Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机列表,秘密短语Java项目_Java - Fatal编程技术网

随机列表,秘密短语Java项目

随机列表,秘密短语Java项目,java,Java,我的学校项目要求我修改上一次作业(代码如下),从至少10个单词的列表中随机抽取一个短语供用户猜测。我对此一无所知。任何帮助都将不胜感激。我知道我必须添加一个类来导入文本文件或列表,然后我需要修改一个循环以便它随机选择 import java.util.Scanner; // Allows the user to read different value types public class SecretPhrase { String phrase; // Sc

我的学校项目要求我修改上一次作业(代码如下),从至少10个单词的列表中随机抽取一个短语供用户猜测。我对此一无所知。任何帮助都将不胜感激。我知道我必须添加一个类来导入文本文件或列表,然后我需要修改一个循环以便它随机选择

    import java.util.Scanner; // Allows the user to read different value types

    public class SecretPhrase {

    String phrase; //
    Scanner scan = new Scanner(System.in);

    SecretPhrase(String phrase){
        this.phrase = phrase.toLowerCase();
    }

    public static void main(String args[]){
        SecretPhrase start = new SecretPhrase("Java is Great"); // The phrase the user will have identify
        start.go(); // Starts Program

    }

    void go(){
        String guess;
        String word="";
        String[] words = new String[phrase.length()]; // array to store all charachters
        ArrayList<String> lettersGuessed = new ArrayList();


         for(int i=0;i<phrase.length();i++){ 
            if(phrase.charAt(i)== ' '){words[i] = " ";} 
            else{words[i] = "*";} // Array that uses * to hide actual letters
        }

        int Gcount =0; // Records the count     
        while(!word.equals(phrase)){ // continues the loop
            word = "";
            int Lcount = 0;
            System.out.print("Guess a letter> ");
            guess = scan.next();


            for(int i=0;i<phrase.length();i++){ // Accounts for any attempts by user to use more than one charachter at a time.
                if((guess.charAt(0)+"").equals(phrase.charAt(i)+"")&&(lettersGuessed.indexOf(guess.charAt(0)+"")==-1)){

                    words[i] = ( guess.charAt(0))+ ""; 
                    Lcount++; 
                }
            }
            lettersGuessed.add(guess.charAt(0)+""); // Reveals the letter in phrase
            System.out.println("You found " + Lcount +" letters"); // Prints out the total number of times the charachter was in the phrase

            for(int i=0;i<words.length;i++){
                word=word+words[i]; 
            }
            System.out.println(word);
            Gcount ++;
        }
        System.out.println("Good Job! It took you " + Gcount + " guesses!" ); // Prints out result with total count

    }
}
import java.util.Scanner;//允许用户读取不同的值类型
公开课密语{
字符串短语//
扫描仪扫描=新扫描仪(System.in);
秘密短语(字符串短语){
this.phrase=phrase.toLowerCase();
}
公共静态void main(字符串参数[]){
SecretPhrase start=newsecretphrase(“Java很棒”);//用户将识别的短语
start.go();//启动程序
}
void go(){
字符串猜测;
字串=”;
String[]words=新字符串[phrase.length()];//存储所有字符的数组
ArrayList lettersGuessed=新建ArrayList();

对于(int i=0;i,在现有代码中,您正在创建一个SecretPhrase对象,其中包含要猜测的短语:

public static void main(String args[]){
    SecretPhrase start = new SecretPhrase("Java is Great");
    start.go(); // Starts Program

}
您应该将其替换为列表(ArrayList或LinkedList都可以),并用您的数据(由文件、用户输入或硬编码提供)填充它:

ArrayList短语=新建ArrayList();
//从文件中读取:
File File=新文件(“myPhrases.txt”);
FileReader=新的FileReader(文件);
BufferedReader br=新的BufferedReader(读卡器);
字符串短语=null;
而((短语=br.readLine())!=null){
添加(新的秘密短语(短语));
}

现在,在短语上使用Random.size()然后执行继续,或者如果您希望它是一系列短语,您可以创建一个排列并循环它们。我不确定您的要求是什么。

为什么要使用古老的API?如果您回答问题,请使用适当的代码。实际上,这是
列表短语=文件.行(路径.get(“myPhrases.txt”)).map收集(toList())
。首先,你必须尝试一些东西。
ArrayList<SecretPhrase> phrases = new ArrayList<SecretPhrase>();
//reading from file:
File file = new File("myPhrases.txt");
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String phrase = null;
while ((phrase = br.readLine()) != null) {
    phrases.add(new SecretPhrase(phrase));
}