Java入门 Day004 补充(switch语句 for循环)

1、根据输入的值,判断当前季节(分别用if语句和switch语句实现

3,4,5 属于春季
6,7,8 属于夏季
9,10,11属于秋季
12,1,2属于冬季
(考虑非法数据)

If语句:
import java.util.Scanner;
public class month
{
	public static void main(String[] args)
	{
		System.out.println("请输入月份:\n");
		Scanner in=new Scanner(System.in);
		int month;
		month=in.nextInt();
		if(month==3||month==4||month==5)
		{
			System.out.println("春季!");
		}
		else if(month==6||month==7||month==8)
		{
			System.out.println("夏季!");
		}
		else if(month==9||month==10||month==11)
		{
			System.out.println("秋季!");
		}
		else if(month==12||month==1||month==2)
		{
			System.out.println("冬季!");
		}
		else
		{
			System.out.println("你输入的月份不存在!");
		}
	}
}
//if语句另一种形态:双与&&
{
	public static void main(String[] args)
	{
		System.out.println("请输入月份:\n");
		Scanner in=new Scanner(System.in);
		int month;
		month=in.nextInt();
		if(month>=3&&month<=5)
		{
			System.out.println("你输入的月份是"+month+"月,春季!");
		}
		else if(month>=6&&month<=8)
		{
			System.out.println("你输入的月份是"+month+"月,夏季!");
		}
		else if(month>=9&&month<=11)
		{
			System.out.println("你输入的月份是"+month+"月,秋季!");
		}
		else if(month>=1&&month<=2||month==12)
		{
			System.out.println("你输入的月份是"+month+"月,冬季!");
		}
		else
		{
			System.out.println("输入的月份不存在,请重新输入!");
		}
	}
}

Switch语句:
import java.util.Scanner;
public class Test {

	public static void main(String[] args) {
	  	System.out.println("请输入月份:");
			Scanner sc=new Scanner(System.in);
			int month=sc.nextInt();

	        switch (month){
	            case 3: case 4: case 5:        //switch的穿透性
	                System.out.println("春天");
	                break;
	            case 6: case 7: case 8:
	                System.out.println("夏天");
	                break;
	            case 9: case 10: case 11:
	                System.out.println("秋天");
	            case 12: case 1: case 2:
	                System.out.println("冬天");
	                default:
	                	System.out.println("非法数据");
	                    break;
	        }
	}

}

2、for循环的格式?要能看懂执行流程。

for循环语句
for(初始化语句;条件表达式;控制体语句){
语句体;
}
执行流程:
1)执行初始化语句---->给变量进行赋值
2)判断条件表达式是否成立,如果成立,true—>执行语句体
3)执行控制体语句,----->再次判断条件是否成立,如果成立,继续执行语句体
4)一直执行到条件判断表达式不成立,即为false的时候,循环语句结束…
用for循环完成如下案例

求1-100之间的偶数和

public class Test {

	public static void main(String[] args) {
		int sum=0;
	  for(int i=1;i<=100;i++) {
		  if(i%2==0) {
			  sum+=i;
		  }
	   }
	  System.out.println(sum);
	 }
}

打印所有的水仙花数

public class No3 {
	public static void main(String[] args){	
		for(int i=100;i<999;i++){
			int a,b,c;
			//分别用a,b,c代表个位,十位,百位
			a=i%10;
			b=i/10%10;
			c=i/10/10 % 10;
			if(i==a*a*a+b*b*b+c*c*c){
				System.out.println(i);
			}
		}
	}
}

2、用while循环完成如下案例

a.求1-100之间的和

 public class No4 {
	public static void main(String[] args){	
		
		int x=1;
		int sum=0;
		while(x<=100) {
			sum+=x;
			x++;
		}
		System.out.println(sum);
	}
}

b)我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。请问,我折叠多少次,就可
以保证厚度不低于珠穆朗玛峰的高度?

**

public class Test {
	public static void main(String[] args) {
		double h2=0.01;
		int count=0;
		while(8848>h2) {
			h2*=2;
			count++;
		}
		System.out.println(count++);
	 }
}

结果:20次

**

3、键盘录入x的值,计算出y的并输出。

x和y的关系满足如下:
x>=3 y = 2x + 1;
-1<=x<3 y = 2x;
x<=-1 y = 2x – 1;

import java.util.Scanner;
  class No3{
     public static void main(String[] args){
        int y = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数:");
        int x = sc.nextInt();
       if(x >= 3)
       {
          y = 2 * x + 1;
          System.out.println("y=2*"+x+"+1="+y);
       }
       else if(x >= -1 && x < 3)
       {
          y = 2 * x;
          System.out.println("y=2*"+x+"="+y);
       }
       else
       {
          y = 2 * x - 1;
          System.out.println("y=2*"+x+"-1="+y);
       }
    }
 }

**

4、键盘录入数据,模拟单项选择题(注意:键盘录入的时候,Scanner不能录入字符—>可以考虑把A看成 65)

	下面的人物喜欢谁?
		A:马伊琍
		B:马苏
		C:高圆圆("假设为正确答案")
		D:林志玲**
import java.util.Scanner;
public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in) ;
		
		System.out.println("请输入一个数:  ") ;
		int a = sc.nextInt() ;
		char ch=(char)a;
		if(ch=='A') {
			System.out.println("马伊琍");
		}
		if(ch=='B') {
			System.out.println("马苏");
		}
		if(ch=='C') {
			System.out.println("高圆圆");
		}
		if(ch=='D') {
			System.out.println("林志玲");
		}		
	}
}

打印九九乘法表

public class Test {

	public static void main(String[] args) {
	
           for(int i=1;i<10;i++) {//一个控制行
        	   for(int j=1;j<=i;j++) {//一个控制列
        		   System.out.print(j+"X"+i+"="+i*j+"\t");
        		   
        	   }
        	   System.out.println( );
           }
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值