Four operator extension: changes the program to the Java language and allows user input to validate input results

Source: Internet
Author: User
Tags sca

Topic

Each student choose a direction, the program extension:
1, let the program can accept the user input answers, and determine the right and wrong. Finally, the total number of pairs/errors is given.
2, the program into a Web program, the user by setting parameters, you can get a variety of topics.
3, the program into a Windows graphical interface program.
4, the program into a smartphone program (you are using what cell phone, just write that phone program). (Can be postponed after 3 weeks)
5, choose a programming language you have never learned, try to achieve the basic functions.

Design ideas

This procedure uses an array to record the answer, including the input array and the correct result to save the array, for comparison

Source

Package Minirisoft;import Java.util.random;import Java.util.scanner;public class Sizeyunsuan {public static Random Rand =new Random ();p ublic static class qst{static int Operand (int Range)//generates operand {int opd=rand.nextint (range*2+1)-range; return OPD;} public static char Operatorkind (char operator,boolean IFSUPPMD)//generate operator {int operatorpossible=rand.nextint (4); 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)//determines whether to repeat {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)//Generate answer {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;}    Generate a math problem 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)//To determine whether to repeat {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)//output generated arithmetic question {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 (' \ t ');}}} public static void input (int input[],int qstnum)//input question answer {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)//output answer {for (int j=0;j<qstnum;j++) {System.out.print (ans[j]); if (J%COL==COL-1) {System.out.println ();} Else{system.out.print (' \ t ');}}} 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 ("Correct number of questions:"); 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 ("Please enter the number of generated topics:"); Qstnum=sca.nextint (); System.out.println ("Please enter the Arithmetic range:"); Range=sca.nextint (); System.out.println ("pleaseInput divided by number of columns: "); Col=sca.nextint (); System.out.println ("Whether or not to support multiplication law: (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 ("Input answer:"); System.out.println (); Qst.outans (Input,qstnum,col); SYSTEM.OUT.PRINTLN ("Correct answer:"); System.out.println (); Qst.outans (Ans,qstnum,col); Qst.confirmans (Ans,input,qstnum,count);}}

Program

Experimental summary

This experiment to solve the problem in front of the idea of carding, no longer impulsive, the use of the Java language to do again, experience the difference between Java and C + + and similarities, but also the simplification of the modular algorithm has a certain improvement, the problem of the program itself has been a preliminary solution, I will redouble my efforts!

Four operator extension: changes the program to the Java language and allows user input to validate input results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.