[Java] 纯文本查看 复制代码 import java.util.Scanner;//引入Scanner包
import java.util.Random;//引入Random包
public class GuessGame{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);//创建Scanner对象
Random r = new Random();//创建Random对象
int arr[] = new int[3]; //创建结算数组
int sun_game = 0; //总次数记录值
for(int pan =0;pan<3;pan++ ){ //三盘为一局
System.out.println("请输入1-100以内的数字进行猜测");
System.out.println("这是第"+ (pan + 1) +"盘游戏");
int number = r.nextInt(100);
int i = 1;
//System.out.println(number);
while(true){
System.out.println("猜数字游戏开始请输入这个数字");
int You_number = sc.nextInt();
if(You_number==number){
System.out.println("恭喜您答对了");
System.out.println("您一共猜了"+ i +"次,猜对好棒哦~");
arr[pan] = i;//将每盘成绩录入数组
break;
}
else if(You_number < number){
System.out.println("您猜的数字小咯");
i++;
}
else if (You_number > number){
System.out.println("您猜的数字大咯");
i++;
}
else{
System.out.println("您猜的根本不是数字");
i++;
}
System.out.println("当前猜的第"+i+"次");
}
sun_game += arr[pan]; //总次数结算
}
int ci = sun_game/3;
System.out.println("系统结算:您总共猜了"+ sun_game +"次,平均每盘猜"+ ci +"次");
}
} |