j控制流程语句基础入门_第1页
j控制流程语句基础入门_第2页
j控制流程语句基础入门_第3页
免费预览已结束,剩余27页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*控制流程语句 :语句 : 使用分号分隔的代码就是一个语句。顺序语句 : 按照代码顺序从上往下执行所有的代码就是顺序语句, */class Demo1 public static void main(String args)/*int i = 10; / 声明变量的语句;/ 空语句System.out.println("Hello World!"); / 输出语句*/System.out.println("A");System.out.println("B");System.out.println("C");Sy

2、stem.out.println("D");/*控制流程语句之( if )判断语句if 判断语句的格式:格式 1:适用于一种情况使用if( 判断的条件 )符合条件执行的代码格式 2 : 适用于两种情况下去使用的if( 判断条件 )符合条件执行的代码else不符合条件执行的代码;三元运算符的格式:布尔表达式?值 1:值 2;if-else 与三元运算符非常像:三元运算符的优点: 结构比较简洁三元运算符的缺点: 符合条件必须要返回一个结果,不能执行语句格式三: 适用于多种情况下去使用的。if( 判断条件 1)符合条件 1 执行的代码else if( 判断条件 2)符合条件 2

3、执行的代码else if( 判断条件 3)符合条件 3 执行的代码else都不符合上述 的条件执行的代码if 语句要注意的细节:1. 如果符合条件后只有一个语句需要执行,那么可以省略大括号。但是建议不 要省略,因为结构不清晰。2. if 语句的判断条件后不能添加分号,否则会影响到执行的效果的,需求 1:工作经验要两年或者两年以上需求2:根据一个变量所记录的数字输出对应的星期。0 -星期天 1 星期一*/ class Demo2 public static void main(String args) int workAge = 2;/*格式 1 :if(workAge>=2)System

4、.out.println(" 电话通知过来面试 .");格式 2 :if(workAge>=2)/ 符合条件执行的代码System.out.println(" 电话通知你面试 ");else/ 如果不符合上述的条件执行的代码System.out.println(" 电话通知不要再投简历了,不收你 !");*/int num = 31;if(num=0)System.out.println(" 星期天 ");else if(num=1)System.out.println(" 星期一 ");

5、else if(num=2)System.out.println(" 星期二 ");else if(num=3)System.out.println(" 星期三 ");else if(num=4)System.out.println(" 星期四 ");else if(num=5)System.out.println(" 星期五 ");else if(num=6)System.out.println(" 星期六 ");elseSystem.out.println(" 没有对应的星期 &q

6、uot;);/*需求: 键盘录入一个分数,根据分数输出对应的等级。比如: 100-90 A 等级 89-80 B 等级 E 等级接受键盘录入数据的步骤:1. 创建一个扫描器对象。2. 调用扫描器对象的 nextInt 方法扫描数据。3. 导入包*/ import java.util.*;class Demo3public static void main(String args)/ 创建一个扫描器Scanner scanner = new Scanner(System.in);/ 调用扫描器扫描键盘录入的数据System.out.println(" 请输入一个分数: ");

