2017年3月6号课堂笔记

2017年3月6号 晴 大风

备注: 这两天感冒了,所以7号才写完发表

主要内容:For循环、break、continue

一、do-while老师上周留题目(摄氏度华氏度对照表)

1、老师代码:

public class WhileDemo08 {

/**
* 使用do-while实现:输出摄氏温度与华氏温度的对照表,
* 要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32

循环操作:计算摄氏温度,并输出对照条目
循环条件:
条目<=10 && 摄氏温度 <= 250
*/
public static void main(String[] args) {
int count = 1; // 对照条目
double c = 0; // 摄氏度
double f = 0; // 华氏度
System.out.println("条目 摄氏度 华氏度");
do {
// 转换
f = c * 9 / 5.0 + 32;
System.out.println(count + " " + c + " " + f);
count++;
c += 20;
} while (count <= 10 && c <= 250);

}

}

2、自己代码:

package cn.bdqn.test;

/**

*<P>Title<P>DoWhile02
*<P>Description<P>
*使用do-while实现:
*输出摄氏温度与华氏温度的对照表;
*要求它从摄氏温度0度到250度,每隔20度为一项;
*对照表中的条目不超过10条;
*转换关系:华氏温度=摄氏温度*9/5.0+32;

*提示:循环操作:计算摄氏温度,并输出对照条目
*循环条件:
*条目<=10&&摄氏温度<=250
*
* @author alex
* @date 2017-3-3下午4:35:59
* You can either travel or read,but either your body or soul must be on the way
*/
public class DoWhile02 {
public static void main(String[] args) {
// 声明摄氏温度
double celsiusTemperature = 0.0;
// 声明华氏温度
double fahrenheit = 0.0;
// 声明摄氏温度的条目
int i = 1;
// 根据摄氏温度计算华氏温度并输出
System.out.println("******摄氏温度与华氏温度对照表******");
do {
fahrenheit = celsiusTemperature * 9 / 5.0 + 32;
System.out.println("-------------------------------------");
System.out.println("摄氏温度:" + celsiusTemperature + " " + "华氏温度:"
+ fahrenheit);
i++;
celsiusTemperature += 20;
} while (i <= 10 && celsiusTemperature <= 250);

}

}

二、WhileDemo09(数字的反转)

重点关注,上课时候此题没想出来思路!!

1、老师代码:

public class WhileDemo09 {

/**
* 实现一个数字的反转
*/
public static void main(String[] args) {
int num = 123456789;
int temp = 1;
System.out.print("反转之后的数字:");
while (num != 0) {
// 依次与10取余
temp = num % 10; // 6
System.out.print(temp);
num = num / 10;
}
}

}

2、自己代码:

package cn.bdqn.test;

/**
*
*<P>Title<P>While09
*<P>Description<P>
*实现一个数字的反转:123456----->654321
* @author alex
* @date 2017-3-6上午8:50:09
* You can either travel or read,but either your body or soul must be on the way
*/
public class While09 {
public static void main(String[] args) {
int num = 123456;
int temp = 1;
System.out.println("反转之后的数字:");
// 依次与10取余
while (num != 0) {
temp = num % 10;// 6
System.out.print(temp);
num = num / 10;
}
}
}

三、ForDemo01(输出100次好好学习)

 1、老师代码:

public class ForDemo01 {

public static void main(String[] args) {
/**
*
* for(表达式1;表达式2;表达式3) {
* 循环体
* }
* 表达式1:初始化变量 int a=0;
* 表达式2:循环条件!满足条件执行循环体操作!
* 表达式3:迭代变量!
*
* 三个表达式都可以省略!但是;不能省略!
*
*
* 执行顺序:
* 表达式1---》表达式2---》循环体---》表达式3--->表达式2---》循环体---》表达式3
*/
for (int i = 1; i <= 100; i++) {
System.out.println("好好学习!" + i);
}
}
}

四、ForDemo02(循环输入某同学考试的5门课成绩,并计算平均分)

 1、老师代码:

public class ForDemo02 {
/**
* 循环输入某同学S1结业考试的5门课成绩,并计算平均分
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.println("请您输入姓名:");
// 定义一个变量 来保存总成绩
double sum = 0;
String name = scanner.next();
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入第" + i + "门课程的成绩:");
double score = scanner.nextDouble();
sum += score; // 总成绩
}
System.out.println("平均分是:" + (sum / 5));

}

}

 2、自己代码:

package cn.bdqn.test;

/**
* 循环输入某同学S1结业考试的5门课成绩,
* 并计算平均分
*/
import java.util.Scanner;

public class ForDemo01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入学生姓名:");
String name = input.next();
double sum = 0.0;
for (int i = 1; i <= 5; i++) {
System.out.println("输入5门功课中第" + i + "门课的成绩:");
double score = input.nextDouble();
sum += score;
}
System.out.println(name + "同学5门功课的平均分是:" + (sum / 5));
}
}

