java: 无法从静态上下文中引用非静态 方法 nextInt()

爱上喜多川海梦 2021-11-11 22:04:25

一个猜数字的小程序,总是提示无法从静态上下文中引用非静态,求各位大佬指点一下

...全文
385 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zreo_ZYF 2021-11-18
  • 打赏
  • 举报
回复 1

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int nextInt = sc.nextInt();
        //只能猜一次
        if (true) {
            System.out.println("你猜的数字:");
            int result = sc.nextInt();
            if (result == nextInt) {
                System.out.println("猜对了");
            } else if (result > nextInt) {
                System.out.println("大了");
            } else {
                System.out.println("小了");
            }
        }
        /*
        //优化 可多次输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int nextInt = sc.nextInt();
        int result = 0;
        //循环多次输入,猜对了结束,猜错了可以重新输入
        while (true){
            System.out.println("你猜的数字:");
            result = sc.nextInt();
            if (result == nextInt) {
                System.out.println("猜对了");
                //退出循环
                break;
            } else if (result > nextInt) {
                System.out.println("大了");
            } else {
                System.out.println("小了");
            }
        }*/
老王就是我 2021-11-13
  • 打赏
  • 举报
回复

new一下对象,很难吗

比特灵 2021-11-12
  • 打赏
  • 举报
回复 1

你这个遍体鳞伤啊。

Scanner scanner = new Scanner(System.in);
int result = scanner.nextInt();

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