Java自学Record(四)

Java的键盘输入+几种循环

特别强调:如果不用编译器编译,用cmd编译时,cmd是不支持中文的,所以就算是字符串,用中文也会报错。最好用英文。

强调:在线编译器不能实现输入,请安装JDK完成。

//如何从键盘获取不同类型值的变量输入Scanner//
//重点:导包:import java.util.Scanner;//
import java.util.Scanner;
class ScannerTest{
    public static void main(String args[]) {
	   Scanner scan = new Scanner(System.in);//实例化格式//
		int num = scan.nextInt();
		System.out.println(num);
	}
}

while循环
为什么输出位置是+ x ;因为前面是字符串,起连接作用

class TestWhile {
   public static void main(String args[]) {
      int x = 1;
      while( x < 7 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

do-while循环

class TestDowhile {
   public static void main(String args[]){
      int x = 1;
      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 7 );
   }
}

for循环

class TestFor {
   public static void main(String args[]) {
 
      for(int x = 1; x < 7; x++) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

for增强forbuff

class TestForbuff {
   public static void main(String args[]){
      int [] numbers = {6, 7, 8, 9, 10};
 
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"安其拉", "亚瑟", "牛魔", "高渐离"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
  }

break

class TestBreak {
   public static void main(String args[]) {
      int [] numbers = {6, 7, 8, 9, 10};
 
      for(int x : numbers ) {
         if( x ==  9) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

countinue

class TestCountinue {
   public static void main(String args[]) {
      int [] numbers = {6, 7, 8, 9, 10};
      for(int x : numbers ) {
         if( x == 9 ) {
        continue;//在 for 循环中,continue 语句使程序立即跳转到更新语句。
				//在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。 
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值