A simple addition, subtraction, multiplication, division, automatic generation of small programs (JAVA), addition, subtraction, multiplication, division, and java

Source: Internet
Author: User

A simple addition, subtraction, multiplication, division, automatic generation of small programs (JAVA), addition, subtraction, multiplication, division, and java

When I was studying software engineering, I encountered such a problem. When a programmer's son went to the second grade of elementary school, the teacher asked the parents to give 30 questions to the students every day. Because the parents were programmers, so, he wrote a program implementation by himself. Our lovely teacher also told us to write a program similar to this, but I still have some shortcomings in this program. First: if the question does not match the meaning of the question, it should only be able to add or subtract, but I still have the multiplication and division, haha, but it can be changed a little. The second is that although the program can output the answer, because I set the result to only the INT type, the answer to division is incorrect. The third is that some multiplication numbers are relatively large, and the second-year users cannot calculate the result; also, I have not made any parentheses. Because I am a beginner in JAVA, it took quite a few hours to develop this program. Because there are some modifications. The source code is provided below:

Import java. util. collections;
Public class Sizeyunsuan {
Public static void main (String [] args ){
Running in = new Processing (System. in );
System. out. println ("the number of arithmetic operations you want to output is :");
Int num = in. nextInt ();
String [] a = {"+ ","-","*","/"};
Int [] B;
Int [] c; int j = 0;
B = new int [4]; c = new int [4];
Do {
Int sum =-1;
For (int I = 0; I <4; I ++)
{
B [I] = (int) (Math. random () * 100 );
}
For (int I = 0; I <4; I ++)
{
C [I] = (int) (Math. random () * 14/3 );
}
Switch (c [0])
{
Case 1: sum = B [0] + B [1]; break;
Case 2: sum = B [0]-B [1]; break;
Case 3: sum = B [0] * B [1]; break;
Case 4: sum = B [0]/B [1]; break;
}
If (sum <0)
{
J --;
}
Else
{
System. out. println (B [0] + a [c [0]-1] + B [1] + "= ");
// System. out. println (sum); // The result is displayed.
}
J ++;
} While (j <num );
}
}

Related Article

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.