五、ForDemo03

 1、老师代码:(根据用户的输入 产生对应的加法表)

public class ForDemo03 {
public static void main(String[] args) {
// 根据用户的输入 产生对应的加法表
Scanner scanner = new Scanner(System.in);
System.out.println("请您输入一个数字:");
int num = scanner.nextInt();
for (int i = 0; i <= num; i++) {
System.out.println(i + "+" + (num - i) + "=" + (num));
}

}

}

2、自己代码:

package cn.bdqn.test;

/**
*
*<P>Title<P>ForDemo02
*<P>Description<P>
*加法表
*0+6=6
*1+5=5
*2+4=6
*3+3=6
*4+2=6
*5+1=6
*6+0=0
* @author alex
* @date 2017-3-6上午9:23:15
* You can either travel or read,but either your body or soul must be on the way
*/
import java.util.Scanner;

public class ForDemo02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数值:");
int num = input.nextInt();
for (int i = 0; i <= num; i++) {
System.out.println(i + "+" + (num - i) + "=" + num);
}
}
}

六、ErrorDemo04(关于for的几种有问题的写法)

 1、老师代码:

public class ErrorDemo04 {
public static void main(String[] args) {
/**
* int i = 0; // 把初始化变量 局部化!
for (; i <= 5; i++) {
System.out.println(i);

}
*/
/**for (;;) { // 死循环
System.out.println(1);
}*/

/**for (int i = 0;; i++) { // 省略了 条件判断
System.out.println(i);
}
*/

for (int i = 0; i < 10;) { // 省略了 条件判断迭代变量
System.out.println(i);
}

}
}

七、ForDemo05(1~100之间不能被3整除的数之和)

 1、老师代码:

public class ForDemo05 {
public static void main(String[] args) {
int sum = 0;
// 求1~100之间不能被3整除的数之和
for (int i = 1; i <= 100; i++) {
// 不能被3整除的数
if (i % 3 != 0) {
sum += i;
}
}
System.out.println("不能被3整除的数之和:" + sum);
}

}

 2、自己代码:

package cn.bdqn.test;

/**
*
*<P>Title<P>ForDemo03
*<P>Description<P>
*求1~100之间不能被3整除的数之和
* @author alex
* @date 2017-3-6下午4:14:04
* You can either travel or read,but either your body or soul must be on the way
*/
public class ForDemo03 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 3 != 0) {
sum += i;
}
}
System.out.println("1~100之间不能被3整除的数之和为:" + sum);
}
}

八、ForDemo06(计算年龄大于30岁(含30岁)的顾客占比)

 1、老师代码:

public class ForDemo06 {
// 计算年龄大于30的 占比
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 定义年龄的变量
int age = 0;
// 年龄大于30以上的人数
int count = 0;
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入年龄:");
age = scanner.nextInt();
if (age >= 30) {
count++;
}
}
System.out.println("30岁以上的比例是:" + (count / 5.0 * 100) + "%");
System.out.println("30岁以下的比例是:" + ((1 - count / 5.0) * 100) + "%");
}

}

 2、自己代码:

package cn.bdqn.test;

// 计算年龄大于30岁(含30岁)的顾客占比
import java.util.Scanner;

public class ForDemo05 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = 0;// 年龄30以上

for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "位顾客的年龄:");
int age = input.nextInt();
if (age >= 30) {
num++;
}
}
System.out.println("30岁以上的比例是:" + (num / 5.0 * 100) + "%");
// 重难点:num直接/5,得到结果为0;想得到小数,需要/5.0;
}
}

九、ForDemo07(保留有效数字的两种方法)

 本节课重难点之一,掌握其中一种先!

 1、老师代码:

public class ForDemo07 {
public static void main(String[] args) {

System.out.println("请您输入一个数字:");
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();

DecimalFormat df = new DecimalFormat("0.00"); // 保留两位有效数字
String number = df.format(num); // 把double类型的数据 保留两位有效数字 返回String
System.out.println("保留两位有效数字:" + number);

System.out.println("*********************************");

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2); // 保留两位有效数字
number = nf.format(num);
System.out.println("保留两位有效数字:" + number);

}

}

 2、自己代码:

