java 使用 引用数据类型(以Scanner、Random模块为例)

 
创建一个新变量
类型      变量名  =     new 类型()
举个例子:
Scanner sc = new Scaner()

使用引用数据类型中的功能;

变量.功能名字()


  • Scanner类:接受键盘输入

    1、导入包-指定包所在的文件夹
    2、公式,创建Scanner类型变量
    3、使用
    

import java.util.Scanner;

public class Scan{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        System.out.println(i);
    }
}

import java.util.Scanner;

public class Scan{
    public static void main(String[] args){
        //Scanner sc = new Scanner(System.in);
        //int i = sc.nextInt();
        //System.out.println(i);

        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        System.out.println(s);
    }
}

  • random模块

    import java.util.Random;

    public class RandomDemo{ public static void main(String[] args){ Random ran = new Random(); //create a num. between 1-100,not contain 100; int i = ran.nextInt(100); System.out.println(i);

        double d = ran.nextDouble();
        System.out.println(d);
    }
    

    }


if 语句

/*
    if(条件){
        执行动作
    }
*/

public class ifDemo{
    public static void main(String[] args){
        int i = 5;
        if (i == 5){
            System.out.println("true");
            i++;
        }
        System.out.println(i);
    }
}

if ... else

/*
    if(条件){
        执行动作1
    }else{
        执行动作2
    }

*/

public class ifelseDemo{
    public static void main(String[] args){
        int i = 17;
        if (i % 2 == 0){
            System.out.println(i + "0");        
        }else{
            System.out.println(i + "1");
        }       
    }
}

if ..else if..else if..else,可以有无限的else if

if(condition){
    do_something
}else if{
    do_something
}else if{
    do_something
}else{
    do_something
}

三元运算符

//判断两个数值的大小,并且将大一点的数值赋给x并且输出
public class ifelseDemo1{
    public static void main(String[] args){
        int i = 5;
        int j = 9;
        int x = i>j? i:j;
        //如果条件满足就输出i,否则输出j
        System.out.println(x);
    }
}

while 循环

while (条件){
    循环体
}

for循环

/*
for (初始化变量;条件;增量){
    循环体
}
*/

for循环例子:

public class forDemo{
    public static void main(String[] args){
        for(int i = 1;i<11;i++)
            System.out.println(i);
    }
}

do..while...

特点是:先执行一次在判断条件是不是成立;

public class dowhileDemo{
    public static void main(String[] args){
        int i = 1;
        do{
            System.out.println(i);
            i++;
        }while(i<10);
    }
}

死循环:没有定义退出条件,一直循环

while(true){

}


for (;;){

}

嵌套循环

总循环次数=外循环次数*循环次数

例子:用for嵌套循环打印一个直角三角形

public class loop{
    public static void main(String[] args){
        for(int j = 1;j<=10;j++){
            for(int i=1;i<=j;i++){
                System.out.print("*");  
            }
            System.out.print("\n");

        }
    }

}

break 可以用于结束当期循环,也可以在循环前加 a: 相当于给该循环起了一个名字a,在内部循环可以是用break a;

练习:

做一个猜字游戏

import java.util.Random;
import java.util.Scanner;

public class guessNum{
    public static void main(String[] args){
        Random ran = new Random();
        int r = ran.nextInt(31);
        //提前将随机到到的数值打印出来便于测试
        System.out.println(r);
        Scanner sc = new Scanner(System.in);
        while(true){            
            int s = sc.nextInt();
            //判断当预先随机的数值和用户输入的数值相等时候,就退出循环
            if(s==r){
                System.out.println("good..");
                break;
            }
        }


    }   
}

转载于:https://www.cnblogs.com/chenadong/p/9624556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值