Fórum [RESOLVIDO]Dúvida - Famoso exercicio de adivinhação #568467
17/02/2012
0
package JogoAdivinha;
import javax.swing.JOptionPane;
public class Jogo {
public static void main(String[] args) {
String nome ;
int numTentativas ;
int randon = (int)(1+Math.random()*100);
int[] jogadas = new int[20];
nome = JOptionPane.showInputDialog(null,"Olá,qual o seu nome?");
JOptionPane.showMessageDialog(null,"Agora estou pensando em um número...Pronto!Tente adivinhar!");
numTentativas = Integer.parseInt(JOptionPane.showInputDialog(null,"Qual número você acha que é?"));
for(int i = 0; i < 21; i++)
while(numTentativas != randon && numTentativas < 21)
{
numTentativas = Integer.parseInt(JOptionPane.showInputDialog(null,"Tente de novo "+nome+ "."));
if (numTentativas < randon)
JOptionPane.showMessageDialog(null,"Tente um numero maior " +nome+".");
else if (numTentativas > randon)
JOptionPane.showMessageDialog(null,"Tente um numero menor " +nome+".");
}
if (numTentativas == randon)
JOptionPane.showMessageDialog(null,"PARABÉNS "+nome+" !!");
JOptionPane.showMessageDialog(null,"Você acertou na " +jogadas);
else (numTentativas < 21)
JOptionPane.showMessageDialog(null,"Fim de jogo,acabaram as tentativas.");
}
}Kleber Bogo
Curtir tópico
+ 0Posts
17/02/2012
Mathews Motta
Gostei + 0
17/02/2012
Kleber Bogo
package JogoAdivinha;
import javax.swing.JOptionPane;
public class Jogo {
public static void main(String[] args) {
String nome ;
int numTentativas ;
int randon = (int)(1+Math.random()*100);
int[] jogadas = new int[20];
nome = JOptionPane.showInputDialog(null,"Olá,qual o seu nome?");
JOptionPane.showMessageDialog(null,"Agora estou pensando em um número...Pronto!Tente adivinhar!");
numTentativas = Integer.parseInt(JOptionPane.showInputDialog(null,"Qual número você acha que é?"));
for(int i = 0; i < 21; i++)
while(numTentativas != randon && numTentativas < 21)
{
numTentativas = Integer.parseInt(JOptionPane.showInputDialog(null,"Tente de novo "+nome+ "."));
if (numTentativas < randon)
JOptionPane.showMessageDialog(null,"Tente um numero maior " +nome+".");
else if (numTentativas > randon)
JOptionPane.showMessageDialog(null,"Tente um numero menor " +nome+".");
}
if (numTentativas == randon)
JOptionPane.showMessageDialog(null,"PARABÉNS "+nome+" !!");
JOptionPane.showMessageDialog(null,"Você acertou na " +jogadas);
else (numTentativas < 21)
JOptionPane.showMessageDialog(null,"Fim de jogo,acabaram as tentativas.");
}
}int num = <código de obter número>
if(num > 10){
<Ação caso seja maior que 10>
} else if(num < 10){
<Ação caso seja menor que 10>
} else {
<Ação caso seja IGUAL a 10> //Já que ele não é NEM maior e NEM menor, então é igual.
}Gostei + 0
17/02/2012
Kleber Bogo
int numTentativas = 0; //Começa com 0 tentativas.
int chute = <RECEBE VALOR> //RECEBE O VALOR DO CHUTE AQUI E NÃO NO NÚMERO DE TENTATIVAS
while(chute != randon && numTentativas < 21) {
//...
numTentativas++; //Isso aqui é igual a numTentativas = numTentativas + 1;
}Gostei + 0
17/02/2012
Mathews Motta
Gostei + 0
17/02/2012
Kleber Bogo
Gostei + 0