package cn.bdqn.test;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class ForDemo06 {
public static void main(String[] args) {

System.out.println("请您输入一个数字:");
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble();

DecimalFormat df = new DecimalFormat("0.00"); // 保留两位有效数字
String number = df.format(num); // 把double类型的数据 保留两位有效数字 返回String
System.out.println("保留两位有效数字:" + number);

System.out.println("*********************************");

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3); // 保留(i)位有效数字,i=几,保留几位有效数字;
number = nf.format(num);
System.out.println("保留两位有效数字:" + number);
}
}

十、ForDemo08(日期的转换)

 重难点:老师要求掌握!!!据说伴随java编程终身~

 1、老师代码:

public class ForDemo08 {
/**
* 日期的转换
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {

Date date = new Date(); // 日期 格式 Mon Mar 06 11:02:55 CST 2017
System.out.println("日期 格式:" + date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日 hh:mm:ss");
String time = sdf.format(date); // 把日期格式 转换成 String
System.out.println(time);
time = "2018年-05月-05日 11:11:11";
// 把 String 转换成 日期格式
System.out.println(sdf.parse(time));
}

}

 2、自己代码:

package cn.bdqn.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ForDemo07 {
/**
* 日期的转换
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// 抛出有可能出现的异常

Date date = new Date(); // 日期 格式 Mon Mar 06 11:02:55 CST 2017
System.out.println("日期格式:" + date);// 默认输出当前时间

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日 hh:mm:ss");
// 把日期格式 转换成 String
String time = sdf.format(date);
System.out.println(time);
// 把 String 转换成 日期格式
time = "2018年-05月-05日 11:11:11";
System.out.println(sdf.parse(time));
}
}

十一、ForDemo09(跳出当前循环结构,跑步中途第八圈坚持不住退出) 

 1、老师代码:

public class ForDemo09 {
//break 跳出当前循环结构
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "圈");
if (i == 8) {
System.out.println("坚持不住!!!");
break;
}
}
System.out.println("比赛结束!");
}
}

2、自己代码:

package cn.bdqn.test;

/**
*
*<P>Title<P>ForDemo08
*<P>Description<P>
*跳出当前循环结构
*需求:4000米跑步(10圈),
*第8圈坚持不住退出
* @author alex
* @date 2017-3-7上午9:59:38
* You can either travel or read,but either your body or soul must be on the way
*/

public class ForDemo08 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("跑第" + i + "圈");
if (i == 8) {
System.out.println("身体状况不佳,坚持不住了");
break;
}
}
System.out.println("中途退出,长跑比赛提前结束");

}
}

十二、ForDemo10(循环录入成绩并计算平均分,负分出现退出)

 1、老师代码:

flag的使用(思想很重要)

public class ForDemo10 {
/**
* 循环录入某学生5门课的成绩并计算平均分。
* 如果某分数录入为负,停止录入并提示录入错误
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 用来保存总成绩
double sum = 0;
// 标记 是否计算平均分
boolean flag = true;
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入第" + i + "门课程的成绩:");
double score = scanner.nextDouble();
if (score < 0) { // 分数为负数
System.out.println("您的输入有误!退出系统!");
flag = false;
break;
}
sum += score;
}
if (flag) { // flag==true
System.out.println("平均分是:" + (sum / 5));
}

}
}

 2、自己代码:

package cn.bdqn.test;

import java.util.Scanner;

/**
*
*<P>Title<P>ForDemo09
*<P>Description<P>
* 循环录入某学生5门课的成绩并计算平均分。
* 如果某分数录入为负,停止录入并提示录入错误
* @author alex
* @date 2017-3-7下午3:22:53
* You can either travel or read,but either your body or soul must be on the way
*/
public class ForDemo09 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = input.next();
double sum = 0;// 记录总成绩

for (int i = 1; i <= 5; i++) {
System.out.println("请输入第" + i + "门成绩:");
double score = input.nextDouble();
if (score < 0) {
System.out.println("您输入的分数为负数,录入错误,系统自动退出");
return;
}
sum += score;
}
System.out.println(name + "同学5门课的平均分为:" + (sum / 5) + "分");

}

}

十三、ForDemo11(1~10之间的整数相加,得到累加值大于20的当前数)

 1、老师代码:

 备注:老师最后计算的是累加值

public class ForDemo11 {

/**
*1~10之间的整数相加,得到累加值大于20的当前数
*/
public static void main(String[] args) {

// 定义变量保存 累加值
int sum = 0;
for (int i = 0; i <= 10; i++) {
sum += i;
if (sum > 20) {
break; // 跳出当前循环结构!
}
}
System.out.println("当前的sum===:" + sum);

}

}

 2、自己代码:

