| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 534 人关注过本帖
标题:[讨论]一道至今不能解决的项目
只看楼主 加入收藏
路在何方
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-6-13
收藏
 问题点数:0 回复次数:1 
[讨论]一道至今不能解决的项目

利用随机数生成器,编写一个小学生的四则运算的练习程序,要求:
1)可以进行难度选择。一级难度十一位数的运算,二级难度是两位数的运算,三级难度是三位数的运算。
2)可以选择运算类型,包括加、减、乘、除。
3)给出适当的错误提示
4)可以根据计算结果统计成绩。
程序如下:
/************************************************************
Programmer:zhaochen
*Date Written:june 13,2006
*This program displays "yunsuan" to the standard output
************************************************************/
import java.io.*;
public class math
{
int a,b,diffcon=0,total;
char op;


public int InputIntData() throws IOException
{
byte buf[]=new byte[20];
String str;
System.in.read(buf); //从键盘读入数据,保存在buf数组第0个位置中
str=new String(buf,0); //buf数组第0个位置中的数据赋值给变量str
return Integer.parseInt(str.trim());
}
public char InputCharData() throws IOException
{
return (char)(System.in.read());
}
public void menu1()
{
do{
System.out.println("请选择一个难度:");
System.out.println(" 一位数运算————1");
System.out.println(" 两位数运算————2");
System.out.println(" 三位数运算————3");
System.out.println(" 不做了,退出————0");

//程序暂停,enter回车程序继续执行
try{
diffcon=InputIntData();

}catch(IOException e){}
}while(diffcon!=0&&diffcon!=1&&diffcon!=2&&diffcon!=3);
}

public void menu2()
{
do{
System.out.println("选择运算符,并给出答案:");
System.out.println(" 加法运算符————+");
System.out.println(" 减法运算符————-");
System.out.println(" 乘法运算符————*");
System.out.println(" 除法运算符————/");

try{
op=InputCharData();

}catch(IOException e){}
}while(op!='+'&&op!='-'&&op!='*'&&op!='/');
}

public void menu3(){
String s= "";
switch(diffcon){
case 1:a=(int)(Math.random()*10);b=(int)(Math.random()*10);break;
case 2:a=(int)(Math.random()*100);b=(int)(Math.random()*100);break;
case 3:a=(int)(Math.random()*1000);b=(int)(Math.random()*1000);break;
default:a=-2;b=-2;
try
{
BuffeReader in = new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}
catch(IOException e){}
int number=Interger.parseInt(s);
if(number=num)
System.out.printlnf("答对了!");
else
System.out.printlnf("答错了!正确答案是"+num);
}
}

public int menu4(){
switch(op){
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '%': return a/b;
default: return -2;
}
}
//编写方法进行运算
public int jisuan(int a,int b,char op)
{
return a+b;
}


public static void main(String args[]) throws IOException
{
math es=new math();
while(true)
{
es.menu1(); //难度选择菜单
es.menu2(); //运算符选择菜单
es.menu3();
es.total=es.menu4();
System.out.print(es.a+" "+es.b);
System.out.println("的运算结果为"+es.total); break;



}
}
}
不知为什么我总是不能解决出上述算法的思想,如果各位同行有好的程序,在下感谢不激。

搜索更多相关主题的帖子: 项目 
2006-06-14 18:58
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你这个程序连编译都通过不了
try
{
BuffeReader in = new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}
catch(IOException e){}
int number=Interger.parseInt(s);//应该是Integer
if(number=num)//应该是number==num
System.out.printlnf("答对了!");//应该是println
else
System.out.printlnf("答错了!正确答案是"+num);//num是哪里定义的?
}
}

可惜不是你,陪我到最后
2006-06-14 19:37
快速回复:[讨论]一道至今不能解决的项目
数据加载中...
 
   



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

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