0

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

4 回答 4

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();
    }
}
于 2013-11-15T08:48:41.317 回答
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 + "?";
    }
}
于 2013-11-15T08:58:29.480 回答
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 + "?";
    }

}
于 2013-11-15T08:57:25.163 回答
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();
    }

几点:

  • 命名约定建议方法应以小写开头
  • 我看不出有一个单独的方法来问这个问题有什么意义
于 2013-11-15T08:58:06.093 回答