java学习笔记3

1.创建引用类型变量公式;

    数据类型 变量名 =  new  数据类型(); 

    调用包内方法:变量名.方法名();

2.Scanner类的使用 :

//声明调用util包中的Scanner类
import java.util.Scanner;
public class ScannerEx{
    public static void main(String[] args){
          //创建并初始化Scanner 对象 scan;
        Scanner scan=new Scanner(System.in);
          //定义变量接收用户输入的数字;
           System.out.print("请输入整数");
           int i=scan.nextInt();
           System.out.println("您输入的数字为"+i);
          //next()方法接收用户输入的字符串;
           System.out.print("请输入字符串:");
           String s=scan.next();
           System.out.println("您输入的字符串为"+s);
    }
}        

3.Random随机数类的使用:

 1 import java.util.Random;
 2 public class RandomEx{
 3    public static void main(String[] args){
 4       //声明创建并初始化Random 对象ran;
 5       Random ran=new Random();
 6       //定义变量接收随机数对象
 7       int num=ran.nextInt(100);//nextInt();方法内的参数代表    随机数的取值范围 
 8       //控制台打印随机数
 9          System.out.println("本次随机数为"+num);
10       //随机数还可以使用的是Math.random()方法;该方法取得的值为0-1之间的随机数
11    }    
12  }      

4.if语句

  if语句用法:

    if(判断条件){条件为真执行本句表达式}else{ 条件为假时执行该语句};

5.if语句与三元运算符互换

public class IfandSanyuan{
  public static void main(String[] args){
    int i=6;
    int j=66;
    if(i>j){
      System.out.println("最大值为"+i);
    }else{
      System.out.println("最大值为"+j);    
    }
    /*三元运算符:(判断语句)?(表达式1):(表达式2)
    如果判断语句为真,则执行表达式1,否则执行表达式2;*/
    //转换为三元运算符后为
    int m=i>j?i:j;
      System.out.println("最大值为"+m);
  }  
}

6.while循环

public class WhileEx{
  public static void main(String[] args){
    /*while循环:
    条件初始化 
    while(条件){//条件为真,执行括号内内容,条件为假,结束循环
      表达式;
      增量; //增量使变量向着循环结束的方向发展
    }
    */
    int i=1;
    while(i<=5){
      System.out.println(i);
      i++;
    }
  }
}

7.for循环

  for循环编写格式

  for(初始化变量;条件;增量){

    执行语句;//条件为真时,执行语句,条件为假时,结束循环

  }

8.do  while()循环

  do while()循环编写格式;

  初始化变量;

  do{

    表达式;

       }while(条件);

  while循环与do while循环的区别?

    while循环先判断条件是否为真,为真则执行循环体,否则结束循环。do while 循环先执行循环体一次,在判断条件是否为真,为真则继续循环,为假结束循环,

      do while循环比while循环多执行一次循环体;

9.嵌套for循环

 1 public class FortoFor{
 2   public static void main(String[] args){
 3         /* for(变量初始化1;条件1;增量1){
 4                 for(变量初始化2;条件2;增量2){
 5                  执行循环体;
 6                 }
 7             }   循环体执行次数为变量1*变量2;*/  
 8         //打印乘法口诀表
 9         for(int i=1;i<10;i++){
10             for(int j=1;j<=i;j++){
11                 System.out.print(i+"*"+j+"="+i*j+"	");
12             }
13             System.out.println();  
14         }    
15     }
16 }

10.break语句

  终止循环作用;跳出循环语句

 1 public class BreakEx{
 2   public static void main(String[] args){
 3     //编写循环,打印1到10的数
 4     for(int i=1;i<=10;i++){
 5       System.out.println(i);
 6       if(i==5){
 7       break;//当i=5时,结束循环
 8       }
 9     }
10   }
11 }

11.continue语句

  结束本次循环;

public class ContinueEx{
  public static void main(String[] args){
      for(int i=1;i<=10;i++){
            if(i==5){
                continue;//当i=5时,跳出本次循环,继续下一次循环;
            }
            System.out.println(i);
      }
  }
}
原文地址:https://www.cnblogs.com/Zs-book1/p/10496331.html