This is the subject URL: http://student.csdn.net/mcd/topic/235300/753730
Note:
With the code 9L @ jetvster added, it's amazing. At first glance, I thought it was not very good.
I feel like 7L @ SeeMore91 and 8L have a good idea. I want to learn more.
I just saw a reply from 3L. Here I declare that the following is only my own code and ideas. Please do not think this is an explanation, but just take a look at the previous code, seek guidance and guidance
In addition, the output statement is only for test results. Ignore it! If you need to fully satisfy the question, modify line 26th of the Code to: System. out. println (txt); then add the default part to the switch, and the statement is a continue, directly giving up the next cycle of execution in this round
I thought it was quite simple, but after writing it for a while, it became more and more difficult. So I made full use of our Java object-oriented features and completed the writing.
Code (it seems like this is the case. If there are errors or omissions, we hope our predecessors can correct them)
PS: I just checked the encyclopedia. The original positive integer is an integer greater than 0. So, modify the code.
/*** Traverse all positive integers from 0 to 100 in sequence. * If the number can be divisible by 3, the number and '*' Mark are output; * If the number can be divisible by 5, the number and '#' are marked. * If the number can be divisible by 3 and 5, the number and '* #' Mark are output. */Public class Print {public static void main (String [] args) {new Print (). prints ();} private void prints () {for (int I = 1; I <= 100; I ++) {String txt = ""; int flag = 0; if (op3 (I) {flag + = 1;} if (op5 (I) {flag + = 2;} switch (flag) {case 1: txt = "*"; break; case 2: txt = "#"; break; case 3: txt = I + "#"; break;} System. out. println ("current number:" + I + "---->" + txt) ;}} private boolean op3 (int I) {if (I % 3 = 0) {return true;} return false;} private boolean op5 (int I) {if (I % 5 = 0) {return true;} return false ;}}
Output:
Current number: 1 ----> current number: 2 ----> current number: 3 ----> * Current number: 4 ----> current number: 5 ----> # current number: 6 ----> * Current number: 7 ----> current number: 8 ----> current number: 9 ----> * Current number: 10 ----> # current number: 11 ----> current number: 12 ----> * Current number: 13 ----> current number: 14 ----> current number: 15 ----> 15 # current number: 16 ----> current number: 17 ----> current number: 18 ----> * Current number: 19 ----> current number: 20 ----> # current number: 21 ----> * Current number: 22 ----> current number: 23 ----> current number: 24 ----> * Current number: 25 ----> # current number: 26 ----> current number: 27 ----> * Current number: 28 ----> current number: 29 ----> current number: 30 ----> 30 # current number: 31 ----> current number: 32 ----> current number: 33 ----> * Current number: 34 ----> current number: 35 ----> # current number: 36 ----> * Current number: 37 ----> current number: 38 ----> current number: 39 ----> * Current number: 40 ----> # current number: 41 ----> current number: 42 ----> * Current number: 43 ----> current number: 44 ----> current number: 45 ----> 45 # current number: 46 ----> current number: 47 ----> current number: 48 ----> * Current number: 49 ----> current number: 50 ----> # current number: 51 ----> * Current number: 52 ----> current number: 53 ----> current number: 54 ----> * Current number: 55 ----> # current number: 56 ----> current number: 57 ----> * Current number: 58 ----> current number: 59 ----> current number: 60 ----> 60 # current number: 61 ----> current number: 62 ----> current number: 63 ----> * Current number: 64 ----> current number: 65 ----> # current number: 66 ----> * Current number: 67 ----> current number: 68 ----> current number: 69 ----> * Current number: 70 ----> # current number: 71 ----> current number: 72 ----> * Current number: 73 ----> current number: 74 ----> current number: 75 ----> 75 # current number: 76 ----> current number: 77 ----> current number: 78 ----> * Current number: 79 ----> current number: 80 ----> # current number: 81 ----> * Current number: 82 ----> current number: 83 ----> current number: 84 ----> * Current number: 85 ----> # current number: 86 ----> current number: 87 ----> * Current number: 88 ----> current number: 89 ----> current number: 90 ----> 90 # current number: 91 ----> current number: 92 ----> current number: 93 ----> * Current number: 94 ----> current number: 95 ----> # current number: 96 ----> * Current number: 97 ----> current number: 98 ----> current number: 99 ----> * Current number: 100 ----> # Process finished with exit code 0
The 9L code is easy to understand. It is better to understand the meaning of the question by modifying the keyword.
Public class Test {public static void main (String [] args) {int max = 100; int flag3 = 1; // positive integers do not include 0, so count int flag5 = 1; int sum3 = 0; // I % 3 = 0 & I % 15 = 0 int sum5 = 0; // I % 5 = 0 while (sum3 <max | sum5 <max) {sum3 = flag3 * 3; sum5 = flag5 * 5; if (sum3 <sum5) {System. out. println (sum3 + ": *"); flag3 ++;} else if (sum3> sum5) {System. out. println (sum5 + ": #"); flag5 ++;} else {System. out. println (sum3 + ": * #"); flag5 ++; flag3 ++ ;}}}}
The output is as follows (do not tangle. If you want to fully match the meaning of the question, you only need to modify the corresponding sout method. Here, we only want to make the test results look better)
3:*5:#6:*9:*10:#12:*15:*#18:*20:#21:*24:*25:#27:*30:*#33:*35:#36:*39:*40:#42:*45:*#48:*50:#51:*54:*55:#57:*60:*#63:*65:#66:*69:*70:#72:*75:*#78:*80:#81:*84:*85:#87:*90:*#93:*95:#96:*99:*100:#