Devilhorn
Devilhorn

Reputation: 102

Java How to programm Deitel Book exercise 6.35

I've been studying Deitel's Book(Java how to program) and I want to solve exercise 6.35. Here's what it asks:

Write a program to assist a student to learn multiplication.Use a Random object to produce two positive integers (one digit each). The program should show on screen something like this:("How much is 7 times 3")

Then the student should insert the answer and the program controls if the answer is correct or wrong.If it's correct the program continue asking another question,else the program waits until the student answer is correct.For every new question it must be a new method created(this method should be called once when the application starts and when the user answers correct to a question).

How do I do this?

//I have a problem inside the do-while block!

      package multiplication;
import java.util.Random;
import java.util.Scanner;

/*Hey again! I've been trying to solve this problem using NetBeans environment
 *
 */


public class Ypologismos
{
    private int p;
    private int a,b;

    public Ypologismos(int a,int b,int p)
    {
        this.a=a;
        this.b=b;
        this.p=p;

    }


 public Ypologismos()
 {


 }






 public void Screen()
 {
     System.out.println("Wrong answer ....please retry");
 }






    public void askForNumbers()
    {
        Random r=new Random();

        int a,b;
        a=r.nextInt(10);
        b=r.nextInt(10);
        int p;//p=product
        p=(a*b);
        System.out.println("How much is:"+" "+a+" "+"times"+" "+b+" "+"?");

        System.out.println("Please insert your answer!");

       Scanner s=new Scanner(System.in);
       int ans;//ans=answer
       ans=s.nextInt();

       do
       {
           while(ans==p){

               System.out.println("Well done!");
               askForNumbers();
           }

       }while(ans!=p);


    }






}

//and my main class ...

package multiplication;


public class Main
{


    public static void main(String[] args)
    {

        Ypologismos application=new Ypologismos();

        application.askForNumbers();


    }

}

Upvotes: 0

Views: 862

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109567

Make a terse story book of how to do it.

teach multiplication:
    repeat // solving problems
       int first number = something random
       int second number = something random
       int product = first number * second number
       repeat
           int answer = ask how much is first number times second number
           if answer != product
               say error!
       until answer == product
       say solved!

The above is just a first idea, not necessarily following the requirements. But it clears which loop comes in which loop and so on.


Reading your extended question

public class Ypologismos {

    /** Entry point to the application. */
    public static void main(String[] args) {
        Ypologismos application = new Ypologismos();
        application.teachMultiplication();
    }

    private void teachMultiplication() {
        while (wantsAProblem()) {
             askAProblem();
        }
    }

    private void askAProblem() {
        int αλφα = random.nextInt(10);
        int βητα = random.nextInt(10);
        ...
    }
}

Upvotes: 1

Related Questions