java程序设计基础篇 复习笔记 第三单元
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");
}
}
}
java程序设计基础篇 复习笔记 第三单元的更多相关文章
- java程序设计基础篇 复习笔记 第七单元&&第八单元
7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...
- java程序设计基础篇 复习笔记 第六单元
第六章 一维数组 1 数组初始化语法 array initializer 2 for each loop 3 off-by-one error 通常是在循环中该使用<的地方使用了<= 4 ...
- java程序设计基础篇 复习笔记 第五单元
1. method header: modifier, return value type, method signature(method name, parameter) method body ...
- java程序设计基础篇 复习笔记 第四单元
1 think before coding code incrementally 2 sentinel value sentinel-controlled loop 3 输入输出重定向 > &l ...
- java程序设计基础篇 复习笔记 第一单元
java语言程序设计基础篇笔记1. 几种有名的语言COBOL:商业应用FORTRAN:数学运算BASIC:易学易用Visual Basic,Delphi:图形用户界面C:汇编语言的强大功能和易学性,可 ...
- java程序设计基础篇 复习笔记 第二单元
1原始数据类型(primitive data type) == 基本类型 (fundamental type)byte short int long float double char boolean ...
- Java程序设计基础作业目录(作业笔记)
持续更新中............. Java程序设计基础笔记 • [目录] 我的大学笔记>>> 第1章 初识Java>>> 1.1.4 学生成绩等级流程图练习 1 ...
- Java程序设计基础笔记 • 【目录】
持续更新中- 我的大学笔记>>> 章节 内容 实践练习 Java程序设计基础作业目录(作业笔记) 第1章 Java程序设计基础笔记 • [第1章 初识Java] 第2章 Java程序 ...
- Java程序设计基础笔记 • 【第7章 Java中的类和对象】
全部章节 >>>> 本章目录 7.1 理解类和对象 7.1.1 对象 7.1.2 抽象与类 7.1.3 类与对象的关系: 7.2 Java中的类和对象 7.2.1 类的定义 ...
随机推荐
- ionic简介
CordovaCordova是贡献给Apache后的开源项目,是从PhoneGap中抽出的核心代码,是驱动PhoneGap的核心引擎.提供了一组设备相关的API,通过这组API,移动应用能够以Java ...
- qml源码查看
已5.4为例说明: QtQuick源码查看: 地址:Qt\Qt5.4.1\5.4\Src\qtdeclarative\src\quick\items Qt control源码查看: 地址:\Qt\Qt ...
- ffmpeg下载安装和简单应用
先介绍一下ffmpeg:FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影.转换.流功能,包含了libavcodec —这是一个用于多个项目中音频和视频的解码器库,以及libavformat ...
- PHP引入框架包
引入包 之后 在写代码的时候会有提示. 流程: 项目名称右击->包含目录->TAB页签选择库-> add external source folder 找到需要的包.
- Spark高级数据分析· 2数据分析
wget https://archive.ics.uci.edu/ml/machine-learning-databases/00210/donation.zip 数据清洗 cd /Users/eri ...
- Koa源码解析
Koa是一款设计优雅的轻量级Node.js框架,它主要提供了一套巧妙的中间件机制与简练的API封装,因此源码阅读起来也十分轻松,不论你从事前端或是后端研发,相信都会有所收获. 目录结构 首先将源码下载 ...
- JAVA-JVM垃圾回收算法
哪些对象可以回收,有两种算法: 1. 引用计数算法,对象被引用计数器加1,对象被释放计数器减1.计数器为0的对象是可以被回收的. 此种方法优点:简单.缺点:会存在互相引用的两个对象,但实际这两个对象都 ...
- pxe基于虚拟机的自启动
环境系统:centos6.4 min版 虚拟机实现:提供的服务器ip为192.168.0.105,桥接 安装dhcp服务: yum -y install dhcp 配置dhcp服务,使能够成功启动: ...
- #ifndef用法
用于避免重复包含头文件 #ifndef _STDIO_H_ #define _STDIO_H_ ...... #endif
- wireshark抓包分析
TCP协议首部: 分析第一个包: 源地址:我自己电脑的IP,就不放上来了 Destination: 222.199.191.33 目的地址 TCP:表明是个TCP协议 Length:66 表明包的长度 ...