| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 616 人关注过本帖
标题:[求助]答案输出错误
只看楼主 加入收藏
寂寞天涯人
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-3-18
收藏
 问题点数:0 回复次数:7 
[求助]答案输出错误

//产生随机的四则运算题目,输入答案,判断正误
import java.util.*;
import java.io.*;
class suanshi{ //产生随机数//
Random rand=new Random();
//int a=(int)Math.random()*50;
//int b=(int)Math.random()*20;
//int c=(int)Math.random()*10+1;
//rand.setSeed(long d);
int a=(int)rand.nextInt()/1000000;
int b=(int)rand.nextInt()/1000000;
int c=(int)(rand.nextFloat()*10+1);
int sum=0;
public suanshi(){}
public suanshi(int k){ //产生运算符号//
char d;
if(1<=c&&c<=3){
d='+';
sum=a+b;
}
else
if(4<=c&&c<=6){
d='-';
sum=a-d;
}
else
if(7<=c&&c<=8){
d='×';
sum=a*b;
}
else {
d='÷';
sum=a/b;
}
System.out.print("("+a+")"+"\t"+d+"\t"+"("+b+")"+"=");
//System.out.print(""a+d+b+"=");
}
}

class panduan extends suanshi {
int result;
panduan(){super (1);}
public int flag=0;
panduan(int i){
// if(i==sum) {
if(i==(a+b)) {
System.out.println("你答对了");
flag=1;
}
else {
System.out.println("你答错了"+"\n"+"不要放弃"+"\t");
flag=0;
}
}
int f0(){
flag=0;
return flag;
}
int f1(){
flag=1;
return flag;
}
}
//主类开始//
public class ceshi extends panduan{
public int i=0;
String s="";
ceshi(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
i=Integer.parseInt(s);
}catch(IOException e){}
panduan p=new panduan(i);
}
//int fc(){return i;}
public static void main(String [] args) {
int j=0;
int tt=0;
String ss="";
System.out.println("这是四则运算题!");
System.out.println("***************");
do{
ceshi t=new ceshi();
//int pp=t.fc();
// t.panduan(pp);
System.out.println("输入0继续.输入1推出");
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
ss=in.readLine();
j=Integer.parseInt(ss);
}catch(IOException e){}

switch(j){
case 0:
tt=t.f0();
System.out.println("请继续下一题");break;
case 1:
tt=t.f1();
System.out.println("谢谢使用,再见!");break;
}
}while(tt==0);
}
}
//为什么计算结果不对的 ?

搜索更多相关主题的帖子: 输出 
2006-03-24 23:43
寂寞天涯人
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-3-18
收藏
得分:0 
哪为知道,请帮忙看看

书籍,电影,音乐的互动交流平台http://www./
2006-03-24 23:48
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

import java.util.*;
import java.io.*;
class suanshi{ //产生随机数//
static Random rand=new Random();
//int a=(int)Math.random()*50;
//int b=(int)Math.random()*20;
//int c=(int)Math.random()*10+1;
//rand.setSeed(long d);
int a=(int)rand.nextInt()/1000000;
int b=(int)rand.nextInt()/1000000;
int c=(int)(rand.nextFloat()*10+1);
static int sum=0;
public suanshi(){}
public suanshi(int k){ //产生运算符号//
char d;
if(1<=c&&c<=3){
d='+';
sum=a+b;
}
else
if(4<=c&&c<=6){
d='-';
sum=a-b;//你的原程序中,这是d,我找了好久才找到原来是这里错了
}
else
if(7<=c&&c<=8){
d='×';
sum=a*b;
}
else {
d='÷';
sum=a/b;
}
System.out.print("("+a+")"+"\t"+d+"\t"+"("+b+")"+"=");
//System.out.print(""a+d+b+"=");
}
}

class panduan extends suanshi {
int result;
panduan(){super (1);}
public int flag=0;
panduan(int i){
// if(i==sum) {
if(i==sum) {
System.out.println("你答对了");
flag=1;
}
else {
System.out.println("你答错了"+"\n"+"不要放弃"+"\t");
flag=0;
}
}
int f0(){
flag=0;
return flag;
}
int f1(){
flag=1;
return flag;
}
}
//主类开始//
public class ceshi extends panduan{
public int i=0;
String s="";
ceshi(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
i=Integer.parseInt(s);
}catch(IOException e){}
panduan p=new panduan(i);
}
//int fc(){return i;}
public static void main(String [] args) {
int j=0;
int tt=0;
String ss="";
System.out.println("这是四则运算题!");
System.out.println("***************");
do{
ceshi t=new ceshi();
//int pp=t.fc();
// t.panduan(pp);
System.out.println("输入0继续.输入1推出");
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
ss=in.readLine();
j=Integer.parseInt(ss);
}catch(IOException e){}

switch(j){
case 0:
tt=t.f0();
System.out.println("请继续下一题");break;
case 1:
tt=t.f1();
System.out.println("谢谢使用,再见!");break;
}
}while(tt==0);
}
}

