Displaying problem help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Displaying problem help

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Unhappy Displaying problem help

    import java.util.Random;
    import java.util.*;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    import java.lang.Math;

    public class MathIsSuperFun1{

    Scanner input = new Scanner(System.in);
    int correctAnswers;
    int randomNum1;
    int randomNum2;
    int choice;
    int corrrectAnswers, wrongAnswers;
    String playerName ="";
    int operation;
    int userAnswer;
    int correctAnswer = 0;
    int userRemainder, correctRemainder;
    int x = 0;
    int temporaryNum1, temporaryNum2;
    int range;
    int randomNumber;


    public static void main (String args[]){
    MathIsSuperFun1 lab = new MathIsSuperFun1();
    lab.init();
    }

    public void init(){

    getName();
    pickDifficulty();
    pickOperation();
    for(int x = 0; x < 10; x++)
    {

    System.out.println("\t\t\t~~~~~~~PROBLEM NUMBER" + (x + 1) + "~~~~~~~~");
    assignNum();
    getProblem();
    checkAnswer();
    }
    }

    //GET PLAYER NAME USING PANE
    public static String getName(){
    String playerName;


    playerName = JOptionPane.showInputDialog(null, "Welcome!\nEnter your name and press OK.", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
    System.out.println("Do your best," + playerName + "!");
    return playerName;
    }


    //PICK DIFFICULTY USING DIALOG
    public void pickDifficulty(){
    int choice = 0;
    String choiceBox;

    choiceBox = JOptionPane.showInputDialog(null, "1 - Child's Play\n2 - No Sweat\n3 - Bitter\n4 - Cold-blooded\n5 - Brutal\n6 - Genius", "Math Is Super Fun!", JOptionPane.QUESTION_MESSAGE);
    choice = Integer.parseInt(choiceBox);
    System.out.println("Difficulty:" + choiceBox);
    }

    //PICK OPERATIONS
    public void pickOperation(){
    int operation = 0;

    String operationBox = JOptionPane.showInputDialog(null,"1 - Addition\n2 - Subtraction\n3 - Multiplication\n4 - Division ", "Math Is Super Fun", JOptionPane.QUESTION_MESSAGE);
    operation = Integer.parseInt(operationBox);

    }

    //GET NUMBER RANGE BASED ON DIFFICULTY
    public int numberRange(){
    int range = 0;

    switch(choice){

    case 1:
    range = 100;
    break;

    case 2:
    range = 1000;
    break;

    case 3:
    range = 10000;
    break;

    case 4:
    range = 100000;
    break;

    case 5:
    range = 1000000;
    break;

    case 6:
    range = 10000000;
    break;
    }
    return range;
    }

    //GET CORRECT RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
    public void correctResponse(){
    String responseCorrect = "";

    switch (getRandom(5)){

    case 1:
    responseCorrect = "Correct. Keep up the good work!";
    break;

    case 2:
    responseCorrect = "Correct. Keep aiming higher!";
    break;

    case 3:
    responseCorrect = "Correct. Well done!";
    break;

    case 4:
    responseCorrect = "Correct. Nice work!";
    break;

    case 5:
    responseCorrect = "Correct. We're almost there!";
    break;
    }

    System.out.println(responseCorrect);
    correctAnswers += 1;
    }

    //GET WRONG RANDOM RESPONSE USING CASE SWITCH BASED ON GETRANDOM METHOD
    public String wrongResponse(){
    String responseWrong = "";
    switch (getRandom(5)){

    case 1:
    responseWrong = "Wrong. Don't give up!";
    break;

    case 2:
    responseWrong = "Wrong. You can do it!";
    break;

    case 3:
    responseWrong = "Wrong. Try again puny human!";
    break;

    case 4:
    responseWrong = "Wrong. You must be really weak at math!";
    break;

    case 5:
    responseWrong = "Wrong. I pity you!";
    break;
    }

    System.out.println(responseWrong);
    System.out.println("The correct answer is:" + correctAnswer);
    if(operation == 4)
    System.out.println("Correct Remainder: " + correctRemainder);
    return responseWrong;
    }

    //GET PROBLEM BASED ON OPERATION
    public void getProblem(){
    switch(operation){

    case 1:
    System.out.println(randomNum1 + "+" + randomNum2 + "= ?\n");
    correctAnswer = randomNum1 + randomNum2;
    break;

    case 2:
    System.out.println(randomNum1 + "-" + randomNum2 + "= ?\n");
    correctAnswer = randomNum1-randomNum2;
    break;
    case 3:
    System.out.println(randomNum1 + "*" + randomNum2 + "= ?\n");
    correctAnswer = randomNum1*randomNum2;
    break;
    case 4:
    System.out.println(randomNum1 + "/" + randomNum2 + "= ?\n");
    correctAnswer = randomNum1/randomNum2;
    correctRemainder = randomNum1%randomNum2;
    break;
    }

    System.out.print("Answer: ");
    userAnswer = input.nextInt();
    if(operation == 4){
    System.out.print("Remainder: ");
    userRemainder = input.nextInt();
    }

    }


    public void checkAnswer(){
    if(operation != 4 && userAnswer == correctAnswer){
    correctResponse();
    }
    else if(operation == 4 && userAnswer == correctAnswer && userRemainder == correctRemainder){
    correctResponse();
    }
    else{
    wrongResponse();
    }
    }

    public void assignNum()
    {
    int temporaryNum1 = getRandom(numberRange());
    int temporaryNum2 = getRandom(numberRange());
    while(operation == 4 && temporaryNum1 == 0){
    temporaryNum1 = getRandom(numberRange());
    }
    while(operation == 4 && temporaryNum2 == 0){
    temporaryNum2 = getRandom(numberRange());
    }
    if(temporaryNum1 > temporaryNum2)
    {
    randomNum1 = temporaryNum1;
    randomNum2 = temporaryNum2;
    }
    else
    {
    randomNum1 = temporaryNum2;
    randomNum2 = temporaryNum1;
    }
    }



    public int getRandom(int range){
    randomNumber = (int)Math.floor((Math.random()*range)+1);
    return randomNumber;
    }



    }



    At the getProblem(); method the problem does not display, what is the error? Please help me.
    Here is a sample output

    ~~~~~~~~PROBLEM NUMBER1~~~~~~~
    Answer: 0
    Correct....
    ~~~~~~PRBLEM NUMBER2~~~~~~~
    ........

    The problem does not display.

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: Displaying problem help

    When posting code use [code] Post code inside here [/code] tags.

    The issue I notice is that you are creating variables at the class level then you are creating them again within methods. This means that the one at the Method level will get discarded when the method ends so none of the class variables will have any of the information saved to them. I may be wrong but I am not sure why you would want to create class level variables within the main class. I think class level variables are used more in Objects where you have accessors and mutators to assign or get values from private variables.

    Code:
    import java.util.Scanner;
    
    public class ExampleOfVariableLevels
    {
      // This is a class level variable
      String usrName = "";
      
      // Create scanner object.
      Scanner usrInput = new Scanner(System.in);
    
      public static void main(String[] args)
      {
        ExampleOfVariableLevels ex1 = new ExampleOfVariableLevels();
        ex1.init();
      }
      
      public void init()
      {
        example1(); // No value will be stored into usrName since any value is discarded after method ends and not saved into variable
        System.out.println("This is value of variable: " + usrName);
        
        usrName = example1(); // Value is returned and saved into usrName at class level.
        System.out.println("This is value of variable: " + usrName);
        
        example2(); // Since method did not create same name variable it just saved the data into the variable
        System.out.println("This is value of variable now: " + usrName);
      }
      
      public String example1()
      {
        String usrName = "";
        
        System.out.print("Please enter your name: ");
        usrName = usrInput.nextLine();
        
        return usrName;
      }
      
      public String example2()
      {
        usrName = "";
        
        System.out.print("Please enter your name: ");
        usrName = usrInput.nextLine();
        
        return usrName;
      }
    }
    I hope the example clarifies things a little bit. I am sure someone with more experience can explain why not to do certain things.

  3. #3
    Join Date
    Feb 2013
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center