zoukankan      html  css  js  c++  java
  • 四则运算程序扩展:将程序改为java语言,并允许用户输入,对输入结果进行验证

    题目

    每个同学选一个方向,把程序扩展一下:
    1、让程序能接受用户输入答案,并判定对错。最后给出总共对/错 的数量。
    2、把程序变成一个网页程序,用户通过设定参数,就可以得到各种题目。
    3、把程序变成一个Windows 图形界面的程序。
    4、把程序变成一个智能手机程序 (你正在用什么手机, 就写那个手机的程序)。(可以延期3周后)
    5、选一个你从来没有学过的编程语言,试一试实现基本功能。

    设计思想

    本次程序采用数组记录答案,包含输入数组和正确结果保存数组,进行比较

    源代码

    package minirisoft;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class SizeYunsuan {
    	public static Random rand=new Random();
    	public static class Qst
    	{
    		static int Operand(int Range)//产生操作数
    		{
    			int Opd=rand.nextInt(Range*2+1)-Range;
    			return Opd;
    		}
    		public static char OperatorKind(char Operator,boolean IfSuppMD)//生成运算符
    		{
    			int OperatorPossible=rand.nextInt(4);
    			if(!IfSuppMD)
    			{
    				OperatorPossible/=2;
    			}
    			switch(OperatorPossible)
                {
    	            case 0:
    	            	Operator='+';
    	                break;
    	            case 1:
    	            	Operator='-';
    	                break;
    	            case 2:
    	            	Operator='*';
    	                break;
    	            case 3:
    	            	Operator='/';
    	                break;
    	            default:
    	            	System.out.print("Error!");
                }
    			return Operator;
    		}
    		public static boolean IfRepeated(String str[],int Location)//判断是否重复
    		{
    			for(int i=0;i<Location;i++)
    			{
    				if(str[i].equals(str[Location]))
    					return true;
    			}
    			return false;
    		}
    		public static int Ans(int ans,int Operand1,int Operand2,char Operator)//生成答案
    		{
    			switch(Operator)
                {
    	            case '+':
    	            	ans=Operand1+Operand2;
    	                break;
    	            case '-':
    	            	ans=Operand1-Operand2;
    	                break;
    	            case '*':
    	            	ans=Operand1*Operand2;
    	                break;
    	            case '/':
    	            		ans=Operand1/Operand2;
    	                break;
    	            default:
    	            	System.out.print("Error!");
                }
    			return ans;
    		}
    		//生成一道运算题
    		public static void CreateStr(int Range,char Operator,String str[],int i,int QstNum,int ans[],boolean IfSuppMD)
    		{	    int answer = 0;
    				Qst.OperatorKind(Operator,IfSuppMD);
    				int Operand1=Qst.Operand(Range);
    				int Operand2=Qst.Operand(Range);
    				str[i]=Integer.toString(Operand1);
    				str[i]+=Operator;
    				str[i]+=Integer.toString(Operand2);
    				str[i]+="=";
    				while(IfRepeated(str,i))//判断是否重复
    				{
    					Operand1=Qst.Operand(Range);
    					Operand2=Qst.Operand(Range);
    					str[i]=Integer.toString(Operand1);
    					str[i]+=Operator;
    					str[i]+=Integer.toString(Operand2);
    					str[i]+="=";
    				}
    					ans[i]=Qst.Ans(answer,Operand1,Operand2,Operator);
    		}
    		public static void Display(String str[],int Col)//输出生成的运算题
    		{
    			
    			for(int j=0;j<str.length;j++)
    			{
    				System.out.print(str[j]);
    				if(j%Col==Col-1)
    				{
    					System.out.println();
    				}
    				else
    				{
    					System.out.print('	');
    				}
    			}
    		}
    		public static void Input(int Input[],int QstNum)//输入问题答案
    		{
    			Scanner sca=new Scanner(System.in);
    			for(int j=0;j<QstNum;j++)
    			{
    				Input[j]=sca.nextInt();
    			}
    		}
    		public static void OutAns(int ans[],int QstNum,int Col)//输出答案
    		{
    			for(int j=0;j<QstNum;j++)
    			{
    				System.out.print(ans[j]);
    				if(j%Col==Col-1)
    				{
    					System.out.println();
    				}
    				else
    				{
    					System.out.print('	');
    				}
    			}
    		}
    		public static void ConfirmAns(int ans[],int Input[],int QstNum,int count)
    		{
    			count=0;
    			for(int i=0;i<QstNum;i++)
    			{
    				if(ans[i]==Input[i])
    					count++;
    			}
    			System.out.println("答题正确个数:");
    			System.out.print(count);
    		}
    		
    	}
    	
    	public static void main(String args[])
    	{
    		int Range,QstNum=0,Col=0,count=0;
    		boolean IfSuppMD=true;
    		char Operator = '+';
    		Scanner sca=new Scanner(System.in);
    		System.out.println("请输入生成题目的数量:");
    		QstNum=sca.nextInt();
    		System.out.println("请输入算数范围:");
    		Range=sca.nextInt();
    		System.out.println("请输入划分列数:");
    		Col=sca.nextInt();
    		System.out.println("是否支持乘除法:(true/false)");
    		IfSuppMD=sca.nextBoolean();
    		String str[] = new String[QstNum];
    		int ans[]=new int[QstNum];
    		int Input[]=new int[QstNum];
    		for( int i=0;i<QstNum;i++)
    		{
    			try
    			{
    				Qst.CreateStr(Range,Qst.OperatorKind(Operator,IfSuppMD),str,i,QstNum,ans,IfSuppMD);
    			}
    			catch(Exception e)
    			{
    				i--;
    			};
    		}
    		Qst.Display(str,Col);
    		Qst.Input(Input, QstNum);
    		System.out.println("输入答案:");
    		System.out.println();
    		Qst.OutAns(Input,QstNum,Col);
    		System.out.println("正确答案:");
    		System.out.println();
    		Qst.OutAns(ans,QstNum,Col);
    		Qst.ConfirmAns(ans,Input,QstNum,count);
    	}
    
    
    }
    

      程序截图

    实验总结

    本次实验对前面解决该问题的思路进行了梳理,不再意气用事,采用java语言重新做了一遍,体验到了java和C++的区别与相似之处,也对模块化算法的精简有了一定的提高,程序本身的问题进行了初步的解决,我会再接再厉!

  • 相关阅读:
    js 中的基本包装类型
    js监听浏览器,关闭,刷新(兼容IE6+,Firefox,Chrome,Safari)
    js 中的 && 与 ||
    程序设计模式的工厂(Factory)模式
    关于MOSS首页不能打开提示“根级别上的数据无效
    获取汉字的拼音的首个字母方法
    XmlDocument扩展类
    初次体验Android,过程很艰辛!
    用Python写个翻译工具
    开发经验是修炼设计模式的基石
  • 原文地址:https://www.cnblogs.com/tianma-0/p/4355758.html
Copyright © 2011-2022 走看看