JAVA——循环语句、条件语句

一、循环结构:

Java中有三种主要的循环结构:

  • while 循环
  • do…while 循环
  • for 循环
1. while 循环
public class Test {
   public static void main(String args[]) {
      int x = 10;
      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
2. do…while循环
public class Test {
   public static void main(String args[]){
      int x = 10;
 
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}
while 与do …while区别

while:如果不满足条件,则不能进入循环。
do…while:至少会执行一次。

3. for循环
public class Test {
   public static void main(String args[]) {
 	//最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
      for(int x = 10; x < 20; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}
for 和 while区别

for循环的初始化变量,在循环结束后,不可以被访问。
while循环的初始化变量,是可以被继续使用的。
如果初始化变量,后面还要继续访问,就使用while,否则,推荐使用for。

for(int x=1; x<=10; x++){
			System.out.println("爱生活,爱Java");
		}
		//这里的x无法继续访问
		//System.out.println(x);
		System.out.println("-----------------");	
		int y = 1;
		while(y<=10) {
			System.out.println("爱生活,爱Java");
			y++;
		}
		System.out.println(y);
	}
【补充】Java 增强 for 循环
/**
*for(声明语句 : 表达式){
	//代码句子
}
//声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
**/
public class Test {
public static void main(String args[]){
   int [] numbers = {10, 20, 30, 40, 50}; 
     for(int x : numbers ){
       System.out.print( x );
       System.out.print(",");
    }
   System.out.print("\n");
    String [] names ={"James", "Larry", "Tom", "Lacy"};
   for( String name : names ) {
     System.out.print( name );
    System.out.print(",");
    }
  }
}
终止循环关键字
1.break

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块
break 跳出最里层的循环,并且继续执行该循环下面的语句。

2.continue

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

二、条件语句

1.if…else
public class Test {
   public static void main(String args[]){
      int x = 30;
 
      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}
2.switch case
switch(expression){
    case value1 :
       //语句
       break; //可选
    case value2 :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}
switch(expression){
    case value1 :
    case value2 :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

【注】

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。
    从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
    当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
switch和if…else

所有switch可用if…else代替,但不会所有if…else可switch代替
switch效率高,能用的地方一定要用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你介绍一下如何使用 Java 编写程序实现文件拷贝。 Java 提供了非常方便的 I/O 类来处理文件读写操作。文件拷贝的基本思路是读取源文件中的数据,然后将数据写入到目标文件中。下面是一个简单的文件拷贝程序示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) throws IOException { // 源文件路径 String srcFilePath = "C:/temp/source.txt"; // 目标文件路径 String destFilePath = "C:/temp/dest.txt"; // 创建输入流和输出流 FileInputStream fis = new FileInputStream(srcFilePath); FileOutputStream fos = new FileOutputStream(destFilePath); // 定义缓冲区,每次读取 1024 个字节 byte[] buffer = new byte[1024]; int len; // 循环读取源文件中的数据,并将数据写入到目标文件中 while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } // 关闭输入流和输出流 fis.close(); fos.close(); System.out.println("文件拷贝完成!"); } } ``` 在上面的示例中,我们首先定义了源文件路径和目标文件路径。然后创建了输入流和输出流,用于读取源文件和向目标文件写入数据。我们定义了一个缓冲区,每次从输入流中读取 1024 个字节,并将这些字节写入到输出流中。最后,我们关闭输入流和输出流,并输出一条完成信息。 需要注意的是,上面的代码中使用了 try-catch 语句来捕获可能出现的 IOException 异常。这是因为在文件读写过程中可能会出现异常,比如文件不存在、文件无法读取等等。为了保证程序的健壮性,我们需要使用 try-catch 语句来处理这些异常。 希望这个简单的示例可以帮助你了解如何使用 Java 编写文件拷贝程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值