java初学者题目,求详细解答
发布网友
发布时间:2022-05-06 12:07
我来回答
共3个回答
热心网友
时间:2022-06-30 11:44
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;
}
}
热心网友
时间:2022-06-30 11:45
以下是标准答案:
import javax.swing.JOptionPane;
public class Text {
public static void main(String[] args) {
int number=(int)(Math.random()*100);
String num;
num=JOptionPane.showInputDialog(null," 请猜计算机输出的数:(0~100)");
int i=Integer.parseInt(num);
while(i!=number)
{
if(i>number)
JOptionPane.showMessageDialog(null,"输入的数值大了");
else
{
JOptionPane.showMessageDialog(null,"输入的数值小了");
}
num=JOptionPane.showInputDialog(null," 请猜计算机输出的数:(0~100)");
i = Integer.parseInt(num);
}
System.out.println("恭喜你猜对了!");
}
}
热心网友
时间:2022-06-30 11:45
在B.java 的文件中:
A{
public int v = 100;
}
public B{
public static void main(String[] args){
int temp = new A().v;
Scanner scan = new Scanner(system.in);
while(true){
int v = scan.nextInt();
if(v>temp){
System.ou.println("输入值大了");
continue;
}else if(v<temp){
System.ou.println("输入值小了");
continue;
}else{
System.ou.println("你猜对了!NB!");
break;
}
}
}
}
导包自己导下就OK;欢迎追问。