作用:猜数字游戏。随机产生1个数字(1~10),大了、小了或者成功后给出提示。
语言:java
工具:eclipse
作者:潇洒鸿图
时间:2016.11.10
>>>>>>>>>>>>>>>>>>>>
代码详情:
1 package homework1129; 2 3 import java.util.Scanner; 4 5 /* 6 *7、编写类 GussN ,用do 、、while语句开发猜数字游戏,大了、小了或者成功后给出提示。 7 */ 8 public class Seventh { 9 //全局变量: 10 11 static Scanner scan = new Scanner(System.in); 12 static int[] arr=new int[1];//数组arr 13 static int guess; 14 15 //随机数产生 方法: 16 public static void arr(){ 17 for(int i=0;i<arr.length;i++){ 18 int a= (int)(Math.random()*10);//产生随机数 19 arr[i]=a; 20 System.out.print(arr[i]+" "); } 21 22 } 23 24 //GussN游戏方法; 25 public static void GussN(){ 26 do{ 27 arr(); 28 System.out.println("猜吧!"); 29 //定义变量,把从控制台输入的对象复制给guess 30 guess = scan.nextInt(); 31 if(guess<arr[0]){System.out.println("小了,再来!!");} 32 if(guess>arr[0]){System.out.println("大了,再来!!");} 33 //if(!(guess==arr[0])){System.out.println("猜错了!再来!!");} 34 if(guess==arr[0]){System.out.println("恭喜你!猜对了!");} 35 } 36 37 while(!(guess==arr[0])); 38 } 39 public static void main(String[] args) { 40 GussN(); 41 } 42 }