java学习笔记(第四章)

package practice;
import java.util.*;


public class 第四章 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String a;
		//a = in.next();
		//System.out.println(a);
		int x=20; {
			int y=40;
			System.out.println(y);
			int z=245;
			boolean b; {
				b=y>z;
			}
		}
		String word = "hello java";
		System.out.println(word);
		//System.out.println(b);这一句是错误的  因为b在上面的复合语句中
		int c=100;
		if(c==100)
			System.out.println(666);
		boolean d = false;
		if(!d) {
			System.out.println("d=false");
		}
		int xx=1;
		int yy;
		yy= xx<0?xx:-xx;
		System.out.println(yy);
		//switch:
		String str="abcd";
		switch(str) {
			case "ab":
				System.out.println("java");
			case "cd":
				System.out.println("good");
			default:
				System.out.println("none");//输出none 因为必须是连续的字符串才行
		}
		int day=in.nextInt();
		switch(day) {
			case 1:
				System.out.println("Monday");
				break;
			case 2:
				System.out.println("Tuesday");
				break;
			case 3:
				System.out.println("Wednesday");
				break;
			case 4:
				System.out.println("Thursday");
				break;
			case 5:
				System.out.println("Firday");
				break;
			case 6:
				System.out.println("Satursday");
				break;
			case 7:
				System.out.println("Sunday");
				break;
			default:
				System.out.println("I dont know!");
		}
		//while
		int s=3;
		while(s>0) {
			System.out.println("233");
			s--;
		}
		int f=100;
		do {
			System.out.println("ok");
			f--;
		}while(f==60);//先执行一次再进行判断,所以只执行一次
	//foreach
	int arr[]= {7,10,1};
	for(int i :arr) {
		System.out.println(i);
	}//遍历完整个数组推出循环
	//标签功能
	loop:for(int i=0;i<2;i++)
			for(int j=0;j<6;j++) {
				if(j==4) {
					break loop;
	  }
				System.out.println("i="+i+" j="+j);//j=4的话就退出大循环(loop)
	}
	
  }
}

新增东西:

1.新增了一个复合语句,规定了变量的生命周期 eg:

package practice;
import java.util.*;


public class 第四章 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int x=20; 
		{
			int y=40;
			System.out.println(y);
			int z=245;
			boolean b; 
			{
				b=y>z;
			}
		}
		String word = "hello java";
		System.out.println(word);
		//System.out.println(b);这一句是错误的  因为b在上面的复合语句中
		}
}

这里的b,y,z都在主函数里面的另外的一个复合语句中,如果直接在主函数中调用会错误
我也不知道java有这东西有啥用,可能我还没有遇到有用的地方8

2.for循环的标签

//标签功能
	loop:for(int i=0;i<2;i++)
			for(int j=0;j<6;j++) 
			{
				if(j==4)
				 {
					break loop;
				 }
				System.out.println("i="+i+" j="+j);//j=4的话就退出大循环(loop)
	  		}

/

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int count = 0;
		zaiZheTingDun:
			for (int i = 1; i <= 10; i++) {
				for (int j = 1; j <= 10; j++) {
					if (i == 7 && j == 8)
						break zaiZheTingDun;
					count++;
				}
			}
		System.out.print(count + ", Done.");
		in.close();
	}
}

暂时就这么多2333

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值