Last active
December 15, 2015 17:48
-
-
Save precious-ming/5298575 to your computer and use it in GitHub Desktop.
猜数字(1.用户猜数字。2.电脑猜数字)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GuessNums { | |
public static void main(String[] args){ | |
int[] nums = new int[100]; | |
for(int i = 0;i < nums.length;i++){ | |
nums[i] = i; | |
} | |
java.util.Random random = new java.util.Random(); | |
int index = random.nextInt(nums.length); | |
//System.out.println(nums[index]); | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
System.out.println("请输入您猜的数字(0-99)"); | |
int guess = input.nextInt(); | |
while(guess >=0 && guess <= 99){ | |
if(guess < nums[index]){ | |
System.out.println("您输入的数太小了"); | |
System.out.println("请换一个大点的数字输入(0-99)"); | |
guess = input.nextInt(); | |
}else if(guess > nums[index]){ | |
System.out.println("您输入的数太大了"); | |
System.out.println("请换一个小点的数字输入(0-99)"); | |
guess = input.nextInt(); | |
}else if(guess == nums[index]){ | |
System.out.println("恭喜你猜对了"); | |
break; | |
} | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GuessNums2 { | |
public static void main(String[] args){ | |
int[] nums = new int[100]; | |
for(int i = 0;i < nums.length;i++){ | |
nums[i] = i; | |
} | |
java.util.Random random = new java.util.Random(); | |
int index = random.nextInt(nums.length); | |
System.out.println("电脑随机抽取的数:"+nums[index]); | |
int index2 = random.nextInt(nums.length); | |
//System.out.println("电脑随机猜的数:"+nums[index2]); | |
int guess = nums[index2]; | |
while(guess >=0 && guess <= 99){ | |
int index3 = random.nextInt(nums.length); | |
if(guess < nums[index]){ | |
System.out.println("电脑随机猜的数:"+guess); | |
System.out.println("您输入的数太小了"); | |
System.out.println("请换一个大点的数字输入(0-99)"); | |
guess = nums[index3]; | |
}else if(guess > nums[index]){ | |
System.out.println("电脑随机猜的数:"+guess); | |
System.out.println("您输入的数太大了"); | |
System.out.println("请换一个小点的数字输入(0-99)"); | |
guess = nums[index3]; | |
}else if(guess == nums[index]){ | |
System.out.println("电脑随机猜的数:"+guess); | |
System.out.println("恭喜你猜对了"); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment