java - 使用随机整数询问乘法表

标签 java

我编写了以下代码,询问“num1 乘以 num2 是多少?”这个问题。但是,当我尝试运行 java 文件时,我没有得到任何响应。你能帮助我理解我做错了什么吗?代码如下:

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

public class MultiplyLearn{

    public void Learn(){

        Random multiple = new Random();
        Scanner input = new Scanner( System.in );
        boolean wrong = true;

        int num1 = 1 + multiple.nextInt( 9 );
        int num2 = 1 + multiple.nextInt( 9 );

        while( wrong == true ){

        askQuestion( num1, num2 );
        int answer = input.nextInt();

        if( answer == num1*num2 ){
            System.out.println( "Very Good" );
            wrong = false;
        }

        else{
            System.out.print( "No. Please try again." );
        }
        }
    }

    public String askQuestion( int x, int y ){

        return "How much is" + x + "times" + y + "?";
    }
}

最佳答案

向类中添加一个 main 方法

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

public class MultiplyLearn{

   //your actual code goes here

   public static void main(String args[]) throws Exception{
       new MultiplyLearn().Learn();
   }
}

所以你的最后一个类将如下所示

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

public class MultiplyLearn{

    public void Learn(){

        Random multiple = new Random();
        Scanner input = new Scanner( System.in );
        boolean wrong = true;

        int num1 = 1 + multiple.nextInt( 9 );
        int num2 = 1 + multiple.nextInt( 9 );

        while( wrong == true ){

        askQuestion( num1, num2 );
        int answer = input.nextInt();

        if( answer == num1*num2 ){
            System.out.println( "Very Good" );
            wrong = false;
        }

        else{
            System.out.print( "No. Please try again." );
        }
        }
    }

    public String askQuestion( int x, int y ){

        return "How much is" + x + "times" + y + "?";
    }

    public static void main(String args[]) throws Exception{
       new MultiplyLearn().Learn();
    }
}

关于java - 使用随机整数询问乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19996883/

相关文章:

javascript - 从 React Native(android) 中的 .jar 文件访问方法

java - Runnable 和 ExecutorService 的奇怪行为

java - 如何使用 Apache POI 读取 Excel 文件中特定行的数据?

java - 未找到请求的操作的编解码器 : [map<varchar, int> <-> java.util.Map];问题 Apache Cassandra

java - Solr 。如何连接两个单独的查询

java - 更改 Swagger 生成的类名称

java - LWJGL 鼠标滚轮 getDWheel() 方法

java - "No Persistence provider for EntityManager"错误

java - 为交易系统设计订单对象

java - 循环计算 - Java