I do not want to accept same letter to guess and if does , want to show caution
ID: 3621341 • Letter: I
Question
I do not want to accept same letter to guess and if does , want to show caution and forceto type another try...
and If user got correct letter than I do want to show the letter instead of "_"
please help me out and please basic coding..
thank you in advance...
-----------------------------------------------------------------------------------------------------
import java.util.*;
public class HangmanTest
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
Dictionary dic = new Dictionary();
System.out.println("Enter your name to begin....");
name = keyboard.nextLine();
System.out.println(dic.s);
System.out.println(dic.t);
System.out.println("The length of word is " + dic.s.length());
for(int counter = 0; counter < dic.s.length(); counter++)
{
System.out.print("_ ");
}
System.out.println("");
System.out.println("");
//System.out.println("Enter the alphabet.");
//String input = keyboard.nextLine();
//keyboard.nextLine();
StringBuffer gLetter = new StringBuffer();
String letter;
int maxTries = 7;
while(maxTries > 0)
{
System.out.println("The letters that you have guessed are: " + gLetter);
System.out.print("Please enter a letter to guess: ");
letter = keyboard.next();
gLetter.append(letter + " ");
if(dic.s.indexOf(letter) > 1)
{
System.out.println("Please enter one letter at a time");
}
else if(dic.s.indexOf(letter) != (-1))
{
dic.s.indexOf(letter);
System.out.println("correct");
System.out.println(letter);
}
else
maxTries--;
System.out.println("You have " + maxTries + " wrong guesses left.");
}
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Dictionary
{
String words[] = {"dictionary", "technology", "federation", "restaurant", "collection", "enterprise", "television", "communicate", "exaggerate", "responsive"
, "automobile", "cappuccino"};
String s = words[(int)Math.floor(Math.random() * words.length)];
int t = s.length();
Dictionary()
{
}
}
Explanation / Answer
please rate - and see your messages
import java.util.*;
import java.lang.*;
public class HangmanTest
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
Dictionary dic = new Dictionary();
System.out.println("Enter your name to begin....");
name = keyboard.nextLine();
System.out.println(dic.s);
System.out.println(dic.t);
System.out.println("The length of word is " + dic.s.length());
StringBuffer word = new StringBuffer();
for(int i = 0; i < dic.s.length(); i++)
word.append("_ ");
System.out.println(word);
/*
for(int counter = 0; counter < dic.s.length(); counter++)
{
System.out.print("_ ");
}
*/
System.out.println("");
System.out.println("");
//System.out.println("Enter the alphabet.");
//String input = keyboard.nextLine();
//keyboard.nextLine();
StringBuffer gLetter = new StringBuffer();
boolean used;
String letter;
int maxTries = 7;
while(maxTries > 0)
{
System.out.println("The letters that you have guessed are: " + gLetter);
do
{used=false;
System.out.print("Please enter a letter to guess: ");
letter = keyboard.next();
if(gLetter.indexOf(letter)>=0)
{used=true;
System.out.println(letter +" has already been guessed");
}
}while(used);
gLetter.append(letter + " ");
if(dic.s.indexOf(letter) != (-1))
{
word.setCharAt(dic.s.indexOf(letter,dic.s.indexOf(letter) )*2,letter);
System.out.println("****"+dic.s.indexOf(letter,dic.s.indexOf(letter) ));
System.out.println("correct");
System.out.println(letter);
System.out.println(word);
}
else
maxTries--;
System.out.println("You have " + maxTries + " wrong guesses left.");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.