以下程序运行完之后想再继续做题需要再运行。如何做到重复出题,按某一健才不出题那

yimaoyingbi 2011-07-10 05:23:51
import java.util.Random;
import java.util.Scanner;
public class JiaFa
{
int a;
int b;
int sum;
public int JiaF()

{
Random random = new Random();
this.a = random.nextInt(10);
this.b = random.nextInt(10);
System.out.println(a + "+" + b+ "=");
return sum = a + b;

}
public void showTitle()
{
System.out.println("请计算此题目:");
}

public static void main(String[] args)
{
JiaFa he = new JiaFa();

he.showTitle();
he.JiaF();
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();

if( he.sum == c)
{
System.out.println("正确。");
}
else
{
System.out.println("错误,正确答案为: " + he.sum );
}

}
}
...全文
94 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞跃颠峰 2011-07-10
  • 打赏
  • 举报
回复
输入q后按回车退出

public static void main(String[] args) {
JiaFa he = new JiaFa();
Scanner sc = new Scanner(System.in);

while (true) {
he.showTitle();
he.JiaF();
int c;
try {
c = sc.nextInt();
if (he.sum == c) {
System.out.println("正确。");
} else {
System.out.println("错误,正确答案为: " + he.sum);
}
} catch (InputMismatchException e) {
String s = sc.nextLine();
if (s.equals("q"))
break;
}
}
}

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