java总结

2020年10月4日:

今天下午上了四节java课,最后被要求要完成一个简单的四则运算项目,其特点与上次的四则运算类似,但不同的是这次的四则运算与上次相比多了几个条件,那就是用不同的方法对其进行补充,把整个程序进行合理的分类,然后在运行,下面是我的粗略代码:

package shuxue;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class shuxue
{
 public static int i;
 public static int n;
 public static int j;
 public static int k;
 public static int t;
 public static int g;
 public static char o;
 public static char y;
 public static int str;
 public static int daan1[];
 public static String shu[];
 public static int d;
 public static int dui;
 static Random a=new Random();
 public static List list;
 public static String shuzu[];
 public static int daanshuzu[];
 public static void main(String[] args)
 {
  Scanner scan=new Scanner(System.in);
  System.out.println("请输入题的数量:");
  System.out.println("请输入数值范围:");
  System.out.println("请输入操作数个数:");
     t=scan.nextInt();
  n=scan.nextInt();
  j=scan.nextInt();
  daanshuzu=new int [t];
  shu=new String[t];
     daan1=new int[t];
  if(j>2)
  {
  System.out.println("请选择是否需要乘除法(Y or N):");
  System.out.println("请选择是否需要括号(Y or N):"); 
     o=scan.next().charAt(0);
     y=scan.next().charAt(0);
  }
     chutigeshu(t);
  for(int p=1;p<=t;p++)
  {
      list=new ArrayList();
   System.out.print("第"+p+"题:  ");
      caozuoshugeshu(j);
      if(j>2)
      {
    shuzu[p-1]="";
    for(int u=0;u<list.size();u++)
    {
      shuzu[p-1]=shuzu[p-1]+list.get(u);
    }
    shuzu[p-1]+="=";
   chachong(shuzu[p-1],p);
   System.out.println(shuzu[p-1]);
      }
      else
      {
        System.out.print(shu[p-1]+"=");
      }
   int daan=scan.nextInt();
   daan(daan);
        }
  panduan(daanshuzu,daan1,shu);
 }
 public static void panduan(int a[],int b[],String c[])
 {
  for(int i=0;i<t;i++)
  {
   if(a[i]==b[i])
   {
    dui++;
   }
   else
   {
    System.out.println(c[i]);
   }
  }
  System.out.println("正确率为:"+(double)dui/(double)t);
 }
 public static void daan(int daan)
 {  
  daanshuzu[g++]=daan;   
 }
 public static void chachong(String str,int p)
 {
  boolean is=false;
  while(is)
  {
  for(int s=0;s<p-1;s++)
  {
   if(str.equals(shuzu[s]))
   {
    caozuoshugeshu(j);
   }
   else
    k++;
  }
  if(k==p-1)
  {
   is=true;
  }
  }
 }
 public static void caozuoshugeshu(int j)
 {
  if(j>2)
  {
  for(int i=0;i<2*j-1;i++)
  {
   if((i+1)%2!=0)
   {
    list.add(caozuoshufanwei(n));
   }
   else
   {
    int c=a.nextInt(4)+1;
    if(o=='N')
    {
     if(c==3||c==4)
     {
      c=a.nextInt(2)+1;
     }
    }
    switch(c)
    {
    case 1:list.add('+');break;
    case 2:list.add('-');break;
    case 3:list.add('*');break;
    case 4:list.add('/');break;
    }
   }
  }
  if(y=='Y')
  {
   int v=a.nextInt(2*j-3);
   int g=a.nextInt(2*j+1);
   if((v%2==0&&g%2==0)&&(g-v>=4))
   {
    list.add(v,'(');
    list.add(g,')');
   }
  }
  }
  else
  {
   caozuoliang();
  }
 }
 public static void caozuoliang()
 {
  int b=a.nextInt(n)+1;
  int c=a.nextInt(n)+1;
     switch(a.nextInt(4)+1)
     {
     case 1:
      daan1[d++]=b+c;
      shu[str++]=b+"+"+c;
      break;
     case 2:
      daan1[d++]=b-c;
      shu[str++]=b+"-"+c;
      break;
     case 3:
      daan1[d++]=b*c;
      shu[str++]=b+"*"+c;
      break;
     case 4:
      daan1[d++]=b/c;
      shu[str++]=b+"/"+c;
      break;
     }
 }
 public static void chutigeshu(int t)
 {
   shuzu=new String[t];
 }
 public static int caozuoshufanwei(int n)
 {
  return a.nextInt(n)+1;
 }
}
 这次的代码是我在上次的代码基础上进行改进的,以不同的方法对程序的命令进行分类,使其整个程序看起来更加的井然有序,当然,我的代码还有很多的不足,在下周周二前争取把代码完善,把剩下的功能给尽量的昨晚,当然,前提是我能的话。
原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/13768466.html