Java 仅当输入为单字母时运行程序
我一直在为我的课程做一个java项目,我几乎完成了。该程序是一个刽子手游戏,用户输入一个字母,程序根据字母是否在单词中继续运行。我遇到的问题是,当用户输入多个字母、数字或符号时,程序会打印出一条语句,上面写着“无效输入,请重试”,并让用户再次输入一些内容,而不是将其显示为错过的尝试或“字母”不在单词中。这是我的密码:Java 仅当输入为单字母时运行程序,java,Java,我一直在为我的课程做一个java项目,我几乎完成了。该程序是一个刽子手游戏,用户输入一个字母,程序根据字母是否在单词中继续运行。我遇到的问题是,当用户输入多个字母、数字或符号时,程序会打印出一条语句,上面写着“无效输入,请重试”,并让用户再次输入一些内容,而不是将其显示为错过的尝试或“字母”不在单词中。这是我的密码: import java.util.Scanner; import java.util.Random; import java.io.*; public class Hangman
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class Hangman {
private Scanner in = new Scanner(System.in);
private boardPiece[] board = {new boardPiece(0),new boardPiece(0),new boardPiece(3),
new boardPiece(1),new boardPiece(2),new boardPiece(0)};
private String puzzle;
private String puzzleWord;
private int puzzleIndex;
private Random generator = new Random(System.currentTimeMillis());
private boolean win;
private boolean over;
private String correct = "";
private char guesses[] = new char[26];
private int guessed;
private int misses;
private String puzzles[] = new String[5];
public static void main(String [] args){
String letter;
String again = "y";
Hangman game = new Hangman();
try{
BufferedReader in =
new BufferedReader(new FileReader("puzzles.txt"));
int count = 0;
while (in.ready()) { //while there is another line in the input file
game.puzzles[count] = in.readLine(); //get line of data
count++; //Increment CWID counter
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
System.out.println("Welcome to HangMan!");
System.out.println("To play, guess a letter to try to guess the word.");
System.out.println("Every time you choose an incorrect letter another");
System.out.println("body part appears on the gallows. If you guess the");
System.out.println("word before you're hung, you win");
System.out.println("If you get hung, you lose");
System.out.println();
System.out.println("Time to guess...");
while(again.charAt(0) == 'y'){
game.displayGallows();
while(!game.over){
game.printBoard();
game.printLettersGuessed();
System.out.println("The word so far: " + game.puzzle);
System.out.println("Choose a letter: ");
letter = game.in.next();
game.guesses[game.guessed] = letter.charAt(0);
game.guessed++;
game.sort();
if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
game.correct = game.correct + letter.charAt(0);
game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
game.win = true;
game.over = true;
}
}
else
game.PrintWrongGuesses();
}
game.printBoard();
System.out.println("Solution: " + game.puzzleWord);
if(game.win)
System.out.println("Congratulations! You've solved the puzzle!");
else
System.out.println("You failed, failer!");
System.out.println();
System.out.println("Congratulations, you win!");
System.out.println("Do you want to play again? (y/n)");
again = game.in.next();
}
System.out.println("Goodbye!");
}
public void displayGallows(){
win = false;
over = false;
board[0].piece = " ______ ";
board[1].piece = " | | ";
board[2].piece = " | ";
board[3].piece = " | ";
board[4].piece = " | ";
board[5].piece = " _______| ";
puzzleIndex = generator.nextInt(puzzles.length);
puzzleWord = puzzles[puzzleIndex];
puzzle = puzzleWord.replaceAll("[A-Za-z]","-");
correct = "";
for(int x=0;x<26;x++)
guesses[x] = '~';
guessed = 0;
misses = 0;
}
public void printBoard(){
for(int x =0;x<6;x++)
System.out.println(board[x].piece);
}
public void PrintWrongGuesses(){
misses++;
System.out.println();
if(misses == 1){
board[2].piece = " 0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 2){
board[2].piece = " \\0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 3){
board[2].piece = " \\0/ | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 4){
board[3].piece = " | | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 5){
board[4].piece = " / | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 6){
board[4].piece = " / \\ | ";
System.out.println("Number of misses: " + misses);
over = true;
}
}
public void printLettersGuessed(){
System.out.print("Letters guessed already: ");
for(int x=0;x<26;x++){
if(guesses[x] != '~')
System.out.print(guesses[x] + " ");
}
System.out.println();
}
public void sort(){
boolean doMore = true;
while (doMore) {
doMore = false; // assume this is last pass over array
for (int i=0; i<guesses.length-1; i++) {
if (guesses[i] > guesses[i+1]) {
char temp = guesses[i];
guesses[i] = guesses[i+1];
guesses[i+1] = temp;
doMore = true; // after an exchange, must look again
}
}
}
}
class boardPiece{
public String piece;
public int total;
public int used;
boardPiece(int x){
used = 0;
total = x;
}
}
}
import java.util.Scanner;
导入java.util.Random;
导入java.io.*;
公共级刽子手{
专用扫描仪输入=新扫描仪(System.in);
私有板[]板={新板件(0)、新板件(0)、新板件(3),
新板件(1)、新板件(2)、新板件(0)};
私有字符串拼图;
私有字符串;
私有索引;
专用随机生成器=新随机(System.currentTimeMillis());
私人布尔赢;
私有布尔覆盖;
私有字符串correct=“”;
私有字符猜测[]=新字符[26];
私人智力猜测;
私有整数未命中;
私有字符串拼图[]=新字符串[5];
公共静态void main(字符串[]args){
字符串字母;
再次使用String=“y”;
刽子手游戏=新刽子手();
试一试{
BufferedReader in=
新的BufferedReader(新的文件阅读器(“puzzles.txt”);
整数计数=0;
while(in.ready()){//当输入文件中还有另一行时
game.puzzles[count]=in.readLine();//获取数据行
count++;//递增CWID计数器
}
in.close();
}
捕获(IOE异常){
System.out.println(“读/写时出错”);
}
System.out.println(“欢迎来到刽子手!”);
System.out.println(“要玩,猜一个字母,试着猜单词。”);
System.out.println(“每次您选择一个错误的字母,另一个错误的字母”);
System.out.println(“身体部分出现在绞刑架上,如果你猜的话”);
System.out.println(“悬而未决的话,你赢了”);
System.out.println(“如果你被挂了,你就输了”);
System.out.println();
System.out.println(“猜的时候了…”);
while(同样,charAt(0)='y'){
game.display绞刑架();
当(!游戏结束){
game.printBoard();
game.printLettersGuesed();
System.out.println(“到目前为止的单词:+game.puzzle”);
System.out.println(“选择一个字母:”);
字母=game.in.next();
game.guesses[game.guessed]=字母.charAt(0);
游戏.猜++;
game.sort();
if(game.puzzword.indexOf(letter.charAt(0))!=-1){
game.correct=game.correct+字母.charAt(0);
game.puzzle=game.puzzword.replaceAll(“[^”+game.correct+”],“-”;
if(game.puzzword.replaceAll(“[”+game.correct+“],”).length()=0){
game.win=true;
game.over=正确;
}
}
其他的
游戏。打印错误猜测();
}
game.printBoard();
System.out.println(“解决方案:“+game.puzzword”);
如果(游戏赢)
System.out.println(“祝贺你!你解决了这个难题!”);
其他的
System.out.println(“你失败了,失败者!”);
System.out.println();
System.out.println(“恭喜你,你赢了!”);
System.out.println(“您想再次播放吗?(y/n)”);
再次=game.in.next();
}
System.out.println(“再见!”);
}
公共绞刑架{
赢=假;
过度=错误;
板[0]。件=“\uuuuuuuuuuuuuuuuuuuuuuuuuu”;
板[1]。件=“| |”;
板[2]。件=“|”;
板[3]。件号=“|”;
板[4]。件=“|”;
电路板[5]。piece=“____124;”;
拼图索引=生成器.nextInt(拼图.长度);
拼图词=拼图[拼图索引];
拼图=拼图字.replaceAll(“[A-Za-z]”,“-”;
正确=”;
对于(intx=0;x试试这个
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class Hangman {
private Scanner in = new Scanner(System.in);
private boardPiece[] board = {new boardPiece(0),new boardPiece(0),new boardPiece(3),
new boardPiece(1),new boardPiece(2),new boardPiece(0)};
private String puzzle;
private String puzzleWord;
private int puzzleIndex;
private Random generator = new Random(System.currentTimeMillis());
private boolean win;
private boolean over;
private String correct = "";
private char guesses[] = new char[26];
private int guessed;
private int misses;
private String puzzles[] = new String[5];
public static void main(String [] args){
String letter;
String again = "y";
Hangman game = new Hangman();
try{
BufferedReader in =
new BufferedReader(new FileReader("puzzles.txt"));
int count = 0;
while (in.ready()) { //while there is another line in the input file
String input = in.readLine();
if(input.length() == 1){
game.puzzles[count] = ; //get line of data
count++; //Increment CWID counter
}
else{
System.out.println("INVALID INPUT");
}
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
System.out.println("Welcome to HangMan!");
System.out.println("To play, guess a letter to try to guess the word.");
System.out.println("Every time you choose an incorrect letter another");
System.out.println("body part appears on the gallows. If you guess the");
System.out.println("word before you're hung, you win");
System.out.println("If you get hung, you lose");
System.out.println();
System.out.println("Time to guess...");
while(again.charAt(0) == 'y'){
game.displayGallows();
while(!game.over){
game.printBoard();
game.printLettersGuessed();
System.out.println("The word so far: " + game.puzzle);
System.out.println("Choose a letter: ");
letter = game.in.next();
game.guesses[game.guessed] = letter.charAt(0);
game.guessed++;
game.sort();
if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
game.correct = game.correct + letter.charAt(0);
game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
game.win = true;
game.over = true;
}
}
else
game.PrintWrongGuesses();
}
game.printBoard();
System.out.println("Solution: " + game.puzzleWord);
if(game.win)
System.out.println("Congratulations! You've solved the puzzle!");
else
System.out.println("You failed, failer!");
System.out.println();
System.out.println("Congratulations, you win!");
System.out.println("Do you want to play again? (y/n)");
again = game.in.next();
}
System.out.println("Goodbye!");
}
public void displayGallows(){
win = false;
over = false;
board[0].piece = " ______ ";
board[1].piece = " | | ";
board[2].piece = " | ";
board[3].piece = " | ";
board[4].piece = " | ";
board[5].piece = " _______| ";
puzzleIndex = generator.nextInt(puzzles.length);
puzzleWord = puzzles[puzzleIndex];
puzzle = puzzleWord.replaceAll("[A-Za-z]","-");
correct = "";
for(int x=0;x<26;x++)
guesses[x] = '~';
guessed = 0;
misses = 0;
}
public void printBoard(){
for(int x =0;x<6;x++)
System.out.println(board[x].piece);
}
public void PrintWrongGuesses(){
misses++;
System.out.println();
if(misses == 1){
board[2].piece = " 0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 2){
board[2].piece = " \\0 | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 3){
board[2].piece = " \\0/ | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 4){
board[3].piece = " | | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 5){
board[4].piece = " / | ";
System.out.println("Number of misses: " + misses);
}
else if(misses == 6){
board[4].piece = " / \\ | ";
System.out.println("Number of misses: " + misses);
over = true;
}
}
public void printLettersGuessed(){
System.out.print("Letters guessed already: ");
for(int x=0;x<26;x++){
if(guesses[x] != '~')
System.out.print(guesses[x] + " ");
}
System.out.println();
}
public void sort(){
boolean doMore = true;
while (doMore) {
doMore = false; // assume this is last pass over array
for (int i=0; i<guesses.length-1; i++) {
if (guesses[i] > guesses[i+1]) {
char temp = guesses[i];
guesses[i] = guesses[i+1];
guesses[i+1] = temp;
doMore = true; // after an exchange, must look again
}
}
}
}
class boardPiece{
public String piece;
public int total;
public int used;
boardPiece(int x){
used = 0;
total = x;
}
}
}
import java.util.Scanner;
导入java.util.Random;
导入java.io.*;
公共级刽子手{
专用扫描仪输入=新扫描仪(System.in);
私有板[]板={新板件(0)、新板件(0)、新板件(3),
新板件(1)、新板件(2)、新板件(0)};
私有字符串拼图;
私有字符串;
私有索引;
专用随机生成器=新随机(System.currentTimeMillis());
私人布尔赢;
私有布尔覆盖;
私有字符串correct=“”;
私有字符猜测[]=新字符[26];
私人智力猜测;
私有整数未命中;
私有字符串拼图[]=新字符串[5];
公共静态void main(字符串[]args){
字符串字母;
再次使用String=“y”;
刽子手游戏=新刽子手();
试一试{
BufferedReader in=
新的BufferedReader(新的文件阅读器(“puzzles.txt”);
整数计数=0;
while(in.ready()){//当输入文件中还有另一行时
字符串输入=in.readLine();
if(input.length()==1){
game.puzzles[count]=;//获取数据行
count++;//递增CWID计数器
}
否则{
System.out.println(“无效输入”);
}
}
in.close();
}
捕获(IOE异常){
System.out.println(“读/写时出错”);
}
系统
if(letter.length() != 1) {
// do something to handle error
}
System.out.println("Choose a letter: ");
letter = game.in.next();
if(letter.length() == 1)
{
game.guesses[game.guessed] = letter.charAt(0);
game.guessed++;
game.sort();
if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){
game.correct = game.correct + letter.charAt(0);
game.puzzle = game.puzzleWord.replaceAll("[^"+game.correct+" ]","-");
if(game.puzzleWord.replaceAll("["+game.correct+" ]","").length() == 0){
game.win = true;
game.over = true;
}
}
else
game.PrintWrongGuesses();
}else
{
System.out.println("Invalid input, try again");
}
try{
BufferedReader in =
new BufferedReader(new FileReader("puzzles.txt"));
int count = 0;
String line = null;
while (in.ready()) { //while there is another line in the input file
line = in.readLine();
if (line.length() == 1 && Character.isLetter(line.charAt(0)) {
game.puzzles[count] = line; //get line of data
count++; //Increment CWID counter
} else {
// handle the else-case
}
}
in.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
if (!letter.matches("[a-zA-Z]{1}")) {
System.out.println("Invalid Input") {
else {
<your other code>
}