一、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*/