小白学习java第三天

第三天
复习:

  1. 基本类型间的转换
    a) 自动转换(小—>大) 强制类型(大—>小) (要转换的类型)
    Boolean类型不参与转换
    Byte short char —>int—>long—>float—>double

  2. 运算符
    算术 赋值 比较 逻辑 三元

  3. 判断语句
    If(){}
    If(){}else{}
    If(){}else if(){}…else{}

  4. 键盘录入

学习
一个语句包含另一个语句为嵌套语句

目标

  1. 选择语句switch
  2. 循环语句

1.语句
选择语句:关键字:switch
也是分支语句的一个代表,是多重if语句的另外一种表现形式

switch(表达式){
case 常量值1:(与表达式类型一致)
    语句;…
     break;
case 常量值2:
	语句;…
	break;
default:
	语句;…
	break;
 }

练习

import java.util.*;
public class Demo2{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	//输入一个分数,0-100,输出打印对应的等级。
	System.out.println("请输入一个成绩:");
	int score = sc.nextInt();
 //	if(score >=0 && score <= 100){
 //			if(score >= 90){
 //				System.out.println("优秀");
 //			}else if(score >= 80){
 //				System.out.println("良好");
 //			}else if(score >= 70){
 //				System.out.println("中等");
 //			}else if(score >= 60){
 //				System.out.println("及格");
 //			}else{
 //				System.out.println("不及格");
 //			}

			switch(score/10){
			case 10:
			case 9:
				System.out.println("优秀");
				break;
			case 8:
				System.out.println("良好");
				break;
			case 7:
				System.out.println("中等");
				break;
			case 6:
				System.out.println("及格");
				break;
			default:
				System.out.println("不及格");
			}
		}else{
			System.out.println("输入不合法,成绩应该在0-100之间");
		}
	}
}
import java.util.*;
public class Demo3{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int week = sc.nextInt();
		switch(week){
		default:
			System.out.println("输入有误");
			break;
		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;
		
		}

		System.out.println("Hello World!");
	}
}
import java.util.*;
public class Demo4{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个要计算的符号:");
		String  s = sc.next();
		System.out.println("请输入两个要计算的整数:");
		int a = sc.nextInt();
		int b = sc.nextInt();
		switch(s){
		case "+":
			System.out.println(a+"+"+b+"="+(a+b));
			break;
		case "-":
			System.out.println(a+"-"+b+"="+(a-b));
			break;
		case "*":
			System.out.println(a+"*"+b+"="+(a*b));
			break;
		case "/":
			System.out.println(a+"/"+b+"="+(a/b));
			break;
		}

	}
}

说明:

  1. 表达式结果类型:byte,short,char,int,String,枚举
    流程:
  2. 计算表达式,根据结果进入大括号中,与case后的值进行匹配,相同执行对应的语句,执行break语句,结束switch语句;如果不匹配,找第二个case,与其常量进行匹配…以此类推,全不匹配,执行default(没顺序,一般在最后,可写可不写),然后结束语句。
  3. 如果没有break,会执行匹配到的那个case之后所有的case语句,直到出现break。
  4. 结束switch语句的标志:break;或 }。
  5. case和default编写没有顺序之分,一般按顺序写。

循环语句
某些相同或相似的语句想要重复执行多次,可以使用循环语句简化结构。
代表:while do-while for
组成:重复执行多次的语句 初始化变量 更改初始化变量语句 循环条件

代表语句1:

 初始化变量
  while(循环条件){
     重复执行多次的语句
     更改初始化变量语句
}	

先判断,后执行。次数::0-n

   求和公式:sum=sum+a;a++;
public class Demo5{
	public static void main(String[] args){
		//输出打印5遍的 "Hello World!"
		int a = 0;
		while(a < 5){
			System.out.println("Hello World!");
			a++;
		}

		int x = 5;
		while(x > 0){
			System.out.println("hello,java");
			x--;
		}

		
		//想要输出打印1-10间的每个数字
		int b = 1;
		while(b <= 10){
			System.out.println(b);
			b++;
		}

		int y = 10;
		while(y > 0){
			System.out.println(y);
			y--;
		}

		//练习:输出打印1-10间的奇数,求奇数和
		int m = 1;
		int s = 0;
		while(m < 11){
			if(m % 2 != 0){
				System.out.println("m = " + m);
				//1+3+5+7+9
				s = s + m;
			}
			m++;
		}

		System.out.println("s = " +s );
	

		//练习:计算1+2+3+....+10的和。
		/*
		  int  sum = 0; //和
          0+1
			1+2
			  3+3
			    6+4
				 10+5
				   15+6
				     ...     sum = sum + 加数
		*/
		int n = 1;
		int sum = 0;
		while(n < 11){
			sum = sum + n;
			n++;
		}

		System.out.println("sum = " + sum);

		//练习:计算1~100间的所有的9得倍数。
		int q = 1;
		int he = 0;
		while(q < 101){
			if(q % 9 == 0){
				he = he +q;
			}
			q++;
		}

		System.out.println("he = " + he);

		System.out.println("over");

	}
}

代表语句2:

 初始化变量
 do {
     重复执行多次的语句
     更改初始化变量语句
 }while(循环条件);

先执行,后判断。次数:1-n,必会执行一次

public class Demo6{
	public static void main(String[] args){
		//输出打印1-10间的数字
		int a = 1;
		do{
			System.out.println(a);
			a++;
		}while(a < 1);

		//求和:1+2+3...+10的和
		int sum = 0;
		int b = 1;
		do{
			sum = sum + b;
			b++;
		}while(b < 11);
		System.out.println("sum = " + sum);

		//练习:打印1-50间的每个数字,计算1-50间所有的7的倍数和。
		int x = 1;
		int s = 0;
		do{
			if(x % 7 == 0){
				System.out.println("x = " + x);
				s = s +x;
			}
			x++;
		}while(x < 51);
		System.out.println("s = " + s);

		System.out.println("Hello World!");
	}
}

代表语句3:

for(初始化变量;循环条件;更改初始化变量语句){
	  重复执行多次的语句
}

先判断,后执行。次数:0-n
/*目前定义的变量都叫做局部变量:

  1. 一对大括号内 不能出现同名变量
  2. 变量必须初始化后,才可以使用
  3. 都有作用域(使用范围),只能在作用域范围内使用,出了这个范围,该变量释放 */
    \t: 相当于tab键
public class Demo7{
	public static void main(String[] args){
		//输出打印1-10间的数字
		for(int i = 1; i < 11; i++){
			System.out.println("i = " + i);
		}

		//遍历100-999间水仙花数。
		for(int i = 100; i < 1000; i++){  
			//获取百位  十位 个位上的数字
			int  a = i / 100;
			int  b = i % 100 / 10;
			int  c = i % 10;
			
			if(i == (a*a*a + b*b*b + c*c*c))
				System.out.println("水仙花数:"+i);

		}

		/*
		目前定义的变量都叫做局部变量:
		  1.一对大括号内不能出现同名变量
		  2.变量必须初始化后,才可以使用
		  3.都有作用域(使用范围),只能在作用域范围内使用,出了这个范围,该变量释放。
		*/

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值