Java 如何编程 Deitel 书籍练习 6.35

标签 java

我一直在学习 Deitel 的书(Java 如何编程),我想解决练习 6.35。这是它的要求:

编写一个程序来帮助学生学习乘法。使用 Random 对象生成两个正整数(每个数字一位)。 该程序应在屏幕上显示如下内容:(“7 乘以 3 等于多少”)

然后学生应该插入答案,程序控制答案是否正确。如果正确,程序继续问另一个问题,否则程序会等待,直到学生答案正确。对于每个新问题,它必须是创建一个新方法(该方法应在应用程序启动时以及用户回答正确问题时调用一次)。

我该怎么做?

//我在 do-while block 内遇到问题!

      package multiplication;
import java.util.Random;
import java.util.Scanner;

/*Hey again! I've been trying to solve this problem using NetBeans environment
 *
 */


public class Ypologismos
{
    private int p;
    private int a,b;

    public Ypologismos(int a,int b,int p)
    {
        this.a=a;
        this.b=b;
        this.p=p;

    }


 public Ypologismos()
 {


 }






 public void Screen()
 {
     System.out.println("Wrong answer ....please retry");
 }






    public void askForNumbers()
    {
        Random r=new Random();

        int a,b;
        a=r.nextInt(10);
        b=r.nextInt(10);
        int p;//p=product
        p=(a*b);
        System.out.println("How much is:"+" "+a+" "+"times"+" "+b+" "+"?");

        System.out.println("Please insert your answer!");

       Scanner s=new Scanner(System.in);
       int ans;//ans=answer
       ans=s.nextInt();

       do
       {
           while(ans==p){

               System.out.println("Well done!");
               askForNumbers();
           }

       }while(ans!=p);


    }






}

//和我的主课...

package multiplication;


public class Main
{


    public static void main(String[] args)
    {

        Ypologismos application=new Ypologismos();

        application.askForNumbers();


    }

}

最佳答案

制作一本关于如何做到这一点的简短故事书。

teach multiplication:
    repeat // solving problems
       int first number = something random
       int second number = something random
       int product = first number * second number
       repeat
           int answer = ask how much is first number times second number
           if answer != product
               say error!
       until answer == product
       say solved!

以上只是初步想法,不一定符合要求。但它会清除哪个循环进入哪个循环等等。

<小时/>

阅读您的扩展问题

public class Ypologismos {

    /** Entry point to the application. */
    public static void main(String[] args) {
        Ypologismos application = new Ypologismos();
        application.teachMultiplication();
    }

    private void teachMultiplication() {
        while (wantsAProblem()) {
             askAProblem();
        }
    }

    private void askAProblem() {
        int αλφα = random.nextInt(10);
        int βητα = random.nextInt(10);
        ...
    }
}

关于Java 如何编程 Deitel 书籍练习 6.35,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24452058/

相关文章:

java - 使用引用初始化对象而不访问非默认构造函数

java - 作为服务运行时将JVM参数传递给Tomcat?

java - 使用反射调用方法时出现 NoSuchMethodException

java - SpringBoot : How to make profile specific resources

java - 使用 ObjectOutputStream 和 ObjectInputStream 打开/保存 JTable 中的数据

java - ArrayList.add 有效,但 ArrayList.remove 无效

java - 一对一连接表级联删除

java - 何时创建辅助方法和单独的文件

java - JAXWS/JAXB 检查对象是否为空(不是字符串)

java - Flex 与数据库的连接