51,411
社区成员
发帖
与我相关
我的任务
分享
一个猜数字的小程序,总是提示无法从静态上下文中引用非静态,求各位大佬指点一下
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("小了");
}
}*/
new一下对象,很难吗
你这个遍体鳞伤啊。
Scanner scanner = new Scanner(System.in);
int result = scanner.nextInt();