首页 > 代码库 > java程序设计基础篇 复习笔记 第三单元
java程序设计基础篇 复习笔记 第三单元
1单向if语句双向if语句dangling elseswitch:char,byte,short,int2javax.swing.JOptionPane.showConfirmDialog(null,text);返回值:JOptionPane.YES_OPTION:0JOptionPane.NO_OPTION:1JOptionPane.CANCEL_OPTION:23cannot cast int from booleancannot cast boolean from int4%b5printf("%f",2);java.util.IllegalFormatConversionException6java基本是值传参,基本类型不可改,需要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 expressionBoolean valueboolean typebreak statementconditional operatordangling-else ambiguityfall-through behavior:语句从匹配处开始执行,直到遇到break语句或是达到switch语句的末端。operator associativityoperator precedenceselection statementshort-circuit evaluation3.1< <= >= != == >3.2cannot cast 3.330 is even30 is odd________________30 is even3.4z is 5________________z is 7________________x is 23.5a==c==d缩进正确:b,c3.6.............3.7等价3.80.5,0.0,0.2343.9Math.random()*20;Math.random()*10+10;Math.random()*40+10;3.10if(y > 0)x=1;3.11if(score > 90)pay+=1.03;if(score > 90)pay+=1.03;else pay*=1.01;3.12if (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.13boolean fl = count % 10 == 0;if (fl)newLine = true;else newLine = false;3.14false,false,true,true,true,true,3.15x >= 1 && x <= 1003.16(x >= 1 && x <= 100) || x<03.17x = y && yx /= y(x != 0) || (x = 0)3.18223.19true,false,true,false3.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 false3.21age > 13 && age < 183.22weight > 50 || height > 1603.23weight > 50 && height > 1603.24weight > 50 ^ height > 1603.25char,byte,int,long下一个标签内的语句可以可能不行格式紧凑,可读性好3.2623.27switch(a){case 1: x+=5; break;case 2: x+=10; break;case 3: x+=16; break;case 4: x+=34;}3.28switch(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.29System.out.print(count + count % 10 ==0?"\n":" ");3.30pay*=(temperature > 90?1.5:1.1);3.31%b %c %d %f %s3.32变参个数多变参个数少java.util.IllegalFormatConversionException 3.33amount is 32.32 3.232000e+01amount is 32.3200 3.2320e+01false Java falseJavafalse Java 3.35truetrue3.36假,赋值运算符(简捷运算符/=...)3.37falsefalse3.38yes,yes,yes3.39javax.swing.JOptionPane.showConfirmDialog(null,confirmMessage);JOptionPane.YES_OPTION:0JOptionPane.NO_OPTION:1JOptionPane.CANCEL_OPTION:23.1import 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.9import 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.17import 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.20import 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.21import 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.22import 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.27import 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.28import 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"); } }}
java程序设计基础篇 复习笔记 第三单元
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。