• 匿名对象


    1 匿名对像:new 类名称();  对象只有右边的对象,没有左边的名字和运算符;
    2 匿名对象可以作为参数的返回值
    import java.util.Scanner;
    
    public class Anonymous {
        public static void main(String[] args){
            //普通使用方式
    //        Scanner sc =new Scanner(System.in);
    //        int num  = sc.nextInt();
            //匿名对象的方式
        //        
        //        int num = new Scanner(System.in).nextInt();
        //        System.out.println("输入的的是"+num);
    
        //使用一般写法传入参数
    //        Scanner sc = new Scanner(System.in);
    //        methodParam(sc);
    //        
            //使用匿名对象进行传参数
    //        methodParam(new Scanner(System.in));
    //    }
            Scanner sc = methodReturn();
            int a=sc.nextInt();
            System.out.println("输入的是"+a);
        }
        private static void methodParam(Scanner sc) {
            // TODO Auto-generated method stub
            int num=    sc.nextInt();
            System.out.println("输入的是"+num);
            
        }
        public static Scanner methodReturn(){
    //        Scanner sc = new Scanner(System.in);
    //        return sc;//普通返回
            //匿名返回
            return new Scanner(System.in);
        }
    
    }

    import
    java.util.Random; import java.util.Scanner; /* * 题目: * 用代码模拟猜数字小游戏 * * 思路:首先要产生一个随机数字,并且一旦产生不会变化 * 2需要键盘输入Scanner * 3获取输入的数字Scanner当中的nextInt方法 * 4已得到俩数字,判断一下if: * 若太大重试,提示太大了 * 若太小,提示太小,并且重试 * 如果猜中,游戏结束 * 5,重试就是在来一次,循环,用while(true) */ public class RandomGame { public static void main(String[] args){ Random ran =new Random(); int randomNum = ran.nextInt(100)+1;//范围1到100【1,100】闭区间 System.out.println("请输入你猜测的数字"); Scanner scan = new Scanner(System.in); int x = scan.nextInt(); while(true){ if(x>randomNum){ System.out.println("请重新输入,太大了"); x=scan.nextInt(); }else if(x<randomNum){ System.out.println("请重新输入,太小了"); x=scan.nextInt(); }else{ System.out.println("恭喜你猜对了"); break;//如果不让重复循环猜,那就用for循环哈哈 } } } }
  • 相关阅读:
    Oracle数据库用户密码设为无限期
    CentOS 7设置网卡开机自动启用
    求凹多边形的视觉中心,不是质心、重心
    autocad数据交换格式dxf读取
    gis资源站
    geotools的空间索引使用——R树和四叉树
    JTS的泰森多边形
    Geotools的delaunry三角剖分
    geotools的最短路径实现
    java多线程
  • 原文地址:https://www.cnblogs.com/dream2060/p/11788843.html
Copyright © 2020-2023  润新知