我一直在研究Deitel的书(Java如何编程),我想解决练习6.35。以下是它的要求:
编写一个程序来帮助学生学习乘法。使用Random对象产生两个正整数(每个一个数字)。 该程序应在屏幕上显示如下内容:(" 7次3&#34多少钱;)
然后学生应该插入答案,如果答案正确或错误,程序会控制。如果程序正确,程序会继续询问另一个问题,否则程序会一直等到学生的答案是正确的。问题是它必须是一个新方法创建的(当应用程序启动时以及当用户回答问题时,应该调用此方法一次)。
我该怎么做?
// do-while块里面有问题!
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();
}
}
答案 0 :(得分:1)
制作一本关于如何做的简洁故事书。
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);
...
}
}