1
Good evening, I’m new here in the forum and also in programming, I’m seeing java at the moment, I have a job that consists of a game of hangman in java, part of the class I’ve done (I hope you’re right). I’m having trouble checking if the letter was typed, if it belongs to the word.
package classes;
import java.util.Random;
public class Palavras {
private String[] palavras = {"cachorro", "baleia", "arara", "chevrolet", "volksvagen", "samsung", "colher", "tablet", "espanha", "senegal"};
private String palavra;
private String letra;
private Random palavraSorteada = new Random();
public Palavras() {
palavra = palavras[palavraSorteada.nextInt(palavras.length)];
}
public String getPalavra() {
return palavra;
}
public void setPalavra(String palavra) {
this.palavra = palavra;
}
public String getLetra() {
return letra;
}
public void setLetra(String letra) {
this.letra = letra;
}
public char[] codificarPalavra() {
char[] traco = new char[palavra.length()];
for (int i = 0; i < palavra.length(); i++) {
traco[i] ='_';
}
return traco;
}
public String dica() {
String dica="";
if (palavra.equals(palavras[0]) || palavra.equals(palavras[1]) || palavra.equals(palavras[2])) { //etc
dica = "Animal";
}
if(palavra.equals(palavras[3]) || palavra.equals(palavras[4]) || palavra.equals(palavras[5])){
dica = "Marca";
}
if(palavra.equals(palavras[6])){
dica = "cozinha";
}
if(palavra.equals(palavras[7])){
dica = "eletrônico";
}
if(palavra.equals(palavras[8]) || palavra.equals(palavras[9])){
dica = "país";
}
return dica;
}
public boolean verifica(char letra) {
// char[] l = letra.toCharArray();
for (int i = 0; i < palavra.length(); i++) {
if (palavra.charAt(i) == letra) {
//palavra[i] = letra; vc só quer verificar se existe.
return true;
}
}
return false;
}
}
the point is I tried to make it work in two ways and did not succeed.
and the other is type the letter in a Textfield and check with the word, but both ways.
The difficulty is in the logical part of the implementation of the programme.
Okay, but there your doubt is very wide. What is the problem really? I could not make it work is not a problem per se and it is very indefinite.
– Sorack
So I’m having trouble getting the typed letter checked with the word I have to guess. Type if it is a Textfield, take the letter I typed hit the check button, if it is right put the letter in the correct position, if not error+1.
– Gabriel Oliveira
In this case there is not much to help, your doubt is very wide and we do not have the complete code to assist
– Sorack