zoukankan      html  css  js  c++  java
  • 四则运算

    
    
    import java.io.*;
    import java.util.Random;
    import java.util.Scanner;
    public class yunsuan 
    {	 static  Scanner sc=new Scanner(System.in);
    static int[] c=new int[100];
    static int m=0;
    static int index1=0;
    static int index2=0;
    public static void main(String[] args)throws IOException {
    	
    
    		 try {
    	        	BufferedWriter out = new BufferedWriter(new FileWriter("D:/ps/Test.txt"));
    	           for(int n=0;n<100;n++) 
    	           {
    	        	   int i=(int)(1+Math.random()*100); 
    	               int j=(int)(1+Math.random()*100);
    	               int k=(int)(1+Math.random()*100);
    	               Random r=new Random();
    	               int a=r.nextInt(4);
    	               int b=r.nextInt(4);
    	           if(a==0)
    	           {
    	        	   if(b==0)
    	            {out.write(i+"+"+j+"+"+k+"=");
    	            out.newLine();
    	            out.write("*");
    	            out.newLine();
    	              c[n]=i+j+k;
    	          }
    	           	  if(b==1) 
    	           		{out.write(i+"+"+j+"-"+k+"=");	
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		            c[n]=i+j-k;
    		       
    		            }
    	         	            if(b==2) 
    	            	{out.write(i+"+"+j+"*"+k+"=");
    	        	            out.newLine();
    	        	            out.write("*");
    	        	            out.newLine();
    	        	          c[n]=i+j*k;
    	        	          }
    	            if(b==3)
    	            
    	            	{out.write(i+"+"+j+"/"+k+"=");
    	            out.newLine();
    	            out.write("*");
    	            out.newLine();
    	            c[n]=i+j/k;
    	     
    	            }
    	        	   }
    	           if(a==1)
    	           {   
    	        	   if(b==0)
    	            {out.write(i+"-"+j+"+"+k+"=");
    	            out.newLine();
    	            out.write("*");
    	            out.newLine();
    	        c[n]=i-j+k;
    	        
    	            }
    	           	  if(b==1) 
    	           	  {
    	           		out.write(i+"-"+j+"-"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		         c[n]=i-j-k;
    		    
    	           }
    	            if(b==2) {
    	            	out.write(i+"-"+j+"*"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		         c[n]=i-j*k;
    		     
    	        	  }
    	            if(b==3)
    	            {
    	            	out.write(i+"-"+j+"/"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		            c[n]=i-j/k;
    		    
    	            }
    	            }
    	           if(a==2)
    	           {   if(b==0)
    	            {out.write(i+"*"+j+"+"+k+"=");
    	            out.newLine();
    	            out.write("*");
    	            out.newLine();
    	          c[n]=i*j+k;
    	       
    	            }
    	           	  if(b==1) 
    	           	  {
    	           		out.write(i+"*"+j+"-"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		        c[n]=i*j-k;
    		       
    	           }
    	            if(b==2) {
    	            	out.write(i+"*"+j+"*"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		       c[n]=i*j*k;
    		         
    	        	  }
    	            if(b==3)
    	            {
    	            	out.write(i+"*"+j+"/"+k+"=");
    	            	out.newLine();
    	            	  out.write("*");
    	            	  out.newLine();
    	            	c[n]=i*j/k;
    	            	
    		
    	            }	          
    	            }
    	           if(a==3)
    	           {   
    	        	   if(b==0)
    	            {out.write(i+"/"+j+"+"+k+"=");
    	            out.newLine();
    	            out.write("*");
    	            out.newLine();
    	        c[n]=i/j+k;
    	        
    	            }
    	           	  if(b==1) 
    	           	  {
    	           		out.write(i+"/"+j+"-"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		      c[n]=i/j-k;
    		        
    	           }
    	            if(b==2)
    	            {
    	            	out.write(i+"/"+j+"*"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		       c[n]=i/j*k;
    		         
    	        	  }
    	            if(b==3)
    	            {
    	            	out.write(i+"/"+j+"/"+k+"=");
    		            out.newLine();
    		            out.write("*");
    		            out.newLine();
    		       c[n]=i/j/k;
    		      
    	            }
    	            }
    	           }
    	         
    
    	             out.close();
    	            System.out.println("文件创建成功");
    	            FileReader fr=new FileReader("D:/ps/Test.txt");
    	    		BufferedReader br=new BufferedReader(fr);
    	    		String line=br.readLine();
    	    		while(line!=null)
    	    		{  
    	    		if(line.length()!=1)
    	    		{
    	    			System.out.println(line);
    	    		 int x=sc.nextInt();
    	    		 System.out.println(c[m]);
    	    			if(x==c[m])
    	    				
    	    				{System.out.println("答案正确!");
    	    			index1++;}
    	    			else
    	    				{System.out.println("答案错误!");
    	    				index2++;
    	    				}
    	    			System.out.println("答对:"+index1+"道");
    	    			System.out.println("答错:"+index2+"道");
    	    			m++;
    	    				}
    	    		line= br.readLine();
    	    			}
    	    		br.close();
    	    		}
    	 
    		 catch (IOException e) {
    	        }
    		 
    	        
    
    	       /* File file = new File("E:01.html");
    	        Runtime ce=Runtime.getRuntime();
    	        System.out.println(file.getAbsolutePath());
    	        ce.exec("cmd /c start "+file.getAbsolutePath());*/
    	       
    	    }
    }
    

     问题:不知道怎么将数据写入文件,后来通过数组的方式来写入文件。由于数组元素个数不正确,导致溢出,通过调整数组元素个数解决问题。

  • 相关阅读:
    运营平台——效率型后台管理类产品交互设计心得
    代码在线编译器(下)- 用户代码安全检测
    代码在线编译器(上)- 编辑及编译
    SimpleDateFormat并发隐患及其解决
    Spring-SpringMVC父子容器&AOP使用总结
    PaaS服务之路漫谈(三)
    PaaS服务之路漫谈(二)
    PaaS服务之路漫谈(一)
    Spring 属性配置
    如何通过临时表来加快数据库查询的性能
  • 原文地址:https://www.cnblogs.com/zhang12345/p/9966799.html
Copyright © 2011-2022 走看看