java中引用数据类型Scanner类和Random类的使用-0509

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中引用数据类型Scanner类和Random类的使用-0509相关的知识,希望对你有一定的参考价值。

import java.util.Scanner;
class Demo02 
{
    public static void main(String[] args) 
    {
        //导包
        //创建该类对象
        Scanner sc = new Scanner(System.in);
        //用该对象调用类里面的方法
        //int i = sc.nextInt();
        //String str = sc.next();
        Double i = sc.nextDouble();
        System.out.println("你输入的内容为:"+i);
    }
}
import java.util.Scanner;
class Demo04 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        /*int num = sc.nextInt();
        if(num%2==0){
           System.out.println("偶数");
        }else{
           System.out.println("奇数");
        }
        if(num<60){
           System.out.println("不及格");
        }else if(num<=70){
           System.out.println("良好");
        }else if(num<=80){
           System.out.println("中等");
        }else{
           System.out.println("优秀");
        }*/
        System.out.println("请输入");
        int a = sc.nextInt();
        System.out.println("请再次输入");
        int b = sc.nextInt();
        int max = 0;
        max = a>b?a:b;
        System.out.println("最大值为:"+max);
    }
}
import java.util.Random;
class Demo03 
{
    public static void main(String[] args) 
    {
        //导包
        //创建对象
        Random r = new Random();
        //调用方法,并定义一个变量去接收
        int sjs = r.nextInt(10);
        double sjxs = r.nextDouble();
        System.out.println(sjs);
        System.out.println(sjxs);
    }
}
import java.util.Random;
class dianming 
{
    public static void main(String[] args) 
    {
        Random r = new Random();
        int sz = r.nextInt(8);
        if(sz==0){
            System.out.println("董斌");
    }
         else if(sz==1){
             System.out.println("张锐");
     }
         else if(sz==2){
              System.out.println("王荣臣");
         }
         else if(sz==3){
              System.out.println("韩凯");
         }
         else if(sz==4){
               System.out.println("张禄");    
                   }
         else if(sz==5){
               System.out.println("刘继勋");
         }
         else if(sz==6){
               System.out.println("张维");
         }
         else{
               System.out.println("魏子豪");
         }
         }
         }
import java.util.Random;
class szdianming 
{
    public static void main(String[] args) 
    {
        Random r = new Random();
        int i = r.nextInt(8);
        String[] arr = {"董斌","张锐","王荣臣","韩凯","张禄","刘继勋","张维","魏子豪"};
        System.out.println(arr[i]);
    }
}

 

以上是关于java中引用数据类型Scanner类和Random类的使用-0509的主要内容,如果未能解决你的问题,请参考以下文章

Scanner类匿名对象Random类String类static静态类math类和Arrays工具类

java基础---12. scanner

java之常用API

java中引用数据类型基础(Scanner. Random)

java引用数据类型之Scanner与Random

java第3天:Scanner,Random,ArrayList