我开始学习Java已经3个月了。
我对Java很陌生。
作为课程工作的一部分,我获得了使用instantiable
课程开发Hangman游戏的Java源代码的课堂作业。
所有人都使用Filereaders
来读取用户的输入,但我尝试使用Scanner
类来解决它,以使其更简单直接。
但我得到的错误是"类接口或枚举预期"。我尝试过多次编辑代码,但没有用。请告诉我我做错了什么。
预期的输出是让玩家知道他每次输入新信时都会显示他的生活,猜测和其他统计数据。
谢谢。
这是我的代码:
主要课程
import java.util.Scanner;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Main {
int lives = 7;
Scanner sc = new Scanner(System.in);
public static void main(String args[]) {
String name = "", response = "y";
HangmanApp appobj = new HangmanApp();
System.out.println("Please Enter your name: ");
int games_played = 0, games_won = 0, games_lost = 0;
name = sc.nextLine();
while ((response.equalsIgnoreCase("y")) || (response.equalsIgnoreCase("yes"))) {
games_played++;
if (appobj.runme(randInt()) ) {
System.out.println("Congrats " + name + " you have won this game!");
games_won++;
} else {
System.out.println("Sorry " + name + " you have lost this game!");
games_lost++;
}
System.out.println("Do you want to play again? Type y or n ");
response = sc.nextLine();
}
// provide statistics after user says no to continue
System.out.println("Game Statistics-");
System.out.println("Game played:" + games_played);
System.out.println("Game won: " + games_won);
System.out.println("Game lost: " + games_lost);
}
public static int randInt() {
Random rand = new Random();
int randomNum = rand.nextInt(5) + 1;
return randomNum;
}
}
可实例化的课程 - #HangmanApp
import java.util.Scanner;
import java.util.ArrayList;
public class HangmanApp {
String word_list[] = { "programming", "exhaustive", "violin", "selection", "repetition", "serendipity",
"alabama", "alaska", "arizona", "arkansas", "california", "colorado",
"connecticut", "delaware", "florida", "georgia", "hawaii", "idaho",
"illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana",
"maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi" };
Scanner scan = new Scanner(System.in));
public boolean runme(int index) {
String correct_word = word_list[index].toString();
//System.out.println("Word: " + correct_word);
int corr_wrd_count = correct_word.length();
int left_lives = 7;
ArrayList<String> letter_chosen = new ArrayList<String>();
ArrayList<String> hangman = new ArrayList<String>();
try {
// Insert blank chars
for(int i =0; i < corr_wrd_count; i++)
{
hangman.add("_");
}
while (left_lives != 0 ) {
System.out.print("Guess a letter: ");
try {
String letter_entered = scan.nextLine().toLowerCase();
if (letter_entered.length() == 1) {
if(! isNumeric(letter_entered)) {
if (letter_chosen.contains(letter_entered)) {
System.out.println("You have already entered this character. Try another.");
}
else {
letter_chosen.add(letter_entered);
if (correct_word.contains(letter_entered)) {
// Insert the char at all instances of the string
for (int i = -1; (i = correct_word.indexOf(letter_entered, i + 1)) != -1; ) {
hangman.set(i, letter_entered);
}
}
else {
// Lost life
left_lives--;
}
}
// print status
System.out.println("------------------------");
System.out.println("Lives: " + left_lives);
System.out.println("Word: " + String.join(" ", hangman));
System.out.println("Letters chosen so far: " + letter_chosen);
// Check if array has any pending -
if (! hangman.contains("_")) {
return true;
}
}
else {
System.out.println("Buddy this is Hangman not betting... Try entering letter ->");
}
}
else {
System.out.println("Please be patient and enter only 1 letter...");
}
}
return false;
}
} // end while
}
return false;
}
return false;
}
public static boolean isNumeric(String str)
{
double d = Double.parseDouble(str);
{
return false;
}
return true;
}
}
答案 0 :(得分:0)
你的isNumeric方法很可疑
{
double d = Double.parseDouble(str);
{
return false;
}
return true;
}
你回归真假吗?这可能是导致错误的原因。你有太多的花括号关闭。正确缩进代码并检查您是否正确打开和关闭大括号。
此外,在Main类中,您有两个非静态变量,因此无法从静态main方法访问它们。
答案 1 :(得分:0)
没有编译它,但我可以在Main
类
声明扫描仪在public static void main(String[] str){
}
之外,然后在里面使用它会在你静态引用非静态字段时给出错误
这就像在静态方法中调用实例方法和字段一样,这是无法完成的,因为实例字段和方法在没有对象的情况下不存在,而在main方法内部则没有此对象。您必须改为创建该类的实例,然后调用该实例上的方法。
public class Main {
int lives = 7;
Scanner sc = new Scanner(System.in);//this sc can't be called inside main
public static void main(String args[]) {
String name = "", response = "y";
HangmanApp appobj = new HangmanApp();
System.out.println("Please Enter your name: ");
int games_played = 0, games_won = 0, games_lost = 0;
name = sc.nextLine();