1 单向if语句 双向if语句 dangling else switch:char,byte,short,int 2 javax.swing.JOptionPane.showConfirmDialog(null,text); 返回值: JOptionPane.YES_OPTION:0 JOptionPane.NO_OPTION:1 JOptionPane.CANCEL_OPTION:2 3 cannot cast int from boolean cannot cast boolean from int 4 %b 5 printf("%f",2); java.util.IllegalFormatConversionException 6 java基本是值传参,基本类型不可改,需要Integer,Double打包, Field f = r1.getClass().getDeclaredField("value"); f.setAccessible(true); f.setDouble(r1.r1+1); 同时不能使用拷贝构造函数 public static boolean calEquRoot(double a,double b,double c,Double r1,Double r2) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ double delta = a * a - 4 * b * c; if(delta < 0)return false; delta=Math.sqrt(delta); Field f=r1.getClass().getDeclaredField("value"); //r1=Double.valueof(1.0);r2=Double.valueof(2.0); f.setAccessible(true); f.setDouble(r1,Double.valueOf((-b+delta)/2/a)); f.setDouble(r2,Double.valueOf((-b-delta)/2/a)); return true; } String类型为不可变字符串,传入也不改变其值 Keyword: Boolean expression Boolean value boolean type break statement conditional operator dangling-else ambiguity fall-through behavior:语句从匹配处开始执行,直到遇到break语句或是达到switch语句的末端。 operator associativity operator precedence selection statement short-circuit evaluation 3.1 < <= >= != == > 3.2 cannot cast 3.3 30 is even 30 is odd ________________ 30 is even 3.4 z is 5 ________________ z is 7 ________________ x is 2 3.5 a==c==d 缩进正确:b,c 3.6 ............. 3.7 等价 3.8 0.5,0.0,0.234 3.9 Math.random()*20; Math.random()*10+10; Math.random()*40+10; 3.10 if(y > 0)x=1; 3.11 if(score > 90)pay+=1.03; if(score > 90)pay+=1.03; else pay*=1.01; 3.12 if (score >= 90.0)grade = 'A'; else if (score >= 80.0)grade = 'B'; else if (score >= 70.0)grade = 'C'; else if (score >= 60.0)grade = 'D'; else grade = 'F'; 3.13 boolean fl = count % 10 == 0; if (fl)newLine = true; else newLine = false; 3.14 false,false,true,true,true,true, 3.15 x >= 1 && x <= 100 3.16 (x >= 1 && x <= 100) || x<0 3.17 x = y && y x /= y (x != 0) || (x = 0) 3.18 2 2 3.19 true,false,true,false 3.20 (x < y && y < z) is true (x < y || y < z) is true !(x < y) is false (x + y < z) is false (x + y < z) is false 3.21 age > 13 && age < 18 3.22 weight > 50 || height > 160 3.23 weight > 50 && height > 160 3.24 weight > 50 ^ height > 160 3.25 char,byte,int,long 下一个标签内的语句 可以 可能不行 格式紧凑,可读性好 3.26 2 3.27 switch(a){ case 1: x+=5; break; case 2: x+=10; break; case 3: x+=16; break; case 4: x+=34; } 3.28 switch(day){ case 0: dayName = "Sunday"; break; case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; } 3.29 System.out.print(count + count % 10 ==0?"\n":" "); 3.30 pay*=(temperature > 90?1.5:1.1); 3.31 %b %c %d %f %s 3.32 变参个数多 变参个数少 java.util.IllegalFormatConversionException 3.33 amount is 32.32 3.232000e+01 amount is 32.3200 3.2320e+01 false Java falseJava false Java 3.35 true true 3.36 假,赋值运算符(简捷运算符/=...) 3.37 false false 3.38 yes,yes,yes 3.39 javax.swing.JOptionPane.showConfirmDialog(null,confirmMessage); JOptionPane.YES_OPTION:0 JOptionPane.NO_OPTION:1 JOptionPane.CANCEL_OPTION:2 3.1 import java.lang.reflect.Field; import java.util.Scanner; import java.text.*; public class Exercise { public static int calEquRoot(double a,double b,double c,Double r1,Double r2) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ double delta = b * b - 4 * a * c; if(delta < 0)return -1; delta=Math.sqrt(delta); Field f=r1.getClass().getDeclaredField("value"); f.setAccessible(true); f.setDouble(r1,Double.valueOf((-b+delta)/2/a)); f.setDouble(r2,Double.valueOf((-b-delta)/2/a)); if(delta < 1e-10)return 0; return 1; } public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ double a,b,c; Double r1 = Double.valueOf(0),r2 = Double.valueOf(0); Scanner scanner = new Scanner(System.in); System.out.println("Input a:"); a = scanner.nextDouble(); System.out.println("Input b:"); b = scanner.nextDouble(); System.out.println("Input c:"); c = scanner.nextDouble(); int fl=calEquRoot(a,b,c,r1,r2); if(fl == 1){ System.out.println(r1); System.out.println(r2); } else if(fl == 0){ System.out.println(r1); } else { System.out.println("NO ROOT"); } } } 3.9 import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception{ Scanner scanner = new Scanner(System.in); int[] a = new int[9]; int ans=0; String tmp=scanner.next(); for(int i=0;i<9;i++){ a[i]=tmp.charAt(i)-'0'; ans+=(a[i] * (i + 1)) % 11; } ans%=11; System.out.println(tmp+ans); } } 3.17 import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int r = (int)Math.random() * 3; String[] srp={"scissor","rock","paper"}; String[] result={"You won","You lose","It is a draw"}; System.out.print("scissor(0), rock(1), paper(2): "); int a; while((a=scanner.nextInt())<=2 && a>=0){ System.out.print("The computer is "+srp[r]+". You are "+srp[a]+(a==r?" too. ":". ") + (a==r?result[2]:(a-r==1||a-r==-2)?result[0]:result[1])); } } } 3.20 import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); System.out.print("Enter the temperature in Fahrenheit: "); double ta; while((ta=scanner.nextDouble()) < -58||ta > 41){ System.out.print("Enter the temperature in Fahrenheit bewteen -58 and 41: "); } double v; System.out.print("Enter the wind speed miles per hour: "); while((v=scanner.nextDouble()) < 2){ System.out.print("Enter the wind speed miles per hour above 2 miles/h: "); } double ans=35.74 + 0.6215 * ta - 35.75 * Math.pow(v, 0.16) + 0.4275 * ta * Math.pow(v, 0.16); System.out.println("The wind chill index is "+ans); } } 3.21 import java.util.Scanner; public class Exercise { /** * 泽勒一致性 * */ public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int year,month,day,week; System.out.print("Enter year: "); year = scanner.nextInt(); System.out.print("Enter month: "); month = scanner.nextInt(); if(month < 3)month+=12; System.out.print("Enter the day of the month: "); day = scanner.nextInt(); String[] date = { "Saturday","Sunday", "Monday","Tuesday","Wednesday", "Thursday","Friday" }; week = (day + 26*(month + 1)/10 + year % 100 + year % 100 / 4 + year/400 + year/100*5)%7; System.out.println("Day of the week is " + date[week]); } } 3.22 import java.util.Scanner; public class Exercise { public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); double x,y; System.out.print("Enter a point with two coordinates: "); x = scanner.nextDouble(); y = scanner.nextDouble(); if(x * x + y * y <= 100){ System.out.println("Point ("+x+", "+y+") is in the circle"); } else { System.out.println("Point ("+x+", "+y+") is not in the circle"); } } } 3.27 import java.util.Scanner; public class Exercise { final static double eps=1e-16; public static boolean judge(double a,double b,double x,double y){ if(Math.abs(a - b) < eps && a < eps ){ if(Math.abs(x - y) < eps && x < eps){ return true; } else return false; } else{ boolean fl = a * b < 0; boolean fl2 = (b * x + a * y - a * b) > 0; if(fl^fl2){ return false; } else return true; } } public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); double x,y; System.out.print("Enter a point with two coordinates: "); x = scanner.nextDouble(); y = scanner.nextDouble(); if(judge(200,100,x,y)){ System.out.println("Point ("+x+", "+y+") is in the circle"); } else { System.out.println("Point ("+x+", "+y+") is not in the circle"); } } } 3.28 import java.util.Scanner; public class Exercise { final static double eps=1e-16; public static int judge(double[] w,double[] h,double[] x,double[] y){ w[0]/=2;w[1]/=2;h[0]/=2;h[1]/=2; double dx = Math.abs(x[0] - x[1]); double dy = Math.abs(y[0] - y[1]); if(dx > w[0] + w[1]||dy > h[0] + h[1])return 0; if(dx > w[0]||dy > h[0])return 2; return 1; } public static void main(String[] args) throws Exception{ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); double[] x = new double [2],y = new double [2],w = new double [2],h = new double [2]; System.out.println("Enter r1's center x-, y-coordinates, width, and height: "); x[0] = scanner.nextDouble(); y[0] = scanner.nextDouble(); w[0] = scanner.nextDouble(); h[0] = scanner.nextDouble(); System.out.println("Enter r2's center x-, y-coordinates, width, and height: "); x[1] = scanner.nextDouble(); y[1] = scanner.nextDouble(); w[1] = scanner.nextDouble(); h[1] = scanner.nextDouble(); int fl; if((fl = judge(w,h,x,y)) == 0){ System.out.println("r2 does not overlap r1"); } else if(fl == 1){ System.out.println("r2 is inside r1"); } else { System.out.println("r2 overlaps r1"); } } }