javaSE基础知识——day2逻辑运算符,if/switch/for/while/do while语句及相应的水仙花数,九九乘法表,菱形,三角形等练习题面试题
2019-06-27 17:47
399 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lalaMarry/article/details/93893803
01运算符
- +%-*
02逻辑运算符
-
&(并且) , |(或者) , !(非) , ^(异或) , &&(短路与) , ||(短路或)
-
&&和&的区别?
a:最终结果一样。 b:&&具有短路效果。左边是false,右边不执行
- 开发常用
&&==and:一假必假 ||==or:一真必真 !:取反
- 位异或运算符的特点及面试题
A:案例演示 位异或运算符的特点 ^的特点:一个数据被另一个数据位异或两次,该数本身不变。 自己位异或自己是0 8^8 结果是0 B:面试题: 请自己实现两个整数变量的交换 注意:以后讲课的过程中,我没有明确指定数据的类型,默认int类型。
- 位运算符的基本用法2及面试题
A:案例演示 <<:左移 空位补0,被移除的高位丢弃。 >>:右移 被移位的二进制最高位是0,右移后,空缺位补0;最高位是1,高位补1。 >>>:无符号右移 被移位二进制最高位无论是0或者是1,空缺位都用0补。 B:结论: << : 把<<左边的数据乘以2的移动次幂 例如 10<<3 结果 10*2^3=80 >> : 把>>左边的数据除以2的移动次幂 例如 40>>3 结果 40/2^3=5
03 Scanner键盘录入数据
- 键盘输入一个数字并打印出来
import java.util.Scanner;//导入程序包,位置在class上面 class Demo01{ public static void main(String[] args){ Scanner sc=new Scanner(System.in);//创建键盘录入对象 /sc为对象名称可自命名 System.out.println("请输入一个数字:"); int num=sc.nextInt();//调用该对象录入一个整数,也可以是其他数据类型 .nextInt();前应是自命名的对象名称。 System.out.println("the number"+num+"is inputed by you"); } }
04Java语言基础(顺序结构语句)(掌握)
流程控制语句的分类 顺序结构:没有特定的语法,就是按照代码的先后顺序,依次执行 选择结构 循环结构
选择结构if语句使用(掌握)
- 选择结构的分类
if语句
switch语句() - if语句的使用场景:1.针对结果是boolean进行判断2.判断的是一个范围3.判断的是几个常量值
- if语句的格式1案例演示
- 判断两个数据是否相等
class Demo2{ public static void main(String[] args){ int a=100; int b=10; if(!(a==b)){//==判断两个数是否相等 System.out.println("a和b是不相等的");//比较表达式结果为true执行该语句体 } } } 运行结果:a和b是不相等的
- if语句的格式2案例演示
- 获取两个数据中较大的值
class Demo03{ public static void main(String[] args){ int a=100; int b=10; if(a>b){//比较两个数字,为真执行输出1 System.out.println("a大于b");//1 }else{//否则为假,输出2 System.out.println("a不大于b");//2 } } } 运行结果:a大于b
- 判断一个数据是奇数还是偶数,并输出是奇数还是偶数
import java.util.Scanner; public class IfOdevity { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入一个数字"); int num = sc.nextInt(); if (num%2==0){ System.out.println(num+"为偶数"); }else { System.out.println(num+"为奇数"); } } }
- if语句的格式3:
- 键盘录入一个成绩,判断并输出成绩的等级。
import java.util.Scanner; public class IfScore { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//键盘录入 System.out.println("请输入你的成绩: "); int score = sc.nextInt(); if (score>=0 && score<60){ System.out.println("差");//符合条件输出1 }else if (score>=60 && score<70){ System.out.println("及格");//符合条件输出1 }else if (score>=70 && score<80){ System.out.println("中");//符合条件输出1 }else if (score>=80 && score<90){ System.out.println("良");//符合条件输出1 }else if (score>=90 && score<=100){ System.out.println("优秀");//符合条件输出1 } } }
选择结构switch语句的格式及其解释)(掌握)
- switch语句的使用场景:针对几个常量值进行判断
- 面试题
byte可以作为switch的表达式吗?
long可以作为switch的表达式吗?
String可以作为switch的表达式吗?JDK1.7 - switch语句案例演示
- switch语句键盘录入月份,输出对应的季节
import java.util.Scanner; public class SwitchSerious { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入数字1-12月份"); int month = sc.nextInt(); switch (month){//switch表示这是switch语句,表达式的取值:byte,short,int,char,JDK5以后可以是枚举(什么是枚举,以后再讲),JDK7以后可以是String case 1://case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的 System.out.println("冬季"); break;//可以省略,但是结果可能不是我们想要的。会出现一个现象:case穿透。最终我们建议不要省略 case 2: System.out.println("冬季"); break;//break表示中断,结束的意思,可以结束switch语句 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 12: System.out.println("冬季"); break; default://可以省略,但是不建议,因为它的作用是对不正确的情况给出提示。可以在任意位置。但是建议在最后。 System.out.println("你输入的信息不正确"); break; } } }
循环结构概述for语句使用)(掌握)
- 循环结构的分类
for循环
while循环 - for循环结构
- for循环结构的判断语句结果为boolean类型。
- for语句结构格式和案例演示
- 用for循环求出1-100之间偶数和
public class ForSum { public static void main(String[] args) { int sum=0;//自定义了一个sum for (int i = 0; i < 100; i++) {//在100之间进行循环 if(i%2==0){//为偶数的条件 sum+=i;//为偶数时候进行求和 } } System.out.println("1-100之间的偶数和是:"+sum); } }
- 在控制台用for循环输出所有的”水仙花数”并求和
//所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 public class ForWaterFlower { public static void main(String[] args) { int count=0;//定义一个总数 for (int i = 100; i < 1000; i++) {//100-1000之间的水仙花数 int ge=i%10;//各位上的水仙花数,记清楚是在for 循环里面写 int shi=i/10%10;//十位上的水仙花数 int bai=i/100%10;//百位上的水仙花数 int flower=ge*ge*ge+shi*shi*shi+bai*bai*bai;//进行计算 if (flower==i){//如果是水仙花 System.out.println(i);//就输出水仙花 count++;//进行个数的统计 } } System.out.println(count); } }
//ford的死循环结构 for(;;){ ...//循环体语句 }
- for循环输出5行5列的直角三角形
public class ForTriangle { public static void main(String[] args) { for (int i = 1; i <= 5; i++) {//输出五列,外循环控制内循环内循环控制数列 for (int j =1; j <=i; j++) {//输出五行 System.out.print("*"); } System.out.println();//进行换行 } } }
- for循环输出九九乘法表
public class ForNineNineForm { public static void main(String[] args) { for (int i = 1; i <= 9; i++) {//外循环控制内循环,内循环控制行数 for (int j = 1; j <=i; j++) { System.out.print(j+"*"+i+"="+i*j+"\t");//去掉换行,加上空格 } System.out.println();//实现换行 } } }
- for循环输出菱形
public class ForRhombus { public static void main(String[] args) { int h=5;//请输入一个奇数 int w=5; for(int i = 1; i <= (h+1)/2; i++){ //输出第一部分空格 for(int j = 1; j <= (h+1)/2-i; j++){ System.out.print(" "); } //输出第一部分星号 for(int j = 1;j <= (2*i)-1; j++){ System.out.print("*"); } System.out.println( ); } //开始第二部分 for(int i = 1; i <= h/2; i++){ //输出第二部分空格 for(int j = 1;j <= i;j++){ System.out.print(" "); } //输出第二部分星号 for(int j = 1;j <= (h/2-i+1)*2-1;j++){ System.out.print("*"); } System.out.println(); } } }
- for循环输出正三角形
public class ForTri { public static void main(String[] args) { int h=10; for(int i = 1; i <= (h+1)/2; i++){ //输出第一部分空格 for(int j = 1; j <= (h+1)/2-i; j++){ System.out.print(" "); } //输出第一部分星号 for(int j = 1;j <= (2*i)-1; j++){ System.out.print("*"); } System.out.println( ); } } }
- for循环输出倒三角形
public class ForTria { public static void main(String[] args) { int line=8;//定义行数 for (int i = 0; i <= line; i++) { for (int j = 0; j <= i; j++) {//空格数 System.out.print(" "); } for (int j = 0; j < 2*(line-i)-1; j++) {//*数 System.out.print("*"); } System.out.println(); } } }
- for循环输出平行四边形练习
public class Demo09 { public static void main(String[] args) { //实现一个5行5列的平行四边形 for (int i = 1; i <= 5; i++) { //空格要从0开始,第一行空4个,第二行空3个,以此类推 for (int j = 0; j <5-i; j++) { System.out.print(" "); } //每行都有五个循环五行 for (int k =1; k <= 5; k++) { System.out.print("*"); } System.out.println(); } } }
- 正方形练习
public static void main(String[] args) { //实现一个4行4列的正方形,实现4行 for (int i =1; i <= 4; i++) { //实现4列 for (int j =1; j <=3; j++) { System.out.print("*"); } //换行 System.out.println(); } } }
- 循环结构while语句的格式和案例(掌握)
- 循环结构注意事项之死循环)(掌握)
while(true){...} for(;;){...}
- 案例展示,while求和思想: 求1-100之和
public class WhileSum { public static void main(String[] args) { int i=1;//定义i为1, 初始化条件语句; int sum=0;//定义和为0, 初始化条件语句; while (i<=100){//循环条件,判断条件语句 sum+=i;//进行求和, 循环体语句; i++;//i进行递加, 控制条件语句; } System.out.println(sum); } }
- 统计思想: while循环统计”水仙花数”共有多少个
public class WhileSum { public static void main(String[] args) { int i=100; int count =0; while(i<=1000){ int ge=i%10;//各位上的水仙花数,记清楚是在for 循环里面写 int shi=i/10%10;//十位上的水仙花数 int bai=i/100%10;//百位上的水仙花数 int flower=ge*ge*ge+shi*shi*shi+bai*bai*bai;//进行计算 if (flower==i){//如果是水仙花 System.out.println(i);//就输出水仙花 count++;//进行个数的统计 } i+=1; } System.out.println(); System.out.println(count); } }
- 循环结构do…while语句的格式和基本使用(掌握)
- do while 求出1-100之间的和
class DoWhilePractice01{ public static void main(String[] args){ int sum=0; int i=1;//初始化条件语句 do{ sum+=i;//执行循环体语句 i++;//控制条件语句 }while(i<=100);//判断条件语句,返回值为true,继续循环;返回值为false,结束循环 System.out.println("1--100的和为:"+sum); } }
- 循环结构三种循环语句的区别)(掌握)
(1):do...while循环至少执行一次循环体。 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。 (2):A: 如果你想在循环结束后,继续使用控制条件的那个变量,用while循环,否则用for循环。不知道用for循环。 因为变量及早的从内存中消失,可以提高内存的使用效率。 B:建议优先考虑for循环,然后是while循环 ,最后是do...while循环
05 控制跳转语句
- Java中使用break/continue/return来实现控制语句的跳转和中断。
a.break
- 功能:1.退出单层循环 2.退出多层循环 (很少使用)
- 应用场景:在选择结构switch语句中/在循环语句中。
- 注:离开使用场景的存在是没有意义的。
class BreakPractice01{ public static void main(String[] args){ int count=0;//定义一个变量,用来统计控制台一共输出了几次 for(int i=1;i<=10;i++){ if(i%3==0){//当i=3时,执行break;跳出for循环 break; } System.out.println("控制台输出了"+i);//当i%3!=0时,执行该语句。当i=3时,跳出循环。 count++; } System.out.println("控制台总共输出了"+count+"次"); } } 运行结果:控制台输出了1 控制台输出了2 控制台总共输出了2次
b.continue
- 功能:跳出一次循环,执行下一步操作。
- 应用场景:在循环语句中
- 注:离开使用场景的存在是没有意义的。
class ContinuePractice01{ public static void main(String[] args){ int count=0;//定义一个变量,用来统计控制台一共输出了几次 for(int i=1;i<=10;i++){ if(i%3==0){ continue;//当i%3=0时,只跳出一次循环 } System.out.println("控制台输出了"+i); count++; } System.out.println("控制台总共输出了"+count+"次"); } } 运行结果:控制台输出了1 控制台输出了2 控制台输出了4 控制台输出了5 控制台输出了7 控制台输出了8 控制台输出了10 控制台总共输出了7次
c.return
- 功能:退出一个方法。(在方法的使用里有详细笔记)
相关文章推荐
- switch...case、if...else、for和while循环、do...while语句、双重for循环
- Swift第三课 分支语句 if else for forin switch while do-while
- 【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)
- C语言中流程控制语句(if,switch,for,while,do-while,continue,break,return)
- if,continue,break,while,do-while, switch,return,foreach,for等条件语句的使用
- Java基础-04.总结switch,for,while,do。while跳转语句
- 循环控制语句for while do-while if switch
- 控制流程语句if、switch、while、do-while、for
- if,switch,while,do..while,for语句在JSP中的用法示例
- Java基础第一阶段——03_Java语言基础_运算符&if&while&switch&for语句
- 分支语句 循环语句 if,switch,do,while,for分析
- java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)
- if条件和for循环语句、while、do..while、switch语法
- Java基础巩固(控制流程语句之---if 判断语句、switch选择判断语句、while循环语句、for循环语句的格式,转义字符、break关键字、continue关键字)
- if和switch语句、循环语句while和do……while、for
- 流程控制语句(if,switch,for,while,do-while)
- 使用while、do-while、for打印九九乘法表、三角形和冒泡排序
- js实现输出一个等腰三角形菱形 九九乘法表.....(for 循环 while...)
- JAVA之旅(二)——if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结
- c语言基础switch、if、while和for基本用法