我编写了以下代码,询问了“ 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 + "?";
    }
}

最佳答案

向您的班级添加主要方法

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

10-04 17:43