使用随机整数询问乘法表

时间:2013-11-15 08:45:39

标签: 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 + "?";
    }
}

4 个答案:

答案 0 :(得分:1)

向您的班级添加主要方法

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();
    }
}

答案 1 :(得分:0)

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


public class MultiplyLearn {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MultiplyLearn driver = new MultiplyLearn();
        //driver.askQuestion(2, 4);
        driver.Learn();

    }

    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 ){

        // ISSUE: The returned value needs to be printed out. The program was waiting for input and hence it did not proceed from there(No O/P). I have corrected it.
        System.out.println(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 + "?";
    }

}

答案 2 :(得分:0)

我认为这就是你所需要的:

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){
            System.out.println("How much is " + num1 + " times " + 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 static void main(String[] args)
    {
        MultiplyLearn learner = new MultiplyLearn();

        learner.Learn();
    }

几点:

  • 命名约定建议方法应以小写字母开头
  • 我没有看到单独提出问题的单独方法的重点

答案 3 :(得分:0)

首先使用命名约定:http://java.about.com/od/javasyntax/a/nameconventions.htm 因此将Learn()函数命名为learn()。 然后你必须从静态函数启动项目。 因此,如果你想从learn()函数启动项目,你需要使它成为静态(但在这种情况下你需要使其他函数也是静态的),或者如果你只想使用该函数,你需要编写一个静态主函数(在这种情况下优先)。

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

public class MultiplyLearn{

    public static void main(String args[]){
        MultiplyLearn multiplyLearn = new MultiplyLearn();
        multiplyLearn.learn();
    }

    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 + "?";
    }
}