控制流程语句:
语句:使用分号分隔的代码就是一个语句。
顺序语句:按照代码顺序从上往下执行所有的代码就是顺序语句
*/
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");
System.out.println("D");
}
}
/*
控制流程语句之(if)判断语句
if判断语句的格式:
格式1:(适用于一种情况使用)
if(判断的条件){
符合条件执行的代码;
}
格式2:(适用于两种情况使用)
if(判断条件){
符合条件执行的代码;
}else{
不符合条件执行的代码;
}
三元运算符的格式:布尔表达式?值1:值2;
if-else 与三元运算符非常像:
三元运算符的优点:结果比较简洁。
三元与运算符的缺点:符合条件必须返回一个结果,不能执行语句。
格式3:适用于多种情况下使用的
if(判断条件){
符合条件1执行的代码
}else if(判断条件2){
符合条件2执行的代码
}else if(判断条件3){
符合条件3执行的代码
}......else{
都不符合上述的条件执行的代码
}
if语句要注意的细节:
1.如果符合条件后只有一个语句需要执行,那么可以省略大括号。但是建议不要省略,因为结构不清晰。
2.if语句的判断条件后面不能添加分号,否则会影响到执行的效果。
需求1:工作经验要两年或者两年以上,年龄大于18岁
需求2:根据一个变量所记录的数字输出对应的日期。 0------星期天 1----星期一
*/
class Demo2
{
public static void main(String[] args)
{
int workAge =2;
/*
格式1:
if(workAge>=2){
//如果符合上述的条件,那么就执行大括号的代码
System.out.println("电话通知带齐资料过来面试。");
}
格式2:
if(workAge>=2){
//符合上述的条件执行的代码
System.out.println("电话通知带齐资料过来面试。");
}else{
//如果不符合上述条件执行的代码
System.out.println("电话通知不要再投简历了,不收你!!");
}
三元运算符:
String result=workAge>=2?"电话通知你面试":"电话通知不要再投简历了,不收你!!";
System.out.println(result);
*/
int num=31;
if (num==0)
{
System.out.println("星期天");
}else if(num==1){
System.out.println("星期一");
}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("星期六");
}else{
System.out.println("没有对应的星期");
}
}
}
/*
需求:键盘录入一个分数,根据分数输出对应的等级。
比如 100-90 A等级 89-80 B等级.....E等级
接受键盘录入数据的步骤:
1.创建一个扫描器对象。
2.调用扫描器对象的nextInt方法扫描数据
3.导入包
*/
import java.util.*;
class Demo3
{
public static void main(String[] args)
{
System.out.println("请输入一个分数:");
//创建一个扫描器
Scanner scanner = new Scanner(System.in);
//调用扫描器扫描键盘录入的数据
int score = scanner.nextInt();//定义一个num变量接收扫描到的内容。
//System.out.println("录入的数据是:"+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 (score>=60&&score<=69){
System.out.println("D等级");
}else if (score>=0&&score<=59){
System.out.println("E等级");
}else{
System.out.println("补考");
}
}
}
/*
控制流程语句之----if判断语句
格式一:只适用于一种情况去使用
if(判断条件){
符合条件执行代码
}
格式二:适用于两种情况下去使用:
if(判断条件){
符合条件执行代码
}else{
不符合条件执行的代码
}
格式3:适用于多种情况使用的
if(判断条件的语句1){
符合条件1执行的语句
}else if(判断条件语句2){
符合条件2执行的代码
}else if(判断条件语句3){
符合条件3执行的代码
}......else{
都不符合上述条件执行的代码
}
*/
class Demo1
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*
控制流程语句之-----switch选择判断语句
switch语句的格式:
switch(你的选择){
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语句一旦匹配上了其中的一个case,那么就会执行对应的case中的语句,执行完毕之后如果没有遇到break关键字
或者结束switch语句的大括号,那么switch语句不会再判断,按照代码的顺序从上往下执行所有的代码。直到遇到break或者结束switch语句的大括号为止。
4. 在switch语句中不管代码的顺序如何,永远都会先判断case语句,然后没有符合的条件下才会执行default语句。
if--else if---elseif语句与switch语句非常相似:
switch语句的优点:switch语句的结构清晰
switch语句的缺点:如果判断的条件是一个区间范围的,使用switch语句非常麻烦。(此时使用if--else if 语句)
判断以下哪些不是计算机语言()
A.java B.c# c.javascript D.android
*/
class Demo2
{
public static void main(String[] args)
{
/*
int option = 4;//定义一个变量存储你的选择
switch(option){
case 1:
System.out.println("java");
break;
case 2:
System.out.println("c#");
break;
case 3:
System.out.println("javascript");
break;
case 4:
System.out.println("android");
break;
default:
System.out.println("你的选择有误");
break;
}
String str = "world";
switch(str){
case"hello":
System.out.println("hello");
break;
case "world":
System.out.println("world");
break;
}
*/
int option =13;//定义一个变量存储你的选择
switch(option){
default:
System.out.println("你的选择有误");
case 1:
System.out.println("java");
case 2:
System.out.println("c#");
case 3:
System.out.println("javascript");
case 4:
System.out.println("android");
// default:
// System.out.println("你的选择有误");
}
}
}
/*
需求:接受键盘录入一个月份,根据对应的月份输出对应的季节。
345 春天
678 夏天
9 10 11 秋天
1 2 12 冬天
要求使用switch语句实现。
*/
import java.util.*;
class Demo4
{
public static void main(String[] args)
{
System.out.println("请输入一个月份:");
//创建一个扫描器
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:
case 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:
System.out.println("没有对应的季节");
break;
}
}
}
/*
循环语句------while循环语句
while循环语句的格式:
while(循环条件){
循环条件;
}
while循环语句要注意的事项:
1.while循环语句一般通过一个变量控制循环的次数。
2.while循环语句的循环体代码如果只有一个语句的时候,那么可以省略大括号。但是不建议大家省略的。
3.while循环语句的判断条件后面不能跟有分号,否则会影响我们执行的效果。
需求:在控制台上打印五句hello world.
*/
class Demo5
{
public static void main(String[] args)
{
int count = 0;
while(count<5){
System.out.println("Hello World!");
count++;
}
}
}
/*
需求::计算1+2=3+......+100的总和。
需求1:计算1~100,7的倍数总和
需求2:实现猜数字游戏,
*/
class Demo6
{
public static void main(String[] args)
{
/*
int num = 1;
int sum = 0;//定义一个变量用于保存每次相加的结果
while(num<=100){
sum = sum + num;//sum = 1
num++;
}
System.out.println("sum = "+sum);
*/
int num = 1;
int sum = 0;//定义一个变量用于保存每次相加的总和。
while (num<=100)
{
if(num%7==0){
sum = sum + num;
}
num++;
}
System.out.println("7的倍数总和:" +sum);
}
}
/*
需求1:计算1~100,7的倍数总和
需求2:实现猜数字游戏,如果没有猜对可以继续输入你猜的数字,如果猜对了停止程序。
如何产生一个随机数。
步骤:
1.创建一个随机数对象。
2.调用随机数对象的nextInt方法。
3.导包。
*/
import java.util.*;
class Demo7
{
public static void main(String[] args)
{
//创建一个随机数对象
Random random = new Random();
//调用随机数对象的nextInt方法,产生一个随机数
int num = random.nextInt(11);//产生0-10之间的随机数
System.out.println("随机数:" + num);
}
}
/*
需求2:实现猜数字游戏,如果没有猜对可以继续输入你猜的数字,如果猜对了停止程序。
最多只能猜三次,如果还剩下最后一次机会的时候要提醒用户。
如何产生一个随机数。
步骤:
1.创建一个随机数对象。
2.调用随机数对象的nextInt方法。
3.导包。
*/
import java.util.*;
class Demo8
{
public static void main(String[] args)
{
//创建一个随机数对象
Random random =new Random();
//调用随机数对象的nextInt()方法产生一个随机数
int randomNum = random.nextInt(10)+1;//要求随机数是1~10
//创建一个扫描器对象
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag){
System.out.println("请输入你要猜的数字:");
//调用扫描器的nextInt方法扫描一个数字
int guessNum = scanner.nextInt();
if(guessNum>randomNum){
System.out.println("猜大了...");
}else if (guessNum<randomNum){
System.out.println("猜小了...");
}else{
System.out.println("恭喜你,猜对了...");
flag = false;
}
}
}
}
/*
控制循环语句----do while 循环语句
格式:
do{
}while(判断条件);
需求:在控制台上打印五句hello world.
while循环语句与do-while循环语句的区别:
while循环语句是先判断后执行循环语句,do-while循环语句是先执行,后判断,不管条件是否满足至少会执行一次。
*/
class Demo9
{
public static void main(String[] args)
{
/*
int count = 0;
while (count<5)
{
System.out.println("Hello World!");
count++;
}
do{
System.out.println("hello world");
count++;
}while(count<5);
在java中,java编译器是不允许写废话的。
boolean flag = false;
while(flag){
System.out.println("hello world");
}
boolean flag = false;
do
{
System.out.println("hello world");
}
while (flag);
*/
int count = 0;
do
{
System.out.println("hello world");
count++;
}
while (count < 5);
}
}
/*
需求:使用do-while算出1-100之间的偶数总和。
*/
class Demo10
{
public static void main(String[] args)
{
int num = 1;
int sum = 0;//定义一个变量用于保存每次相加的总和。
do
{
if (num%2==0)
{
sum = sum + num;
}
num++;
}
while (num<101);
System.out.println("偶数的总和:" + sum);
}
}
/*
控制流程语句之-----for循环语句
for 循环语句的格式:
for(初始化语句;判断语句;循环后的语句){
循环语句;
}
for 循环语句的执行流程:
初始化语句A
循环体语句B 第一次
循环语句后c for (System.out.println("初始化语句A");count<5 ;System.out.println("循环语句后c"))
{
System.out.println("循环体语句B");
count++;
}
循环体语句B 第二次
循环语句后c
第三次
循环体语句B
循环语句后c
第四次
循环体语句B
循环语句后c
第五次
循环体语句B
循环语句后c
for 循环语句要注意的事项:
1.for(;;)这种写法是一个死循环语句,相当于while(true);
2.初始化语句只是在第一次循环的时候执行一次而已,之后都不会再执行了
3.for循环语句的循环体语句只有一句的时候可以省略大括号,但是不建议省略
需求: 在控制台上打印五句hello world.
*/
class Demo11
{
public static void main(String[] args)
{
/*
int count = 0;
while (count<5)
{
System.out.println("Hello World!");
count++;
}
for (int count = 0;count<5 ;count++ )
{
System.out.println("hello world");
}
*/
int count = 0;
for (System.out.println("初始化语句A");count<5 ;System.out.println("循环语句后c"))
{
System.out.println("循环体语句B");
count++;
}
}
}
/*
需求:在控制台上打印一个五行五列矩形。
*****
*****
*****
*****
*****
先打印一行
*/
class Demo12
{
public static void main(String[] args)
{
for (int j=0;j<5 ;j++ ) //j=0 控制行数
{
for (int i=0;i<5 ;i++ )//i=0 控制列数
{
System.out.print("*");
}//*****
//换行
System.out.println();
}
}
}
/*
需求: 在控制台上打印一个正立的直角形
*
**
***
*****
多行多列的图形。
行数5行
列数:会发生变化的
分析列数:
i=0;i<5;j=0;j<=0 1个星号
i=1;i<5;j=0;j<=1 2个星号
i=2;i<5;j=0;j<=2 3个星号
*/
class Demo13
{
public 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("");
}
}
}
/*
需求:打印一个倒立的直角三角形。
*****
****
***
**
*
5行
列数会发生变化
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 Demo14
{
public 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.println("");
}
}
}
/*
需求:打印一个九九乘法表。
*/
class Demo15
{
public 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" world
常见的转义字符有:
\b Backspace(退格键)
\t Tab (Tab键盘) 制表符(制表符的作用就是为了让一列对齐)一个Tab键等于四个空格
\n Linefeed (换行)
\r Carriage Return (回车) 把光标移动到一行的首位置上。
\\ Backslash (反斜杠)
注意:如果在windows系统上操作文件的时候需要换行,是需要\r\n一起使用的。
如果是在其他操作系统上需要换行,仅需要\n即可。
需求:
world
*/
import java.io.*;
class Demo16
{
public static void main(String[] args) throws Exception
{
//System.out.println("Hello\" \n World!");
//System.out.println("Hello\" \r World");
File file = new File("F:\\Java project\\day04\\a.txt");
FileWriter out = new FileWriter(file);
out.write("大家好\r\n");
out.write("你们好");
out.close();
}
}
/*
break、continue关键字
break的适用范围:只能用于switch或者循环语句中。
break的作用:
1.break用于switch语句的作用是结束一个switch语句。
2.break用于循环语句中的作用是结束当前所在的循环语句
笔试题目:break目前位于内层for循环,如何才能让break作用于外层for循环.
可以配合标记解决
标记的命名只要符合标识符的命名规则
*/
class Demo17
{
public static void main(String[] args)
{
/*for (int j=0;j<3 ;j++ )//j=0 1 2 外层for循环
{
for (int i=0;i<5 ;i++ ){//i=0 内层for循环
System.out.println("hello world");// 1 2 3
//break;
}
}
*/
outer:for (int j=0;j<3 ;j++ ){//j=0 外层for循环
inner:for (int i=0;i<5 ;i++ ){//i=0 内层for循环
System.out.println("hello world");// 1
break outer;
}
}
}
}
/*
continue关键字
continuede 适用范围:continue只能用于循环语句。
continue的作用:continue 的作用是跳过本次循环内容,继续下一次.
continue要注意的事项:
1.在一种情况下,continue后面不能跟其他语句,因为是废话,因为永远都无法执行到它
for (int i=0;i<5 ;i++ ){
continue;
System.out.println("Hello World!"+i);
2.continue也可以配合标记使用的。
*/
class Demo18
{
public static void main(String[] args)
{
/*for (int i=0;i<5 ;i++ ){
System.out.println("Hello World!");
continue;
//break;
}
for (int i=0;i<5 ;i++ ){//i=1 2
if (i==1){
continue;
}
System.out.println("Hello World!"+i);
}
for (int i=0;i<5 ;i++ ){
continue;
System.out.println("Hello World!"+i);
}
outer:for (int i=0;i<3 ;i++ ){//i=0;i=1 i=2 i=3
inner:for (int j=0;j<2 ; j++){//j=0
System.out.println("Hello");// 1 2 3
continue outer;
}
}
*/
int sum=0;
for (int num=1;num<=100 ;num++ ){
if (num%2!=0){
continue;
}
sum = sum + num;
}
System.out.println("总和:" + sum);
}
}