samedi 5 mai 2018

Someone have a look at my source code for a number guessing game that seems to keep failing

Hello please may someone run my code and assist me in debugging it. I'm having a lot of troubles trying to figure it out and i have not a lot of guidance when it comes to coding.

the problem with my code right now is that it runs certain parts twice. please annotate the issue and any reccomendations to fix it. Thanks in advance

a brief of what i'm trying to do: number guessing game the idea is that the computer will generate a random number and will ask the user if they know the number if the user gets the answer correct they will get a congrats message and then the game will end but if the user enters a wrong number they get a try again message and then they will try again

import javax.swing.*;

import java.lang.*;

public class Main {

public static void main(String[] args) {
   /*
   number guessing game
   the idea is that the computer will generate a random number and will ask the user if they know the number
   if the user gets the answer correct they will get a congrats message and then the game will end but if the user
   enters a wrong number they get a try again message and then they will try again

   the problem with my code right now is that it runs certain parts twice. please annotate the issue and any
   recomendations to fix it. Thanks in advance
    */

    enterScreen();
    if (enterScreen() == 0){
        number();
        userIn();
        answer();
    }


}
public static int enterScreen (){
   String[] options = {"Ofcourse", "Not today"};
    int front = JOptionPane.showOptionDialog(null,
            "I'm thinking of a number between 0 and 100, can you guess what is is?",
            "Welcome",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            null, options, "Yes" );
    if(front == 0){
        JOptionPane.showMessageDialog(null, "Goodluck then, you might need it. :D");
    }
    else {
        JOptionPane.showMessageDialog(null, "okay i dont mind");
    }

    return front;
}
private static int number(){

    double numD;
    numD = (Math.random() * Math.random()) * 100;
    int numI = (int) numD;
    System.out.println(numD);

    return numI;
}
private static int userIn(){
    String userStr = JOptionPane.showInputDialog("What number do you think im thinking of?");
    int user = Integer.parseInt(userStr);

    return 0;
}
private static void answer(){
    // here is the problem
    if(userIn() == number()){
        JOptionPane.showMessageDialog(null, "Well done! You must be a genius.");
    }
    else {
        JOptionPane.showMessageDialog(null, "Shame, TRY AGAIN!");
        userIn();
    }
}

}

Aucun commentaire:

Enregistrer un commentaire