java初学者题目,求详细解答
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class TestNumber {
private A a;
public void setA(A a) {
this.a = a;
}
public A getA() {
return a;
}
public boolean judge(int num) {
if (a.getV() > num) {
System.out.println("小了");
return false;
}
if (a.getV() < num) {
System.out.println("大了");
return false;
} else {
System.out.println("猜对了");
return true;
}
}
public static void main(String[] args) {
TestNumber t = new TestNumber();
A a = new A(new Random().nextInt(100));
t.setA(a);
BufferedReader str = new BufferedReader(
new InputStreamReader(System.in));
while (true) {
System.out.println("请输入要猜的数:");
try {
if (t.judge(Integer.parseInt(str.readLine()))) {
break;
}
} catch (Exception e) {
System.out.println("输入错误请重新输入:");
}
}
}
}
class A {
private int v = 100;
public A(int v) {
this.setV(v);
}
public A() {
}
public void setV(int v) {
this.v = v;
}
public int getV() {
return v;
}
}