Need major help fast

Discussion in 'Software' started by kenny770, Mar 30, 2005.

  1. kenny770

    kenny770 Private E-2

    hey guys im new to this programming so im wondering if you all can help me out here is the program description:

    Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Produce two positive one-digit random integers. The program then displays a prompting question in an input dialog box, such as
    How much is 6 times 7?

    The student then types the answer. Next, the program checks the student’s answer.
    • If it is correct, a message dialog box will be displayed showing “Very good!”.
    • If the answer is wrong, a message dialog box will be displayed showing “No. Please try again.”. The program generates more input dialog boxes and let the student try the same questions repeatedly until the student finally gets it right.
    Write a method for this. The method header could be something similar to this: public static void checkResult (int studentAnswer, int correctAnswer)


    Here is what i have come up with thus far but i need major help please if anyone can help please I will be very thankful:

    import javax.swing.JOptionPane;

    public class Random{

    public static void main (String[] args) {

    int num1 = (int)(Math.random()*11);
    int num2 = (int)(Math.random()*11);
    int sum = 0;
    int userInput = 0;
    int k = checkResult(userInput, sum);
    int s = 0;
    int i = 0;
    int l = 0;

    //Multiplication of numbers

    sum = num1 * num2;

    //User Input and convert to integer

    String userInputString = JOptionPane.showInputDialog(null,"How much is " + num1 + "*" + num2 + "?","Project 3", JOptionPane.QUESTION_MESSAGE);

    userInput = Integer.parseInt(userInputString);

    if(s == i)
    System.out.println("The answer" + userInput + "is corerct");
    else
    if(s == l)
    System.out.println("The answer" + userInput + "is wrong");

    }
    //Mehtod for checking answer
    public static int checkResult(int studentAnswer, int correctAnswer){

    int result = 0;
    int i = 0;
    int l = 0;
    if (studentAnswer == correctAnswer)
    result = i;

    else
    result = l;

    return result;
    }

    }


    Please help asap thanks
     
  2. goldfish

    goldfish Lt. Sushi.DC

    (int)(Math.random()*11);
    Whats that all about?
    Shouldn't it be
    Code:
    intValue(Math.random()*11); 
    Your if statements are wrong
    Syntax is
    Code:
    if (condition==value) { do.This() }
    
    Also, that seems rather overly complex considering all its got to do is multiply a couple of numbers togeher and see if what is typed in is correct or not. It can be done in about half the space.
     
  3. goldfish

    goldfish Lt. Sushi.DC

    OK, answering your question in the other thread, you need to have some sort of recursion there. I.e. if its wrong, run the function again until its right. In english

    Ask for an answer function {

    if (check answer) {
    say "good job";
    } else {
    ask for an answer function
    }

    }
     
  4. QuickSilver

    QuickSilver Corporal

    I would suggest that rather than recursively calling the function a loop is used... Recursion can be useful but I wonder if it's the right tool for the job especially when learning to program when habits are formed. I think a loop would be slightly better... More specifically I would use a do-while loop to repeatedly check the answer until its correct...

    A do while loop will execute the code in the block always at least once. After it has executed the code once it then performs an evaluation to determine whether to loop back round again... In this case the body of the loop would be to get the users answer, and the evaluation to be whether it is equal to the correct answer or not...

    so :

    Code:
    calculate correct answer
    do
      get the user input
    while (user input is not equal to correct answer)
    
    Just my thoughts on the recursive aspect...
     
  5. kenny770

    kenny770 Private E-2

    ok guys now i have it almost but the program is not stopping after the user enters the correct answer on the second guess. what do i need to do or change to make this loop terminate.

    import javax.swing.JOptionPane;

    public class Random{

    /**Main Method*/
    public static void main (String[] args) {

    //Declare and intialize variables
    int num1 = (int)(Math.random()*11);
    int num2 = (int)(Math.random()*11);
    int sum = 0;
    sum = num1 * num2;
    int userGuess;

    //Get user input
    String guessString = JOptionPane.showInputDialog(null,"What is " + num1 + "*" + num2 + "?","Project 3",JOptionPane.QUESTION_MESSAGE);

    //Convert user input into a int
    userGuess = Integer.parseInt(guessString);

    //Invoke the checkResult method to check students answer against computers answer
    checkResult(userGuess, sum);

    System.exit(0);
    }

    //Check students answer with checkResult method

    public static void checkResult(int studentAnswer, int correctAnswer){

    int userGuess2;

    while(studentAnswer == correctAnswer){

    JOptionPane.showMessageDialog(null,"Very Good!","Project 3",JOptionPane.INFORMATION_MESSAGE);

    if (studentAnswer == correctAnswer) break;
    }

    while(studentAnswer != correctAnswer){

    String userGuess2String = JOptionPane.showInputDialog(null,"That was incorrect. Try Again Please:","Project 3",JOptionPane.QUESTION_MESSAGE);

    userGuess2 = Integer.parseInt(userGuess2String);

    if (userGuess2 == correctAnswer)
    JOptionPane.showMessageDialog(null,"Very Good!","Project 3",JOptionPane.INFORMATION_MESSAGE);

    if (studentAnswer == correctAnswer) break;
    }


    }
    }
     
  6. QuickSilver

    QuickSilver Corporal

    Hmmm... Im not sure what your method is doing here...

    You have 2 while loops in there :

    Code:
    public static void checkResult(int studentAnswer, int correctAnswer)
    {
      int userGuess2;
    
      while(studentAnswer == correctAnswer)
      {
    
        JOptionPane.showMessageDialog(null,"Very Good!","Project  3",JOptionPane.INFORMATION_MESSAGE);
    
        if (studentAnswer == correctAnswer) 
          break;
      }
    
      while(studentAnswer != correctAnswer)
      {
        String userGuess2String = JOptionPane.showInputDialog(null,"That was   incorrect. Try Again Please:","Project 3",JOptionPane.QUESTION_MESSAGE);
    
        userGuess2 = Integer.parseInt(userGuess2String);
    
        if (userGuess2 == correctAnswer)
          JOptionPane.showMessageDialog(null,"Very Good!","Project 3",JOptionPane.INFORMATION_MESSAGE);
    
        if (studentAnswer == correctAnswer) 
          break;
      }
    }
    
    The first loop is essentially saying the following in English :
    WHILE the student answer is the same as the correct answer
    tell the user well done
    If the student answer is the same as the correct answer
    break out of the loop

    So... your loop is only going to loop as long as the correct answer is equal to what the user has entered... if the user enters incorrectly this code won't get executed... I'm guessing you later added the if statement and break because you found your program looping infinitely saying well done if you entered the correct answer first time... :) Do you see why that happened though? If not I'll be happy to explain it...

    Your next loop is essentially saying :
    WHILE the student answer is NOT the same as the correct answer
    ask for the answer again and store it in a variable
    if they have got it right then print a message
    if student answer is the same as correct answer
    break out the loop....

    This is closer... you are going to keep going round this UNTIL someone enters the correct answer... Again though you have this if condition in there that will break out the loop - look at the code - the variables in that if statement will never change each time the loop goes round - they are the variables passed into the method and they do not change within that method...
    You have also introduced a new variable to hold subsequent guesses... Is this necessary? Do you think you could maybe use the studentAnswer variable to store subsequent attempts in? Remeber once the user enters the correct answer the loops condition is no longer true - it automatically breaks out...

    Ok so this is part of what you are being asked to do... when learning how to write programs it is useful to break the program down into small steps and then address them one by one... The above quote is a good place to start

    So what do you want your method to do?

    Lets look at building what it does incrementally...

    1) Write the method so that it simply checks the studentAnswer against the correctAnswer. If correct then display a well done message box... if wrong then display a message saying wrong...

    2) Now try replacing this functionality with a loop that asks the user what the correct answer is each time they get it wrong... You only need one loop to do this.
    A while loop breaks automatically when the conditional statement in it is true. So a while loop looks liek this :

    while (some condition)
    {
    ... do some stuff ...
    }

    In the case of your loop the condition is clear... once the user has entered the correct answer your work is done... break. If you enter this then you have no need for the break keyword.
    The condition will get checked everytime the loop goes round...

    You are getting there - try looking at what I have suggested and thinking about the program again... good luck and let us know how you get on...
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds