Hello,
i want to write a program that will help an elementary school student to lean multiplication. For this, i use random object to produce two new positive one-digit integers. For this, i have coded this, but then got confused of the scope issue. Here is the code so far:

import java.util.Random; 
import java.util.Scanner; 

public class CarpimOgretme {
    Random myRandom = new Random(); 
    Scanner input = new Scanner(System.in); 

    public void determine(){ 

    do{
    int num1 = 1 + myRandom.nextInt(11);
    int num2 = 1 + myRandom.nextInt();
    int answer = num1 * num2; 
    int cevap = input.nextInt();
    }while (answer != cevap); 


    }//end method

}//end class

Here, in the while block, it cannot reach answer and cevap, which means 'answer' in my language for your information. How can i reach ? Please help !!

Recommended Answers

All 6 Replies

I mean how can i repeat this code ?

import java.util.Random; 
import java.util.Scanner; 

public class CarpimOgretme {
    Random myRandom = new Random(); 
    Scanner input = new Scanner(System.in); 

    public void determine(){ 
    int num1 = 1 + myRandom.nextInt(11);
    int num2 = 1 + myRandom.nextInt(11);
    System.out.println("num1 = " + num1 + "num2 = " + num2 );
    int answer = num1 * num2; 
    int cevap = input.nextInt();

    while ( answer != cevap ){ 
        determine(); 
    }



    }//end method

}//end class

I have just added this to my code, but i still could not figure out how to ask the same question till answer is correct ?

Try listing the steps in the order the program needs to do them to solve your problem. When you get the logic worked out, then try writing the code.

import java.util.Random; 
import java.util.Scanner; 

public class CarpimOgretme {
    Random myRandom = new Random(); 
    Scanner input = new Scanner(System.in); 
    public void determine(){ 
    for ( int i = 0; i < 20; i++ ){


    int num1 = 1 + myRandom.nextInt(11);
    int num2 = 1 + myRandom.nextInt(11);
    System.out.println("num1 = " + num1 + " num2 = " + num2 );
    int answer = num1 * num2; 
    int cevap = input.nextInt();
        while ( answer != cevap ){
            System.out.println("Wrong... Try Again."); 
            cevap = input.nextInt();
        }//end while 

        }//end for statement
    }//end method

}//end class

OK. I did it myself. Even though you did not answer, thank you all.

That's the idea. Make you think through the problem, not give you the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.