Java教程

猜数字大小

本文主要是介绍猜数字大小,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

public class Work{
public static void main(String[] args){
A a = new A(100);
B b = new B(a);
java.util.Scanner s = new java.util.Scanner(System.in);
while (true){
System.out.print("请输入要猜测的数字:");
int k = s.nextInt();
b.cai(k);
}
}
}
class A{
private int v;
public A(){
}
public A(int v){
this.v = v;
}
public void setV(int v){
this.v = v;
}
public int getV(){
return v;
}
}
class B{
private A a;
public B(){}
public B(A a){
this.a = a;
}
public void setA(A a){
this.a = a;
}
public A getA(){
return a;
}
public void cai(int number){
int num = a.getV();
if (number == num){
System.out.println("猜测成功");
System.exit(0);
}else if(number > num){
System.out.println("猜大了");
}else{
System.out.println("猜小了");
}
}
}

这篇关于猜数字大小的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!