一、true/falsejava
Java中不容許將一個數字做爲布爾值使用。git
if(a)//不容許,若a爲非布爾值 //能夠轉化爲 if(a != 0)
二、「else if」並不是是一個新的關鍵字,僅僅是一個else後面緊跟if語句。數組
package Fourth; /*隨機輸出25個int型隨機數,對每個隨機數使用if-else語句將其分類爲大於、小於、等於緊隨它生成的隨機數*/ import java.util.Random; public class IfElseNextInt { public static void main(String[] args){ int[] a = new int[25]; int count1 = 0,count2 = 0,count3 = 0; Random random = new Random(); for(int i = 0;i<25;i++){ a[i] = random.nextInt(); System.out.print(a[i]+" "); } System.out.println(" "); for(int j = 0; j<24; j++){ if(a[j] < a[j+1]){ System.out.println(a[j]+"小於"+a[j+1]); count1++; }else if(a[j]< a[j+1]){ System.out.println(a[j]+"等於"+a[j+1]); count2++; }else{ System.out.println(a[j]+"大於"+a[j+1]); count3++; } } System.out.println("count1 = "+count1); System.out.println("count2 = "+count2); System.out.println("count3 = "+count3); } }/*Outputs: -1354703007 1805422196 -1297880545 1793371722 -234103217 -833903965 -1176050239 1179914159 1647912769 -917685928 -645989902 522511923 -1215204011 1866894430 -998138732 716269013 -469080041 -1602332390 -1863176595 -160521188 -1280051709 -2121712965 1478433613 866361195 -1817443512 -1354703007小於1805422196 1805422196大於-1297880545 -1297880545小於1793371722 1793371722大於-234103217 -234103217大於-833903965 -833903965大於-1176050239 -1176050239小於1179914159 1179914159小於1647912769 1647912769大於-917685928 -917685928小於-645989902 -645989902小於522511923 522511923大於-1215204011 -1215204011小於1866894430 1866894430大於-998138732 -998138732小於716269013 716269013大於-469080041 -469080041大於-1602332390 -1602332390大於-1863176595 -1863176595小於-160521188 -160521188大於-1280051709 -1280051709大於-2121712965 -2121712965小於1478433613 1478433613大於866361195 866361195大於-1817443512 count1 = 10 count2 = 0 count3 = 14*///~
三、Foreachdom
Java1.5之後新增的foreach語法this
package Fourth; import java.util.Random; public class Foreach { public static void main(String[] args){ Random random = new Random(); float[] a = new float[10]; for(int i = 0;i < 10; i++){ a[i] = random.nextFloat(); } /**java1.5之後,此條語句定義了一個float型的x,繼而將每個 * 數組a的值賦給x*/ for(float x: a){ System.out.print(x+" "); } } }/*Outputs: 0.32246327 0.59484965 0.20389026 0.70910615 0.31593043 0.6692021 0.4307089 0.36957538 0.87660265 0.8898278*/
package Fourth; public class ToCharArray { public static void main(String[] args){ String str = "IwanttobetrongandIwill"; /**String類的toCharArray()方法 * char[] java.lang.String.toCharArray() * Converts this string to a new character array.*/ for(char x: str.toCharArray()) System.out.print(x+" "); } }/*Outputs: I w a n t t o b e t r o n g a n d I w i l l */
四、forspa
package Fourth; /*輸出100之內的素數*/ public class ForSuShu { public static void main(String[] args){ /*int i, j; for (i = 2; i <= 100; i++) { for (j = 2; j < i; j++) { if (i % j == 0) break;//跳出內層循環 } if (j >= i) System.out.println(i); }*/ boolean bool; for (int i = 3; i < 100; i+=2) { bool = true; for (int j = 3; j <= Math.sqrt(i); j++) { if (i % j == 0) { bool = false; break; } } if (bool) System.out.print(i + " "); } } }/*Outputs: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97*/
package Fourth; public class E10_Vampire { public static void main(String[] args) { int[] startDigit = new int[4]; int[] productDigit = new int[4]; for(int num1 = 10; num1 <= 99; num1++) for(int num2 = num1; num2 <= 99; num2++) { // Pete Hartley's theoretical result: // If x·y is a vampire number then // x·y == x+y (mod 9) if((num1 * num2) % 9 != (num1 + num2) % 9) continue; int product = num1 * num2; startDigit[0] = num1 / 10; startDigit[1] = num1 % 10; startDigit[2] = num2 / 10; startDigit[3] = num2 % 10; productDigit[0] = product / 1000; productDigit[1] = (product % 1000) / 100; productDigit[2] = product % 1000 % 100 / 10; productDigit[3] = product % 1000 % 100 % 10; int count = 0; for(int x = 0; x < 4; x++) for(int y = 0; y < 4; y++) { if(productDigit[x] == startDigit[y]) { count++; productDigit[x] = -1; startDigit[y] = -2; if(count == 4) System.out.println(num1 + " * " + num2 + " : " + product); } } } } } /*Outputs: 15 * 93 : 1395 21 * 60 : 1260 21 * 87 : 1827 27 * 81 : 2187 30 * 51 : 1530 35 * 41 : 1435 80 * 86 : 6880*/