类的成员变量: 

猜数字游戏:一个类A有一个成员变量v,通过随机产生一个100内的整数给v赋值。定义一个方法,对A类的成员变量v进行猜。   没有猜对的情况下提示如果大了则提示大了,小了则提示小了,并且一直猜知道猜对为止,统计猜的次数。猜成功了提示一共猜了几

package MXDX01;
import java.util.Random;
import java.util.Scanner;
public class MXDX01 {
	public static void main(String[] args) {
		A a=new A();
		a.cai();
	}
}

//-----------------------类A-----------------
class A{
	Random rd=new Random();
	int V=rd.nextInt(100);
	int count=0;
	void cai(){
	while(true){
		count++;
		System.out.println("输入你猜测的数1-100");
		Scanner sc=new Scanner(System.in);
		int temp=sc.nextInt();
		if(temp==V){
			System.out.println("猜对了,猜了"+count+"次");
			break;
		}
		else if(temp<V){
			System.out.println("猜小了");
		}
		else{
			System.out.println("猜大了");
		}	
	}
	}
}

 

次。

相关文章:

  • 2021-06-08
  • 2021-06-22
  • 2019-08-07
  • 2019-08-07
  • 2021-08-01
  • 2021-09-16
  • 2022-02-15
  • 2021-04-23
猜你喜欢
  • 2022-12-23
  • 2021-10-14
  • 2021-12-03
  • 2022-01-11
  • 2021-05-05
  • 2021-07-21
  • 2021-10-30
相关资源
相似解决方案