package cn.bdqn.test;

//1~10之间的整数相加,
//得到累加值大于20的当前数
public class ForDemo10 {
public static void main(String[] args) {
int sum = 0;// 记录累加值
int i = 1;
for (i = 1; i <= 10; i++) {
sum += i;
if (sum > 20) {
break;
}
}
System.out.println("累加值大于20的当前数为:" + i);
}
}

十四、ForDemo12(统计分数大于等于80分的学生比例)

 1、老师代码:

public class ForDemo12 {

/**
*循环录入Java课的学生成绩,
*统计分数大于等于80分的学生比例
*
*continue:代表的是:结束本次循环!继续下次的循环! 之后的代码不会执行!
*跳出循环体了吗??没有跳出!代表 循环继续!
*
*break: 跳出当前整个循环体!
*
*return: 跳出当前方法,可以带有返回值!
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 定义变量 保存分数大于80的人数
int sum = 0;
System.out.println("请输入班级的人数:");
int count = scanner.nextInt();
for (int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "名同学的成绩:");
double score = scanner.nextDouble();
if (score < 80) {
continue;
}
sum++;
return;
}
System.out.println("80分以上的人数:" + sum);
System.out.println("80分以上的人数占比:" + (sum * 1.0 / count) * 100 + "%");
}
}

 2、自己代码:

package cn.bdqn.test;

/**
*循环录入Java课的学生成绩,
*统计分数大于等于80分的学生比例
*/
import java.util.Scanner;

public class ForDemo11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入班级总人数:");
int num = input.nextInt();
int count = 0;// 记录80分以上人数
for (int i = 1; i <= num; i++) {
System.out.println("请输入第" + i + "位学生的成绩:");
double score = input.nextDouble();
if (score < 80) {
continue;
}
count++;
}
System.out.println("80分以上学生比例是:" + (count * 1.0 / num * 100) + "%");
}
}

十五、ForDemo13(a+=b a=a+b 是一致的吗)

 1、老师代码:

// a+=b a=a+b 是一致的吗?
int a = 5;
double b = 5;
// a =a + b;
a += b; // 做了强制类型转换 (int) (a + b) 不会编译报错
System.out.println(a);

a+=b与 a=a+b

十六、课堂练习Demo(用while,do-while,for分别计算100以内奇数之和;)

 1、自己代码:

package cn.bdqn.test;

//用while,do-while,for分别计算100以内奇数之和;
public class ForDemo04 {
public static void main(String[] args) {
// while
int i1 = 0;
int sum1 = 0;
while (i1 <= 100) {
if (i1 % 2 != 0) {
sum1 += i1;
}
i1++;
}
System.out.println("while版本100以内奇数之和:" + sum1);

// do-while
int i2 = 0;
int sum2 = 0;
do {
if (i2 % 2 != 0) {
sum2 += i2;
}
i2++;
} while (i2 <= 100);
System.out.println("do-while版本100以内奇数之和:" + sum2);
// for
int sum3 = 0;
for (int i3 = 0; i3 <= 100; i3++) {
if (i3 % 2 != 0) {
sum3 += i3;
}
}
System.out.println("for版本100以内奇数之和:" + sum3);
}
}

 2、老师代码(三种循环 来实现 100以内的偶数和):

// 三种循环 来实现 100以内的偶数和
public static void main(String[] args) {
// 01.使用while循环
int num = 0;
int sum = 0; // 求和
while (num <= 100) {
// 找到偶数并相加
if (num % 2 == 0) {
sum += num; // sum=sum+num??
}
num++;
}
System.out.println("while偶数和是:" + sum);

// 02.使用do while
sum = 0;
num = 0;
do {
if (num % 2 == 0) { // 找到偶数
sum += num;
}
num++;
} while (num <= 100);
System.out.println("dowhile偶数和是:" + sum);

// 03.使用for
sum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println("for偶数和是:" + sum);

}

十七、作业

1、复习消化本节课内容

2、用户登录验证

3、视频看到封装!

4、保持每天的做题数目

十八、考试

2017.03.06


15:06开始,15:49结束;答题时间:43分钟;检查时间:0分钟;
感冒了太难受就没有检查直接交卷了!感冒了答题状态也迟钝! o(╯□╰)o


成绩:90分(三道题是真心模糊的,其中一道至今没懂,小苏和小高说是静态的无法调用非静态的)

十九、老师辛苦了!

 

原文地址:https://www.cnblogs.com/wsnedved2017/p/6509600.html