Java beginners: a few simple small programs and new beginners, java small programs

Source: Internet
Author: User

Java beginners: a few simple small programs and new beginners, java small programs

Sort out several small programs written in the past few days, all of which are loop exercises.
// Set N to a four-digit number, which is 9 times the reverse order number.
// (For example, the inverse Number of 1234 is 4321), evaluate the N value
Package Azhi;
Public class Job_5 {
Public static void main (String [] args ){
For (int n = 1000; n <= 9999; n ++ ){
Int ge = n % 10;
Int shi = n % 100/10;
Int bai = n % 1000/100;
Int qian = n/1000;
Int temp = ge * 1000 + shi * 100 + bai * 10 + qian; // obtain the reverse order number.
If (temp = n * 9) {// It is 9 times its reverse order number.
System. out. println (n );
}
}
}
}

/* Print all "Daffodils ",
* The so-called "Daffodils" refers to a three-digit number, each of which is a numerical cube and equals to the number itself.
* For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3 */
Package Azhi;
Public class Job_6 {
Public static void main (String [] args ){
For (int I = 100; I <999; I ++ ){
Int g = I % 10;
Int s = I %100/10;
Int B = I/100;
Int temp = g * g + s * s + B * B; // each digit in three digits is equal to the number itself.
If (temp = I ){
System. out. print ("\ t" + I );
}
}
}
}

// Calculate the result of 1-2 + 3-4 + 5-... + 99-100
Package Azhi;
Public class Job_7 {
Public static void main (String [] args ){
Int sum = 0, sum1 = 0, sum2 = 0;
For (int I = 1; I <= 100; I ++ ){
If (I % 2 = 0) {// even number for Subtraction
Sum1-= I;
} Else {
Sum2 + = I; // Add an odd number
}
}
Sum = sum1 + sum2;
System. out. print (sum );
}
}

// Calculate the sum of the first 20 items of 2/1 + 3/2 + 5/3 + 8/5 + 13/8...
Package Azhi;
Public class Job_8 {
Public static void main (String [] args ){
Int I = 0;
Double x = 2.0, y = 1.0, z = 0.0;
Double sum = 0;
For (I = 1; I <= 20; I ++ ){
Sum = sum + x/y;
Z = x;
X = x + y;
Y = z;
}
System. out. print (sum );
}
}

// Output the 9*9 multiplication table
Package Azhi;
Public class Job_9 {
Public static void main (String [] args ){
For (int x = 1; x <= 9; x ++ ){
For (int y = 1; y <= x; y ++ ){
System. out. print ("\ t" + y + "×" + x + "=" + y * x );
}
System. out. println ();
}
}
}

/* Use the while LOOP
Calculate the multiples of 7 from 1 to and the sum of numbers with 7. 7 14 17 21 27 28 ...*/
Package Azhi;
Public class Seven100 {
Public static void main (String [] args ){
Int I = 0;
Int sum = 0;
While (I <= 1000 ){
Int ge = I % 10;
Int shi = I/10% 10;
Int bai = I/100;
If (I % 7 = 0 | ge = 7 | shi = 7 | bai = 7 ){
Sum + = I;
}
I ++;
}
System. out. println (sum );
}
}

These are simple small programs written over the past few days, but for beginners like me, these small programs are very good. First, I practiced the basic syntax, and then through these small programs, enhance your understanding of the program. I still have some problems writing these simple programs over the past few days. Take the question "2/1 + 3/2 + 5/3 + 8/5 + 13/8 ..... for the sum of the first 20 items, I haven't found the rule of this question for a long time. If I can't find the rule, I can't write the expression. Then I went to Baidu after thinking for a long time, then du Niang told me that it was very simple. I also found that it was very simple, but I didn't think of it at first. It may be more about my own exercises, but I don't know much about such questions and lack of consciousness.

Then, for the output of the 99 multiplication table, the inner loop I wrote is for (int y = 1; y <= 9; y ++). Now it is obvious that a rectangle is output, but I couldn't understand it at the time. To put it bluntly, I still couldn't understand it. The buddy next to me told me that I would write y <x because it loops in X.

Over the past few days, I have a general feeling that I need to work diligently to make it happen. For example, I used to calculate the number of days in a certain month in a certain year, mainly for the evaluation of a leap year. I wrote a lot about the whole program and it looked complicated and messy, but when I spoke about it, just a few simple words, and then I went back to what I just said. I practiced more and did more, and my experience was very important.


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.