到目前为止,我已经找到了单个字符,它甚至完成了大量的猜测,并向您显示猜测的单词。但我不能让它找到重复:(任何想法的家伙。 这是我的代码。
import java.util.*;
public class Hangman
{
public static void main (String[] args)
{
String tempGuess;
char blank = '_';
int amountOfGuesses = 0;
Scanner k = new Scanner(System.in);
System.out.print("Please Enter A Word : ");
String guessWord = k.nextLine();
char[] myArray = guessWord.toCharArray();
for (int i = 0; i < guessWord.length(); i++)
{
myArray[i] = blank;
}
System.out.println(myArray);
boolean whilerun = true;
while (whilerun == true)
{
System.out.print("Please Guess A Letter : ");
String guessLetter = k.nextLine();
amountOfGuesses = amountOfGuesses + 1;
int tempLG = guessWord.indexOf(guessLetter);
if (tempLG == -1)
{
System.out.println("Character Not In Word");
}
else
{
myArray[tempLG] = guessWord.charAt(tempLG);
}
System.out.println(myArray);
String tempGW = new String(myArray);
if (tempGW.equalsIgnoreCase(guessWord))
{
whilerun = false;
}
}
System.out.print("Well Done You Guessed The Word In " + amountOfGuesses + " trys.");
}
}
谢谢你们。
答案 0 :(得分:0)
如果您希望代码在重复的条目上通知您,请将您的else语句更改为以下内容。
else {
String val = String.valueOf(myArray);
if(val.contains(guessLetter)) {
System.out.println("Char already found");
}
myArray[tempLG] = guessWord.charAt(tempLG);
}
答案 1 :(得分:0)
/ * *图表 - Hangman.java,2013年11月18日下午6:08:55 * / import java.util。;
/ ** *班刽子手。 * * @author Rajakrishna V. Reddy * @version 1.0 * * / 公共班刽子手 {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args)
{
String tempGuess;
char blank = '_';
int amountOfGuesses = 0;
final Map<String, Integer> duplicateCharIndexMap = new HashMap<String, Integer>();
Scanner k = new Scanner(System.in);
System.out.print("Please Enter A Word : ");
String guessWord = k.nextLine();
char[] myArray = guessWord.toCharArray();
for (int i = 0; i < guessWord.length(); i++)
{
myArray[i] = blank;
}
System.out.println(myArray);
boolean whilerun = true;
while (whilerun)
{
System.out.print("Please Guess A Letter : ");
String guessLetter = k.nextLine();
amountOfGuesses = amountOfGuesses + 1;
final int tempLG = guessWord.indexOf(guessLetter, duplicateCharIndexMap.get(guessLetter) == null ? 0
: duplicateCharIndexMap.get(guessLetter));
if (tempLG == -1)
{
System.out.println("Character Not In Word");
}
else
{
duplicateCharIndexMap.put(guessLetter, tempLG + 1);
myArray[tempLG] = guessWord.charAt(tempLG);
}
System.out.println(myArray);
String tempGW = new String(myArray);
if (tempGW.equalsIgnoreCase(guessWord))
{
whilerun = false;
}
}
System.out.print("Well Done You Guessed The Word In " + amountOfGuesses + " trys.");
}
}
答案 2 :(得分:0)
package com.hangman;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Hangman
{
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args)
{
String tempGuess;
char blank = '_';
int amountOfGuesses = 0;
final Map<String, Integer> duplicateCharIndexMap = new HashMap<String, Integer>();
Scanner k = new Scanner(System.in);
System.out.print("Please Enter A Word : ");
String guessWord = k.nextLine();
char[] myArray = guessWord.toCharArray();
for (int i = 0; i < guessWord.length(); i++)
{
myArray[i] = blank;
}
System.out.println(myArray);
boolean whilerun = true;
boolean duplicateFlag = false;
while (whilerun)
{
duplicateFlag = true;
System.out.print("Please Guess A Letter : ");
String guessLetter = k.nextLine();
amountOfGuesses = amountOfGuesses + 1;
while (duplicateFlag)
{
final int tempLG = guessWord.indexOf(guessLetter, duplicateCharIndexMap.get(guessLetter) == null ? 0
: duplicateCharIndexMap.get(guessLetter));
if (tempLG == -1)
{
System.out.println("Character Not In Word");
duplicateFlag = false;
}
else
{
duplicateCharIndexMap.put(guessLetter, tempLG + 1);
myArray[tempLG] = guessWord.charAt(tempLG);
}
}
System.out.println(myArray);
String tempGW = new String(myArray);
if (tempGW.equalsIgnoreCase(guessWord))
{
whilerun = false;
}
}
System.out.print("Well Done You Guessed The Word In " + amountOfGuesses + " trys.");
}
}