JAVA 请教一个小问题 在线等~~~

来源:百度知道 编辑:UC知道 时间:2024/06/23 19:50:36
请在下面这个小程序的基础上改动下, 这是个猜数字的小程序,改成每次运行时的答案是随机的(在0-20之间),期待达人帮助解决。本人刚学if else语句,比较喜欢这样简单的, 就用点简单的语句吧, 谢谢啦

import java.util.Scanner;
public class XMX{
public static void main(String[] args){
boolean correct=false;
while(correct==false){
Scanner input = new Scanner(System.in);
System.out.print("请输入商品价[0-20]:");
int A = input.nextInt();
if( ){System.out.println("higher");
} else if( ){System.out.println("you made it!!!");
} else if( ){System.out.println("lower");
}
}
}
}

package test;

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

public class Test {
public static void main(String[] args) {
Random temp = new Random();
int result = temp.nextInt(21);
System.out.println("正确答案是:"+result);
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("请输入商品价[0-20]:");
int a = input.nextInt();
if (a>result && a<=20) {
System.out.println("higher");
} else if (a==result) {
System.out.println("you made it!!!");
break;
} else if (a<result && a>=0) {
System.out.println("lower");
} else{
System.out.println("输入数字应在0-20之间!");
}
}
}
}

没记错的话昨天给你写了。楼上按你的改了,但这么写很不好
1)变量不要用大写字母A
2)boolean变量correct没有实际意义
3)可以输入0-20以外的数字
4)可以输入字母之类的非数字(这个昨天给你处理了)
5)把Scanner input