码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算

时间:2018-11-15 23:40:09      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:time()   exception   highlight   test   static   答案   rand   row   组元   


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());*/
	       
	    }
}

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

四则运算

标签:time()   exception   highlight   test   static   答案   rand   row   组元   

原文地址:https://www.cnblogs.com/zhang12345/p/9966799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!