你的原程序中错误的地方,我都给你标出来了
你这个程序之所以会错,就是因为你没有完全搞懂继承的关系
当你第二次去构造一个对象的时候,这个时候它的超类也会重新去构造一个对象,那么这个时候,sum你原来写是的成员变量,所以当然每次你第二次去构造的时候,成员变量都会不同,但是如果你把它声明成一个类的变量的时候,就好了因为类的变量是所有类的成员所共享的。

但是你这个程序只能运算整型的数, 并且做除法的时候会有很大的误差
比如50/30,结果会是1;


可惜不是你,陪我到最后
2006-03-25 11:05
寂寞天涯人
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-3-18
收藏
得分:0 

谢谢楼上的帮我找错误.其实这个问题,我后来也找出来了.并且改进精确度.做+.-和*的时候还是比较精确的,但是除的时候还是有很大的误差.我不知道怎么去改进,能帮我想想吗?改进后的代码
//产生随机的四则运算题目,输入答案,判断正误
import java.util.*;
import java.io.*;
class suanshi{ //产生随机数//
Random rand=new Random();
int a=(int)rand.nextInt()/1000000;
int b=(int)rand.nextInt()/1000000;
int c=(int)(rand.nextFloat()*10+1);
public static double sum=0;//一定要声明为static的,否则后面的SUM就会为0
public suanshi(){}
char d;
public suanshi(int k){ //产生运算符号//
if(1<=c&&c<=3){
d='+';
sum=a+b;
}
else
if(4<=c&&c<=6){
d='-';
sum=a-b;
}
else
if(7<=c&&c<=8){
d='×';
sum=a*b;
}
else {
d='÷';
sum=a/b;
}
System.out.print("("+a+")"+" "+d+" "+"("+b+")"+"=");

}
}

class panduan extends suanshi {
int result;
public static int fens=0;
panduan(){super(0);}
public int flag=0;
panduan(int i){
if(i==sum) {
System.out.println("你答对了");
flag=1;
fens=fens+1;
}
else {
System.out.println("你答错了"+"\n"+"不要放弃"+"\t");
System.out.println("正确答案应该是:"+sum);
flag=0;
fens=fens-1;
}
}
int f0(){
flag=0;
return flag;
}
int f1(){
flag=1;
return flag;
}
}
//主类开始//
public class ceshi extends panduan{
public int i=0;
String s="";
ceshi(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
i=Integer.parseInt(s);
}catch(IOException e){}
panduan p=new panduan(i);
}
public static void main(String [] args) {
int j=0;
int tt=0;
String ss="";
System.out.println("这是四则运算题!");
System.out.println("***************");
do{
ceshi t=new ceshi();
//int pp=t.fc();
// t.panduan(pp);
System.out.println("输入0继续.输入1推出");
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
ss=in.readLine();
j=Integer.parseInt(ss);
}catch(IOException e){}

switch(j){
case 0:
tt=t.f0();
System.out.println("请继续下一题");break;
case 1:
tt=t.f1();
System.out.println("你的总分为:"+fens);
System.out.println("下次再努力哦!"+"\n"+"谢谢使用,再见^_^!");
System.out.println("***************");break;
}
}while(tt==0);
}
}
比如做(-1332)/(-1546)的时候=0.为什么会这样的,怎么去改进呢?


书籍,电影,音乐的互动交流平台http://www./
2006-03-25 13:17
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

因为你取的值都是int
如果你都取double值的话,就很精确的了
但是你的结果想判断对或错的话,就不容易了
因为位数不一样啊


可惜不是你,陪我到最后
2006-03-25 13:27
寂寞天涯人
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-3-18
收藏
得分:0 

谢谢,有点素了


书籍,电影,音乐的互动交流平台http://www./
2006-03-25 13:49
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

有素不行,还得有荤啊
否则怎么吃得下饭呢


可惜不是你,陪我到最后
2006-03-25 14:10
寂寞天涯人
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-3-18
收藏
得分:0 

说的好.楼上的.我有些问题想单独请教你,可否加我的qq


书籍,电影,音乐的互动交流平台http://www./
2006-03-26 12:26
快速回复:[求助]答案输出错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065614 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved