簡體   English   中英

如何創建一個會產生隨機數學問題的Java函數。

[英]how to create a java function that makes random math problems.

我正在嘗試創建一個Java函數,該函數會產生隨機數學問題。 我想讓此函數創建問題,然后onclick顯示答案。

是否有必要使用Random int

我在想這樣的事情,使i成為一個隨機數,然后在數學問題中使用它...任何幫助,我們感激不盡。

private int rando(int i) {
    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt();
    i = randomInt;
    return rando();
}

public static int add(int i, int i)
{
    int a = i+i;
    return a;
}

b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
        String b = "Click For Answer";
        b1.setText(b.toString());

        jl.setText(prob.toString());
    }
});

怎么樣

//or whatever numbers you want here
int myLow = -10;
int myHigh = 10; 

public int randBetween(int low, int high){
    return (int) Math.round(Math.random() * (high - low)) + low;
}

public String makeProblem(AtomicLong answer){
    int a = randBetween(myLow, myHigh);
    int b = randBetween(myLow, myHigh);
    double result;
    char operator;
    switch(randBetween(1,4)){
        case 1: 
            operator = '+';
            result = a + b;
            break;
        case 2: 
            operator = '-';
            result = a - b;
            break;
        case 3: 
            operator = '*';
            result = a * b;
            break;
        case 4: 
            operator = '/';
            result = (double) a / b;
            break;
        default:
            operator = '\0';
            result = Double.NaN;
    }
    answer.set(Double.doubleToLongBits(result));
    return String.format("%d %c %d = ?",a,operator,b);
} 

public void yourFunction(){

    //whatever you need to do before

    AtomicLong outVariable = new AtomicLong();
    String question = makeProblem(outVariable);
    double answer = Double.longBitsToDouble(outVariable.get());

    //whatever you need to do after

}

像(沒有測試過或任何東西。):

public class MathProblems {
     private static final int MAX_NUMBER = 10;
     private static final Scanner in = new Scanner(System.in);
     private static final Random random = new Random();

     public static void main (String... args) {
         new MathProblems().run();
     }

     public void run() {
         while(true) {  
             final int a = random.nextInt(MAX_NUMBER);
             final int b = random.nextInt(MAX_NUMBER);

             final int type = random.nextInt(4);

             switch (type) {
                case 0: 
                   add(a, b);
                break;
                case 1: 
                   subtract(a, b);
                break;
                case 2:
                   multiply(a, b);
                break;
             }
         }  
     }

     private void add(final int a, final int b) {
         final int expected = a + b;

         final int answer = askQuestion(a + " + " + b + "=");

         checkResult(expected, answer); 
     }

     private void subtract(final int a, final int b) {
         final int expected = a - b;

         final int answer = askQuestion(a + " - " + b + "=");

         checkResult(expected, answer); 
     }

     private void multiply(final int a, final int b) {
         //leaving this for you..
     }

     private int askQuestion(final String question) {
         System.out.print(question);

         return in.nextInt(); 
     }  

     private void checkResult(final int expected, final int answer) {
         if (expected == answer) {
            System.out.println("Correct answer! You rock!");
         } else {
            System.out.println("WROOONG! You suck!");
         }    
     }  
}

這個怎么樣...

public class MyClass{

    Random random;

    public MyClass(){
        random = new Random();
    }


    private String randomNumber(){
        return Integer.toString(random.nextInt());
    }

    private String randomOperator(){
        String[] operators = {"+", "/", "-", "*"};
        int i = random.nextInt(4);
        return operators[i];        
    }

    public void execute(){
        String sum = randomNumber() + randomOperator() + randomNumber();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM