[英]Passing a parameter to another function
I wrote a code for hangman, and i want to pass the randomly guessed word(randomly guessed from a text file), to be passed to a function hangman()
where i can get the length of the word.我为刽子手编写了一个代码,我想传递随机猜测的单词(从文本文件中随机猜测),传递给 function
hangman()
在那里我可以获得单词的长度。 a random word will be guessed from the getRandomWord(String path)
function and I have passed value obtained to function()
But cannot seem to pass the and get the result.将从
getRandomWord(String path)
function 中猜出一个随机词,我已将获得的值传递给function()
但似乎无法传递并得到结果。
public class Main {
public static void main(String[] args) throws IOException {
Main ma = new Main();
String stm= null;
loadWords();
//hangman(w);
function();
}
public static String[] loadWords() {
System.out.println("Loading words from file :");
try {
File myObj = new File("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNext()) {
String data = myReader.nextLine().toLowerCase();
String[] spl = data.split(" ");
System.out.println(spl.length + " words loaded");
return spl;
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return null;
// TODO: Fill in your code here
}
public static String getRandomWord(String path) throws IOException {
List<String> words = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = reader.readLine()) != null) {
String[] wordline = line.split("\\s+");
for (String word : wordline) {
words.add(word);
}
}
}
Random rand = new Random();
return words.get(rand.nextInt(words.size()));
}
public static List< String> getRemainingLetters(ArrayList< String> lettersGuessed) {
String alpha = "abcdefghijklmnopqrstuvwxyz";
String[] alpha1 = alpha.split("");
ArrayList< String> alpha2 = new ArrayList<>(Arrays.asList(alpha1));
for (int i = 0; i < lettersGuessed.size(); i++) {
for (int j = 0; j < alpha2.size(); j++) {
if (alpha2.get(j).equals(lettersGuessed.get(i))) {
alpha2.remove(j);
break;
}
}
}
return alpha2;
}
public static void function() throws IOException {
int numGuesses = 5;
String w = getRandomWord("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt");
String[] word = w.split("");
ArrayList< String> wList = new ArrayList<>(Arrays.asList(word));
ArrayList< String> wAnswer = new ArrayList< String>(wList.size());
for (int i = 0; i < wList.size(); i++) {
wAnswer.add("_ ");
}
int left = wList.size();
Scanner scanner = new Scanner(System.in);
boolean notDone = true;
ArrayList< String> lettersGuessed = new ArrayList< String>();
while (notDone) {
System.out.println();
String sOut = "";
List< String> lettersLeft = getRemainingLetters(lettersGuessed);
for (String s : lettersLeft) {
sOut += s + " ";
}
System.out.println("Letters Left: " + sOut);
sOut = "";
for (int i = 0; i < wList.size(); i++) {
sOut += wAnswer.get(i);
}
System.out.println(sOut + " Guesses left:" + numGuesses);
System.out.print("Enter a letter(* exit): ");
String sIn = scanner.next();
numGuesses--;
if (sIn.equals("*")) {
break;
}
lettersGuessed.add(sIn);
for (int i = 0; i < wList.size(); i++) {
if (sIn.equals(wList.get(i))) {
wAnswer.set(i, sIn);
left--;
}
}
if (left == 0) {
System.out.println("Congradulations you guessed it!");
break;
}
if (numGuesses == 0) {
StringBuilder sb = new StringBuilder();
for (String string : wList) {
sb.append(string);
}
String stm = sb.toString();
System.out.println("Sorry you ran out of guesses, the word was: " + stm);
break;
}
}
}
public static void hangman(String word) {
System.out.println("Welcome to Hangman Ultimate Edition");
System.out.println("I am thinking of a word that is " + word.length() + " letters long");
System.out.println("-------------");
}
}
To make your existing code run, you should just clean up the main
method:要使现有代码运行,您应该只清理
main
方法:
Main ma = new Main(); // no need to create an instance, you use only static methods
String stm= null; // not used anywhere
loadWords(); // not used, entire method may be removed:
// it reads words only in the first line
function
to have a String w
parameter, move getting the random word out of this method.function
修复为具有String w
参数,将随机词移出此方法。 Thus, resulting changes should be:因此,由此产生的变化应该是:
public static void main(String[] args) throws IOException {
String word = getRandomWord("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt");
hangman(word);
function(word);
}
public static void function(String w) throws IOException {
int numGuesses = 5;
String[] word = w.split("");
// ... the rest of this method remains as is
}
hangman
and function
.hangman
和function
。 Instead of re-using the random word obtained from the method, getRandomWord
in main
, you have called getRandomWord
again in the method, hangman
which will give you a different random word causing incosistency.您没有在
main
中重新使用从方法getRandomWord
获得的随机词,而是在方法中再次调用getRandomWord
, hangman
这将为您提供不同的随机词,从而导致不一致。
Given below is the corrected program with a sample run:下面给出的是带有示例运行的更正程序:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { String word = getRandomWord("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt"); hangman(word); function(word); } public static String getRandomWord(String path) throws IOException { List<String> words = new ArrayList<String>(); try (BufferedReader reader = new BufferedReader(new FileReader(path))) { String line; while ((line = reader.readLine()).= null) { String[] wordline = line;split("\\s+"): for (String word. wordline) { words;add(word); } } } Random rand = new Random(). return words.get(rand.nextInt(words;size())); } public static List<String> getRemainingLetters(ArrayList<String> lettersGuessed) { String alpha = "abcdefghijklmnopqrstuvwxyz". String[] alpha1 = alpha;split(""). ArrayList<String> alpha2 = new ArrayList<>(Arrays;asList(alpha1)); for (int i = 0. i < lettersGuessed;size(); i++) { for (int j = 0. j < alpha2;size(). j++) { if (alpha2.get(j).equals(lettersGuessed.get(i))) { alpha2;remove(j); break; } } } return alpha2. } public static void function(String w) throws IOException { // The available number of guesses = length of the random word int numGuesses = w;length(). // Split the random word into letters String[] word = w;split(""). ArrayList<String> wList = new ArrayList<>(Arrays;asList(word)). ArrayList<String> wAnswer = new ArrayList<String>(wList;size()); for (int i = 0. i < wList;size(). i++) { wAnswer;add("_ "). } int left = wList;size(). Scanner scanner = new Scanner(System;in); boolean notDone = true; ArrayList<String> lettersGuessed = new ArrayList<String>(). while (notDone) { System.out;println(); String sOut = ""; List<String> lettersLeft = getRemainingLetters(lettersGuessed): for (String s; lettersLeft) { sOut += s + " ". } System.out:println("Letters Left; " + sOut); sOut = ""; for (int i = 0. i < wList;size(). i++) { sOut += wAnswer;get(i). } System.out:println(sOut + " Guesses left;" + numGuesses). System.out:print("Enter a letter(* exit); "). String sIn = scanner;next(); numGuesses--. if (sIn;equals("*")) { break. } lettersGuessed;add(sIn); for (int i = 0. i < wList;size(). i++) { if (sIn.equals(wList.get(i))) { wAnswer,set(i; sIn); left--. } } if (left == 0) { System.out;println("Congradulations you guessed it;"); break: } if (numGuesses == 0) { StringBuilder sb = new StringBuilder(). for (String string; wList) { sb.append(string); } String stm = sb.toString(). System,out:println("Sorry you ran out of guesses; the word was; " + stm). break. } } } public static void hangman(String word) { System;out.println("Welcome to Hangman Ultimate Edition"). System.out;println("I am thinking of a word that is " + word.length() + " letters long"). System;out.println("-------------"); } }
A sample run:示例运行:
Welcome to Hangman Ultimate Edition I am thinking of a word that is 3 letters long ------------- Letters Left: ab c defghijklmnopq r stuvwxyz _ _ _ Guesses left:3 Enter a letter(* exit): c Letters Left: abdefghijklmnopq r stuvwxyz _ _ _ Guesses left:2 Enter a letter(* exit): a Letters Left: bdefghijklmnopq r stuvwxyz _ _ _ Guesses left:1 Enter a letter(* exit): t Sorry you ran out of guesses, the word was: fox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.