I';在JavaEclipse中编译这段代码时,我遇到了一个错误。这是一个刽子手计划
这是我的密码。我将非常感谢任何帮助我解决这个问题的人。 非常感谢你。我刚开始写代码。我正在尝试做一个刽子手项目,我觉得我做得对,但它就是无法编译。请帮忙I';在JavaEclipse中编译这段代码时,我遇到了一个错误。这是一个刽子手计划,java,eclipse,Java,Eclipse,这是我的密码。我将非常感谢任何帮助我解决这个问题的人。 非常感谢你。我刚开始写代码。我正在尝试做一个刽子手项目,我觉得我做得对,但它就是无法编译。请帮忙 import java.io.*; import java.util.*; public class Hangman { public static void main(String[] args) throws FileNotFoundException { String[] dictionary = load
import java.io.*;
import java.util.*;
public class Hangman {
public static void main(String[] args) throws FileNotFoundException {
String[] dictionary = loadWords();
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to Hangman!");
System.out.println("How many tries");
String word = chooseWord(dictionary);
System.out.println("OK, I've choseen my word. Start guessing");
System.out.println(word);
String wordguess = kb.next();
char guess = wordguess.charAt(0);
boolean[] bool = new boolean[26];
processGuess(guess, word, bool);
// FINISH IMPLEMENTING THIS!
}
// Loads a list of words from a file and returns the lsit as a String[].
// DO NOT ALTER THIS IMPLEMENTATION!!
public static String[] loadWords() throws FileNotFoundException {
ArrayList<String> wordList = new ArrayList<String>();
Scanner wordFile = new Scanner(new File("dictionary.txt"));
String line;
while (wordFile.hasNextLine()) {
line = wordFile.nextLine();
if(!line.equals(""))
wordList.add(line);
}
String[] result = new String[1];
return wordList.toArray(result);
}
// Takes an array of strings that represents the valid words as a parameter.
// Chooses one such word randomly and returns it.
public static String chooseWord(String[] dict) {
Random r = new Random();
int numword = r.nextInt(dict.length);
String hangword = dict[numword];
return hangword;
}
// Checks if a player has won the game
// Returns true only if all letters in the word have been guessed
public static boolean hasWon(String word, boolean[] guesses) {
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i)-'a'] == false){
return false;
}
}
return true;
}
// Prints out the pattern of letters in the secret word based on the word
// and the letters that have been guessed.
// Prints any letter that has already been guessed and a _ for a letter that
// has not been guessed.
public static void printPattern(String word, boolean[] guesses) {
StringBuilder pattern = new StringBuilder();
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i) - 'a']) {
pattern.append(word.charAt(i));
}
else {
pattern.append("_");
}
pattern.append(" ");
}
System.out.println(pattern.toString());
}
// Handles a guess by marking the letter as guessed and returns the number of
// tries to be charged: 0 if the guessed letter is in the word and 1 otherwise.
public static int processGuess(char guess, String word, boolean[] guesses) {
int holdvalue = 0;
if (guesses[guess - 'a']) {
return 0;
}
for(int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess) {
holdvalue ++;
}
}
if (holdvalue >= 0){
return 0;
}
else {
return 1;
}
}
}
import java.io.*;
导入java.util.*;
公共级刽子手{
公共静态void main(字符串[]args)引发FileNotFoundException{
String[]dictionary=loadWords();
扫描仪kb=新扫描仪(System.in);
System.out.println(“欢迎来到刽子手!”);
System.out.println(“尝试次数”);
字符串单词=选择单词(字典);
System.out.println(“好的,我已经看到我的单词了,开始猜测”);
System.out.println(word);
字符串wordguess=kb.next();
char guess=wordguess.charAt(0);
布尔值[]布尔值=新布尔值[26];
进程猜测(猜测、单词、布尔);
//完成这个!
}
//从文件加载单词列表,并将lsit作为字符串[]返回。
//不要更改此实现!!
公共静态字符串[]loadWords()引发FileNotFoundException{
ArrayList wordList=新建ArrayList();
Scanner wordFile=new Scanner(新文件(“dictionary.txt”);
弦线;
while(wordFile.hasNextLine()){
line=wordFile.nextLine();
如果(!line.equals(“”)
添加(行);
}
字符串[]结果=新字符串[1];
返回wordList.toArray(结果);
}
//将表示有效单词的字符串数组作为参数。
//随机选择一个这样的单词并返回它。
公共静态字符串chooseWord(字符串[]dict){
随机r=新随机();
int numword=r.nextInt(dict.length);
字符串hangword=dict[numword];
返回行话;
}
//检查玩家是否赢得了比赛
//仅当单词中的所有字母都被猜到时才返回true
公共静态布尔值haswen(字符串字,布尔值[]猜测){
for(int i=0;i=0){
返回0;
}
否则{
返回1;
}
}
}
我看到了几个问题
String[] result = new String[1];
由于已声明大小为1的数组,因此只能从字典中加载一个单词。这可能是
String[] result = new String[wordList.size()];
如果您从用户处获取字符串输入,并且字符串存储在数组中,您可以简单地比较它们,可能不需要逐个字符进行比较(如果我没有误解您的目标)
您可能想解释确切的问题,这将帮助我们帮助您。请发布您的编译错误。如果您能指出代码中哪一行出现编译问题,可能会对我们有所帮助。我们可以很容易地找到你的问题。我没有看到任何编译器的错误。它编译的很好,只是没有按预期工作显然。您的编译错误是什么?这不是家庭作业吗?每当我试图在java.io.FileInputStream.open(本机方法)的java.io.FileInputStream(未知源代码)的java.util.Scanner(未知源代码)的线程“main”java.io.FileNotFoundException:dictionary.txt(系统找不到指定的文件)中编译代码异常时,就会发生这种情况在Hangman.main(Hangman.java:9)中加载新字符串[1]代码>事情很好:重要的是为
List
的toArray()方法提供一个String
数组(任意大小)(尽管我认为wordList.toArray(新字符串[]{})
更惯用)。
String a = "something";
String b = get from array.
if( a!=null && a.equals(b))
{ }