7、int score = scanner.nextInt(); / 定义了一个 num 变量接收扫描到内容。if(score>=90&&score<=100)System.out.println("A 等级 ");else if(score>=80&&score<=89)System.out.println("B 等级 ");else if(score>=70&&score<=79)System.out.println("C 等级 ");else if(

8、score>=60&&score<=69)System.out.println("D 等级 ");else if(score>=0&&score<=59)System.out.println("E 等级 ");elseSystem.out.println(" 补考 .");/*控制流程语句之 -if 判断语句格式一: 只适用于一种情况下去使用。if( 判断条件 )符合条件执行的代码 ;格式二:适用于两种情况下去使用if( 判断条件 )符合条件执行的代码else不符合条件执行 的

9、代码格式 3: 适用于多种情况使用的if( 判断条件 1)符合条件 1 执行的 语句;else if( 判断条件 2)符合条件 2 执行 的语句 ;else if( 判断条件 3)符合条件 3 执行 的语句 ;else if( 判断条件 4)符合条件 4 执行 的语句 ;else都不符合上述 条件执行的代码 . */class Demo1public static void main(String args) System.out.println("Hello World!");/*控制流程语句之 switch 选择判断语句switch 语句的格式:switch( 你的选择

10、 )case 值 1 :符合值 1 执行的代码 break;case 值 2 :符合值 2 执行的代码 break;case 值 3 :符合值 3 执行的代码 break;case 值 4 :符合值 4 执行的代码 break;default: 你的选择都符合上述的选项时执行的代码 break;switch 语句要注意的事项:1. switch 语句使用的变量只能是 byte、 char、 short 、int 、 String 数据类型, String 数据类型是从 jdk7.0 的时候开始支持的。2. case 后面跟 的数据必须是一个常量3. switch 的停止条件:switch 语句

11、一旦匹配上了其中的一个 case 语句,那么就会执行对应的 case 中的语句代码,执行完毕之后如果没有遇到 break 关键字或者是结束 switch 语句的大括号,那么 switch 语句 不会再判断,按照代码的顺序从上往下执行所有的代码。直到遇到 break 或者是结束 siwitch 语句的大括号为止。4. 在 switch 语句中不管代码的顺序如何,永远都是会先判断 case 语句,然后 没有符合的情况下才会执行 default 语句。if-else if-else if 语句与 switch 语句非常的相似:switch 语句的优点: switch 语句的结构清晰。switch 缺

12、点:如果 判断的条件是一个区间范围的,使用 switch 操作就非常的 麻烦了。判断以下那些不是计算机语言 ( D )A java B C# C javascript D android*/class Demo2public static void main(String args)/ 定义一个变量存储你的选择int option = 13; switch(option)case 1:System.out.println("java");case 2:System.out.println("C#");case 3:System.out.println(&

13、quot;javascript");case 4:System.out.println("android");default:System.out.println(" 你的选择有误 ");/*String str = "world" switch(str)case "hello":break;case "world":System.out.println("world");break;*/*需求: 接受键盘录入一个月份, 根据对应的月份输出对应的季节345 春天67

14、8 夏天9 10 11 秋天1 2 12 冬天要求使用 switch 语句实现。*/import java.util.*;class Demo4public static void main(String args)/ 创建一个扫描器Scanner scanner = new Scanner(System.in);/ 调用扫描器的 nextInt 方法int month = scanner.nextInt();switch(month)case 3:case 4:case 5:System.out.println(" 春天 "); break;case 6:case 7:c

15、ase 8:System.out.println(" 夏天 "); break;case 9:case 10:case 11:System.out.println(" 秋天 ");break;case 12:case 1:case 2:System.out.println(" 冬天 ");break;default:break;/*循环语句 while 循环语句while 循环 语句的格式 :while( 循环的条件 )循环语句;while 循环语句要注意的事项:1. while 循环语句一般是通过一个变量控制其循环的次数。2. wh

16、ile 循环语句的循环体代码如果只有一个语句的时候,那么可以省略大括 号。但是也是不建议大家省略。3. while 循环语句的判断条件后面不能跟有分号,否则会影响到执行的效果。需求: 在控制上打印五句 hello world.*/class Demo5public static void main(String args)int count = 0;while(count<5)System.out.println("Hello World!"); count+;/*需求: 计算 1+2+3+ 100 的总和。*/class Demo6public static voi

17、d main(String args)int num = 1;int sum = 0; / 定义一个变量用于保存每次相加的结果 while(num<=100)sum = sum+num; / sum = 1 num+;System.out.println("sum = "+ sum);/*需求 1:计算 1-100,7 的倍数总和。 7 14 21如何产生一个随机数。步骤:1. 创建一个随机数对象。2. 调用随机数对象的 nextInt 方法。3. 导包。*/class Demo7 public static void main(String args)int num

18、 = 1;int sum = 0; / 定义一个变量用于保存每次相加的总和while(num<=100) / num = 1 if(num%7=0)sum = sum+num; num+;System.out.println(" 总和是: "+ sum);/*需求 2: 实现猜数字游戏, 如果没有猜对可以继续输入你猜的数字,如果猜对了停止 程序。最多只能猜三次,如果还剩下最后一次机会的时候要提醒用户。*/import java.util.*;class Demo8 public static void main(String args)/ 创建一个随机数对象Rando

19、m random = new Random();/ 调用随机数对象的 nextInt 方法产生一个随机数int randomNum = random.nextInt(10)+1; /要求随机数是 110/ 创建一个扫描器对象Scanner scanner = new Scanner(System.in);while(true)System.out.println(" 请输入你要猜的数字 :");/ 调用扫描器的 nextInt 方法扫描一个数字int guessNum = scanner.nextInt();if (guessNum>randomNum)System.

20、out.println(" 猜大了 .");else if(guessNum<randomNum)System.out.println(" 猜小了 .");elseSystem.out.println("恭喜你,猜对了 '.");break;/*控制流程语句 do while 循环语句格式:dowhile( 判断条件 );需求: 在控制上打印五句 hello world.while 循环语句与 do-while 循环语句的区别:while 循环语句是先判断后执行循环语句的, do-while 循环语句是先执行,后判断。不

21、管条件是否满足至少会执行一次。*/ class Demo9 public static void main(String args)/* int count =0;while(count<5)System.out.println("Hello World!"); count+;在 java 中, java 编译器是不允许写废话。boolean flag = false; while(flag)System.out.println("Hello World!"); boolean flag = false; doSystem.out.println(

22、"Hello World!"); while(flag);*/int count = 0;do System.out.println("hello world"); count+;while(count<5);/*需求: 使用 do-while 算出 1-100 之间偶数的总和。*/class Demo10public static void main(String args)int num = 1;int sum = 0; / 定义一个变量用于保存每次相加的总和 doif(num%2=0)sum += num; num+;while(num<

23、;101);System.out.println("sum = "+ sum);/*控制流程语句之 -for 循环语句for 循环语句的格式for( 初始化语句 ;判断语句 ; 循环后的语句 )循环语句 ;for 循环语句 要注意的事项:1. for(;) 这种写法 是一个死循环语句,相当于 while(true);2. for 循环语句的初始化语句只会执行一次,只是在第一次循环的时候执行而 已。3. for 循环语句的循环体语句只有一句的时候,可以省略大括号不写。但是不 建议省略。需求: 在控制上打印五句 hello world.*/class Demo11public

24、static void main(String args)/*int count=0;while(count<5);System.out.println("Hello World!");count+;int count = 0 ;for(System.out.println(" 初始化语句 A");count<5 ;System.out.println(" 循环后的语句 C")System.out.println(" 循环体语句 B");count+;*/for(int count = 0 ; coun

25、t<5; count+)System.out.println("hello world");/*需求: 在控制台上打印一个 五行五列矩形 /. *先打印一行*/class Demo12 public static void main(String args)控制列数for(int j = 0 ; j<5 ; j+) /控制行数for(int i = 0 ; i<5 ; i+) /System.out.print("*"); / */ 换行System.out.println();/*需求: 在控制台上打印一个正立的直角三角形 *多行多列

26、的图形。行数 5 行列数: 会发生变化 的 .分析列数 :i = 0 ; i<5; j=0 ; j<=i1 个星号i = 1 ; i<5 ;j=0 ; j<=12 个星号i = 2 ; i<5; j=0 ; j<=2 3 个星号*/class Demo13public static void main(String args)控制列数for(int i = 0 ; i< 5 ; i+)for(int j = 0 ; j<=i ; j+) /System.out.print("*"/ 换行System.out.println()

27、;/*需求:打印一个倒立的直角三角形。*5行列数会发生变化j<(5-i)i= 0 ; i<5; j=0 ; j<5 ;五个星号i = 1; i<5; j=0 ; j<4;四个星号i = 2; i<5; j=0 ; j<3;三个星号*/class Demo14public static void main(String args)for(int i = 0 ; i<5; i+)for (int j = 0 ; j<(5-i) ;j+ ) System.out.print("*");/ 换行 System.out.print

28、ln();/*需求:打印一个九九乘法表 .*/class Demo15public static void main(String args)for(int i = 1 ; i<=9 ; i+)for(int j = 1 ; j<=i ; j+) /控制列数System.out.print(i+"*"+j+"="+i*j+"t");/ 换行System.out.println();/*转义字符:特殊字符使用” ”把其转化成字符的本身输出,那么使用” ”的字符称 作为转移字符需求: 在控制台上打印一个 hello"

29、 world常见的转义字符有:b Backspace (退格键)t Tab 制表符 (制表符的作用就是为了让一列对齐 ) 一个 tab 一般等于四 个空格。n 换行r 回车 把光标移动到一行的首位置上。注意: 如果是在 windows 系统上操作文件的时候需要换行,是需要 rn 一起使用的。 如果是在其他的操作系统上需要换行,仅需要 n 即可。*/import java.io.*;class Demo16 public static void main(String args) throws Exception/System.out.println("Hello 哈哈 rworld!

30、");File file = new File("F:a.txt"); FileWriter out = new FileWriter(file);out.write(" 大家好 rn");out.write(" 你们好 ");out.close();/*break 、break 适用范围:只能用于 switch 或者是循环语句中。break 作用:1. break 用于 switch 语句的作用是结束一个 switch 语句。2. break 用于循环语句中的作用是结束当前所在的循环语句。的 for 循环笔试题目: break 目前位于内层的 for 循环,如何才能让 break 作用于外层 可以标记解决标记的命名只要符合标识符的命名规则即可。*/class Demo17外层 for 循环public static void main(String args) aaa:for(int j = 0 ; j<3 ; j+) / j=0bbb:for(int i = 0 ; i< 2 ; i+) / i=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论