JAVA 黑马学习笔记记录 for switch while do...while
- 根据运算符的功能,我们把运算符分成哪几类?
逻辑运算符(与(&) 或(|) 非(!) 短路与(&&有假就假) 短路或(||有真就真) ^异或(相同为假,不同为真)
条件 boolean表达式 ?值1 : 值2
1)计算表达式,得到一个boolean结果
2)如果结果为true,条件运算符结果为值1
3)如果结果为false,条件运算符结果为值2
算数
比较(< > <= >= == != )
赋值(= 唯一一个从右向左运算)
位运算 将十进制转化为二进制再进行逻辑运算, - “&”和“&&”的异同
&是与 类似于和,有假就假,前假后面也要运算
&&短语与,有假就假,前假后面不运算
3.程序输出
class OperatorTest {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
short z = 40;
if ((z++ == 40) && (y = true)) {
z++;
}
if ((x = false) || (++z == 43)) {
z++;
}
System.out.println("z = " + z);
}
}
结果为:z = 44 - 自己定义一个int型三位数的变量并赋值(例如:int a = 123),先将这个变量拆分出个,十,百位,再使用三元运算符将这三个数中的较大数实现。
import java.util.Scanner;
public class chaifen{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入一个百位整数,我将进行拆分”);
int zhengshu = sc.nextInt();
int bai = zhengshu/100;
System.out.println(“百位是” + bai);
int ge = zhengshu%10;
System.out.println(“个位是” + ge);
int shi = zhengshu%100/10;
System.out.println(“十位是” + shi);
int max = 0;
max = ge >= bai ? ge:bai;
max = max >=shi ? max:shi;
System.out.println(max);
}
} - 写出输出结果:
class Demo{
public static void main(String[] args){
int a=3,b=8;
int c=(a>b)?a++:b++;
System.out.println(“a=”+a+“\tb=”+b+“\tc=”+c); // a= 3 b=9 c= 8
int d=(a>b)?++a:++b;
System.out.println(“a=”+a+“\tb=”+b+“\td=”+d); //a=3 b=9 c=9
int e=(a<b)?a++:b++;
System.out.println(“a=”+a+“\tb=”+b+“\te=”+e); //a=4 b=8 c=3
int f=(a<b)?++a:++b;
System.out.println(“a=”+a+“\tb=”+b+“\tf=”+f); //a=4 b=8 c=4
}}
课后练习题
岳小鹏参加Java考试,他和父亲岳不群达成承诺:
如果:
成绩为100分时,奖励一辆BMW;
成绩为(80,99]时,奖励一台iphone xs max;
当成绩为[60,80]时,奖励一个 iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断import java.util.Scanner;
public class bc{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入你的成绩”);
int cj = sc.nextInt();
if (cj == 100) {
System.out.println(“一辆BMW”);
}else if (cj > 80 && cj <= 99) {
System.out.println(“一台苹果手机”);
}else if (cj >= 60 && cj <= 80) {
System.out.println(“iPad”);
}else {
System.out.println(“去吃屁”);
}
}
}编写程序,声明2个int型变量并赋值。判断两数之和,如果大于等
于50,打印“hello world!” 其他情况打印 “hello China!”import java.util.Scanner;
public class bc2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入两个数,我来将其比较”);
int a = sc.nextInt();
int b = sc.nextInt();
if ( a+b >=50) {
System.out.println("hello world ");
}
else {
System.out.println(“hello China”);
}
}
}编写程序,声明2个double型变量并赋值。判断第一个数大于10.0, 且第2个数小于20.0,打印两数之和。否则,打印两数的乘积。
import java.util.Scanner;
public class bc2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入两个数,我来将其比较”);
double a = sc.nextInt();
double b = sc.nextInt();
if ( a > 10.0 & b < 20.0) {
a = a+b;
System.out.println(“和” + a);
}
else {
a = a*b;
System.out.println(“积” + a);
}
}
}编写程序:由键盘输入三个整数分别存入变量num1、num2、num3,
对它们进行排序(使用 if-else if-else),并且从小到大输出。
import java.util.Scanner;
public class 排序{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入三个数,我来将其排序”);
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
if (a>b) {
if( c > a){
System.out.println(b );
System.out.println(a);
System.out.println©;
}
else {
System.out.println(b);
System.out.println©;
System.out.println(a);
}
}
else if( a < b){
if( c > b){
System.out.println(a);
System.out.println(b);
System.out.println©;
}
else {
System.out.println(a);
System.out.println©;
System.out.println(b);
}
}else{System.out.println("输入有误");}
}
}
我家的狗5岁了,5岁的狗相当于人类多大呢?其实,狗的前两年每 一年相当于人类的10.5岁,之后每增加一年就增加四岁。那么5岁的狗 相当于人类多少年龄呢?应该是:10.5 + 10.5 + 4 + 4 + 4 = 33岁。
编写一个程序,获取用户输入的狗的年龄,通过程序显示其相当于人 类的年龄。如果用户输入负数,请显示一个提示信息。
import java.util.Scanner;public class 小狗 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("输入小狗的年龄");double a = sc.nextInt();if(a<=2){a = a * 10.5;System.out.println(a);}else {a = (a-2)*4+21;System.out.println(a);}} }
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出
一定的条件:高:180cm以上;富:财富1千万以上;帅:是。(注意:不用管单位)
如果这三个条件同时满足,则:“我一定要嫁给他!!!”
如果三个条件有为真的情况,则:“嫁吧,将就过日子。”
如果三个条件都不满足,则:“不嫁!”条件需要手动键盘输入,注意它们的数据类型!import java.util.Scanner;
public class 嫁人 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入你的身高cm”);
double a = sc.nextDouble();
System.out.println(“输入你的存款W”);
double b = sc.nextDouble();
System.out.println(“你帅吗?帅1,不帅2”);
double c = sc.nextDouble();
if(a>=1.8 && b >= 1000 && c == 1){
System.out.println(“一定嫁”);
}
else if(a>=1.8 || b >= 1000 || c == 1){
System.out.println(“嫁 很好过日子”);
}
else{
System.out.println(“不嫁,除非相信爱情”);
}
}
}
知识点
switch判断语句
理论题1
- switch后面使用的表达式可以是哪些数据类型的。
byte 1 short 2 int 4 char 2 JDK5以后可以是枚举,JDK7以后可以是String
- 使用switch语句改写下列if语句:
int a = 3;int x = 100;if(a==1) x+=5;else if(a==2)x+=10;else if(a==3)x+=16;else x+=34;
public class 改写 {
public static void main(String[] args) {
int a = 3;
int x = 100;
switch (a) {
case 1:
x += 5;
break;
case 2:
x += 10;
break;
case 3:
x += 16;
break;
default:
x+=34;
break;
}
System.out.println(x);
}
}
谈谈你对三元运算符、if-else和switch-case结构使用场景的理解
三元运算符:用在两个条件判断选择一个
if-else:条件判断比较少的时候4个以内吧
switch-case:多个变量一个输出结果,条件很多如何从控制台获取String和int型的变量,并输出?使用代码实现!
import java.util.Scanner;
public classs aa{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt;
String b =sc.next();System.out.println(a);
System.out.println(b);}
}
题目2(综合)
模拟计算器功能,对键盘录入的两个int类型的数据进行加、减、乘、除的运算,并打印运算结果。
要求:
键盘录入三个整数,其中前两个整数代表参加运算的数据,第三个整数为要进行的运算(1:表示加法运算,2:表示减法运算,3:表示乘法运算,4:表示除法运算),演示效果如下:
请输入第一个整数: 30
请输入第二个整数: 40
请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法): 1
控制台输出:30+40=70
训练提示
- 用户录入了数据之后,用什么知识点去判断加减乘除四种不同的操作?
解题方案
- 使用switch判断语句完成。
操作步骤
- 使用键盘录入三个变量。
- 使用switch判断语句对第三个变量进行判断,匹配要执行的操作。
- 在每一个case中分别对第一个变量和第二个变量进行不同的操作。
import java.util.Scanner;
public class 计算器 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入两个整数我将进行运算”);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(“请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法”);
int c = sc.nextInt();
switch ©{
case 1:
c = a+b;
System.out.println©;
break;
case 2:
c = a-b;
System.out.println©;
break;
case 3:
c = a*b;
System.out.println©;
break;
case 4:
c = a/b;
System.out.println©;
break;
default:
System.out.println(“请输入正确的运算规则”);
}
}
}
知识点
switch判断语句
理论题
编写程序:
从键盘上输入2019年的“month”和“day”,要求通过程序 输出输入的日期为2019年的第几天。
import java.util.Scanner;
public class 计算第多少天 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入2019年的“month”和“day” 我将算出其为第几天”);
System.out.println(“月”);
int month = sc.nextInt();
System.out.println(“日”);
int day = sc.nextInt();
int i = 0;
int a = 0;
/*
2019年农历每月多少天
“2019年农历1、3、5、8、11、12是大建每月30天,2、4、6、7、9、10是小建每月29天,共354天。” 更多 >
/
switch(month){
case 12:
i = i+1;
case 11:
i = i+1;
a = a+2;
case 8:
i = i+1;
a = a+2;
case 5:
i = i+1;
a = a+1;
case 3:
i = i+1;
a = a+1;
case 1:
month = i30 +a29 + day;
System.out.println(“2019年的第” + month + “天”);
break;
case 10:
a = a+1;
case 9:
i = i+1;
a = a+1;
case 7:
a = a+1;
case 6:
i = i+1;
a = a+1;
case 4:
i = i+1;
a = a+1;
case 2:
i = i+1;
month = i30 +a*29 + day;
System.out.println(“2019年的第” + month + “天”);
break;
default:
System.out.println(“请输入正确的月份”);
}}
}
对于题1的升级版: 从键盘分别输入年、月、日,判断这一天是当年的第几天
注:判断一年是否是闰年的标准: 1)可以被4整除,但不可被100整除 或 2)可以被400整除
import java.util.Scanner;
public class 判断闰年 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“输入年份”);
int yer = sc.nextInt();
System.out.println(“输入2019年的“month”和“day” 我将算出其为第几天”);
System.out.println(“月”);
int month = sc.nextInt();
System.out.println(“日”);
int day = sc.nextInt();
int i = 0;
int a = 0;
/*
2019年农历每月多少天
“2019年农历1、3、5、8、11、12是大建每月30天,2、4、6、7、9、10是小建每月29天,共354天。” 更多 >
*/
switch(month){
case 12:
i = i+1;
case 11:
i = i+1;
a = a+2;
case 8:
i = i+1;
a = a+2;
case 5:
i = i+1;
a = a+1;
case 3:
i = i+1;
a = a+1;
case 1:
month = i*30 +a*29 + day;System.out.println("2019年农历的第" + month + "天");break;case 10:a = a+1;case 9:i = i+1;a = a+1;case 7:a = a+1;case 6:i = i+1;a = a+1;case 4:i = i+1;a = a+1;case 2:i = i+1;month = i*30 +a*29 + day;System.out.println("2019年农历的第" + month + "天");break;default:System.out.println("请输入正确的月份");}
if(yer%4 == 0){System.out.println("是闰年");
}
else{System.out.println("平年");
}}
}
- 编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期, 每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、sheep、monkey、 rooster、dog、pig。 提示:2019年:猪 2019 % 12 == 3
import java.util.Scanner;
public class 生肖判断 {
public static void main(String[] srgs){
Scanner sc = new Scanner(System.in);
System.out.println(“输入年份我判断你属于什么生肖”);
int year = sc.nextInt();
year = year%12;
switch (year){
case 1:
System.out.println(“鸡”);
break;
case 2:
System.out.println(“狗”);
break;
case 3:
System.out.println(“猪”);
break;
case 4:
System.out.println(“鼠”);
break;
case 5:
System.out.println(“牛”);
break;
case 6:
System.out.println(“虎”);
break;
case 7:
System.out.println(“兔”);
break;
case 8:
System.out.println(“龙”);
break;
case 9:
System.out.println(“蛇”);
break;
case 10:
System.out.println(“马”);
break;
case 11:
System.out.println(“羊”);
break;
case 0:
System.out.println(“猴”);
break;
}
}
}
知识点
for循环语句
理论操作题
What is the result when you compile and run the following code?
编译和运行以下代码的结果是什么?
C
没有定义ipublic class Test{
public void method(){
for(int i = 0; i < 3; i++) {
System.out.print(i);
}
System.out.print(i);
}
}
A. 0122
B. 0123
C. compile error
编译错误D. none of these
答案:?C
编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行 上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印 输出“baz” 。
import java.util.Scanner;
public class 循环打印357 {
public static void main(String[] args){
//Scanner sc = new Scanner(System.in);
for(int i=1; i<=150;i++){
System.out.print(i);
if(i%30){
System.out.print("foo ");
}
if(i%50){
System.out.print("biz ");
}
if(i%7==0){
System.out.print("baz ");
}
else {
System.out.print(‘\n’);
}
}
}
}
- 打印1~100之间所有奇数的和 。
public class 奇数之和 {
public static void main(String[] args){
int a = 0;
for(int i=1; i<=100;i++){
if(i%2==1){
a += i;
}
}
System.out.println(a);
}
}
- 打印1~100之间所有是7的倍数的整数的个数及总和(体会设置计数 器的思想) 。
public class 奇数之和 {
public static void main(String[] args){
int a=0;
int b =0;
for(int i=1;i<=100;i++){
if(i%7==0){
a += 1;
b+=i;
}
}
System.out.println(“数量有” + a);
System.out.println(“之和为” + b);
}
}
下面是一段程序,目的是输出10个=,但是不小心代码写错了,现在需要修改代码,使得程序完成功能,但是只能“增加”或“修改”其中“一个”字符,很明显,将i–改为i++,可以完成功能,但是需要修改“两个”字符,所以并不是一个正确的答案?
public static void main(String[] args) {int n=10;for (int i = 0; i < n; i--) {System.out.println("=");}}
i = 0; 改为 i = 10;
3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数。
public class 绳子 {
public static void main(String[] args){
int a=0;
for(int i=3000;i>=5;i–){
if(i>=5){
i =(int) i/2;
a +=1;
}
}
System.out.println(“一次少一半” + a + “次后就少于5M”);
}
}
- 输出从1到100之间所有不能被3整除的数;并输出这些整数的和
public class 不被3整除数之和 {
public static void main(String[] args){
int a= 0;
for(int i=1;i<=100;i++){
if(i%3!=0){
a+=i;
}
}
System.out.println(a);
}
}
知识点
while语句,do_While语句
用while循环语句实现,打印出20个3的倍数的和
public class while语句 {
public static void main(String[] args){
int a=3;
int b=1;
int c;
do {
c = a*b;
b++;
}
while (b<=20);
System.out.println©;
}
}用do_while循环语句实现,打印出3的倍数,如果这个数是7的倍数则结束程序。
public class while语句2 {
public static void main(String[] args){
int a=3;
int b=1;
int c=1;
do {
c = a*b;
b++;
}
while (c%7 !=0);
System.out.println©;}
}用while循环语句实现,打印出0-100,不包括7或者3的倍数
public class while语100不含7和3 {
public static void main(String[] args){
int i=0;
while(i<=100){
if(i%3!=0&&i%7!=0 )
System.out.println(i);
i++;
}
}
}使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,即每隔20摄氏度需要对华氏温度进行计算,从0摄氏度开始,并且对照表中的条目不超过10条。
转换关系:华氏温度= 摄氏温度*9 / 5.0 + 32
public class while语句温度转换 {
public static void main(String[] args){
double a=0;
double b=1;
do{
if(a%20==0){
b = a*9/5.0+32;
System.out.println(b);
}
a++;
}while(a<=250);
}
}
综合练习
循环,嵌套循环
循环结构是如何最后退出循环的,有哪些不同的情况请说明。
条件语句不满足
循环体内执行到break。
continue 指定位置;指出如下程序输出的结果:
label: for (int i = 1; i <= 4; i++) {for (int j = 1; j <= 10; j++) {if (j % 4 == 0) {continue label;}System.out.print(j);}System.out.println(); }
1 2 3 1 2 3 1 2 3 1 2 3
一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3。编程 找出1000以内的所有完数。(因子:除去这个数本身的其它约数)
public class 完数 {
public static void main(String [] args) {
for(int i = 1;i<=1000;i++) {
int num = 0;
for(int j = 1;j<i;j++) {
if(i%j == 0) {
num += j;
}
}
if(i == num) {
System.out.println(i+“是完数”);
}
}}
}说明break和continue使用上的相同点和不同点
break 是直接结束循环,continue是结束循环且到continue后面跟着的指定位置;输入高度,输出直角三角形。如:输入5, 将输出如下图形
# ## ### #### #####
import java.util.Scanner;
public class 直角三角形 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“你想要直角三角形有多高直接输入数字”);
int a = sc.nextInt();
for(int b=1;b<=a;++b){//输出其高
for(int d=b; d>0;d–){
System.out.print(‘#’);
}
System.out.println( );
}
}
}
输入高度,输出倒直角三角形。如:输入6, 将输出如下图形
****** ***** **** *** ** *
import java.util.Scanner;
public class 直角三角形 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“你想要倒直角三角形有多高直接输入数字”);
int a = sc.nextInt();
for(int b=a;b>0;b–){//输出其高
for(int d=b; d>0;d–){
System.out.print(‘#’);
}
System.out.println( );
}
}
}
执行如下代码后,i的值是多少?
public static void main(String[] args) {int i=10;while(i>0){i = i +1;if(i==10){break;}}System.out.println("i=" + i);}
i= 11
打印1-100之间非13的倍数,使用continue语句
public class while语句 {
public static void main(String[] args){
label: for (int i = 1; i <= 100; i++) {
if(i%13!=0){
System.out.println(i);
continue label;
}
}
}
}打印自然数的个数
1)打印1~100之间 6的倍数的个数
public class while语句 {
public static void main(String[] args){
int a=0;
label: for (int i = 1; i <= 100; i++) {
if(i%6==0){
a =a+1;
continue label;
}
}
System.out.println(a);
}
}2)求出1~100之间,既是3又是7的倍数的自然数出现的次数?
public class while语句 {
public static void main(String[] args){
int a=0;
label: for (int i = 1; i <= 100; i++) {
if(i%30&&i%70){
a =a+1;
continue label;
}
}
System.out.println(a);
}
}打印如下的图形:菱形1
* * * * * * * * * * * * * * * * * * * * * * * * *
public class 菱形 {
public static void main(String[] args){
for (int a = 0; a < 5; a++){
for (int b= (5 - 1); b > a; b–){
System.out.print(" “);
}
for (int b1 = 0; b1 < (a + 1); b1++){
System.out.print(”* “);
}
System.out.println();
}
for (int a1 = 5; a1 > 0; a1–){
for (int b3 = (5 + 2); b3 > (a1 + 1); b3–){
System.out.print(” “);
}
for (int b4 = 0; b4 < (a1 - 1); b4++){
System.out.print(”* ");
}
System.out.println();
}}
}
JAVA 黑马学习笔记记录 for switch while do...while相关推荐
- 黑马程序员Java教程学习笔记(五)
学习视频:https://www.bilibili.com/video/BV1Cv411372m 如侵权,请私信联系本人删除 文章目录 黑马程序员Java教程学习笔记(五) 日期时间:Date.Sim ...
- 黑马程序员Java教程学习笔记(三)
学习视频:https://www.bilibili.com/video/BV1Cv411372m 如侵权,请私信联系本人删除 文章目录 黑马程序员Java教程学习笔记(三) 面向对象:设计对象.注意事 ...
- 尚学堂JAVA高级学习笔记_1/2
尚学堂JAVA高级学习笔记 文章目录 尚学堂JAVA高级学习笔记 写在前面 第1章 手写webserver 1. 灵魂反射 2. 高效解析xml 3. 解析webxml 4. 反射webxml 5. ...
- 可能是最全面的 Java G1学习笔记
转载自 可能是最全面的 Java G1学习笔记 引子 最近遇到很多朋友过来咨询G1调优的问题,我自己去年有专门学过一次G1,但是当时只是看了个皮毛,因此自己也有不少问题.总体来讲,对于G1我有几个疑惑 ...
- 深入浅出 Java CMS 学习笔记
转载自 深入浅出 Java CMS 学习笔记 引子 带着问题去学习一个东西,才会有目标感,我先把一直以来自己对CMS的一些疑惑罗列了下,希望这篇学习笔记能解决掉这些疑惑,希望也能对你有所帮助. 1. ...
- Java NIO学习笔记之图解ByteBuffer
转载自 Java NIO学习笔记之图解ByteBuffer ByteBuffer前前后后看过好几次了,实际使用也用了一些,总觉得条理不够清晰. <程序员的思维修炼>一本书讲过,主动学习,要 ...
- 转载:mongoDB java驱动学习笔记
http://www.blogjava.net/watchzerg/archive/2012/09/22/388346.html mongoDB java驱动学习笔记 指定新mongo实例: Mong ...
- 尚学堂JAVA基础学习笔记_2/2
尚学堂JAVA基础学习笔记_2/2 文章目录 尚学堂JAVA基础学习笔记_2/2 写在前面 第10章 IO技术 1. IO入门 2. IO的API 3. 装饰流 4. IO实战 5. CommonsI ...
- JAVA视频学习笔记-马士兵(六)
`JAVA视频学习笔记-马士兵` 常用类_字符串相关类(20200614~16) 常用类_字符串相关类(20200617) 常用类_基本数据类型包装类(20200618) 常用类_Math类(2020 ...
最新文章
- Linux下常用的命令
- 观点 | 商汤科技联合创始人林达华:深度学习遭遇瓶颈,未来之路需要新的思考
- WebConfig Authorization 节点
- 用jsp开发web应用并不是一个高效率的选择
- 听商业领袖揭秘大数据新动态
- 详解tomcat的连接数与线程池--转载
- 内存数据库 TimesTen
- CAN FD安全通信
- windows安装ffmpeg,yasm,opencv
- 启发函数heuristic 与 A*
- Java期末考试题(附答案)
- 小麦苗健康检查脚本说明
- mac 无法打开22端口 无法远程连接ssh 的解决办法
- Android如何制作.9图片
- 学java有前途吗?方兴未艾!
- H5C3动画实例,通过基于jQuery的fullpage插件完成
- signature=2abb9e363faa3aa7323b2a3393a36011,Winhex软件的使用
- DDR3 基础知识分享
- 拱拱Lite开发(3):三翼页及湘大文库下载实现(解析网页获取信息及模拟登陆)
- 计算机网络原理自考真题,自考计算机网络原理真题附标准标准答案.doc