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

クラスに 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();
    }
}
于 2013-11-15T08:48:41.317 に答える
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 に答える