有什么不足的还望大家多多指教哈!!!
NO.1
package cn.com.hrbust.Test;
import java.io.IOException;
public class GussNumbet {
public static void main(String[]args) throws IOException {
int guss = (int)(Math.random()*21);
int input = 0;
int ch ;
int myguss = 0;
int times = 0;
boolean isEnd = false;
while(true) {
ch = (int)System.in.read();
switch (ch) {
case '\r':
myguss = input/10;
input = 0;
break;
case '\n':
if(times<4) {
if(myguss>guss) {
System.out.println("guess larger!");
times++;
}
if(myguss<guss) {
System.out.println("guess smaller!");
times++;
}
if(myguss==guss){
System.out.println("guess right!");
isEnd=true;
break;
}
}
if(times==4&&myguss!=guss) {
System.out.println("您的机会已经用完了,所猜数字为:"+guss);
isEnd=true;
}
if(isEnd) {
times=0;
guss = (int)Math.random()*21;
isEnd=false;
}
myguss=0;
break;
default:
input += (ch-'0');
input*=10;
break;
}
}
}
}
No.2
package cn.com.hrbust.Test;
import java.util.Scanner;
public class GuessNumber1 {
static int i = 0;
public static void main(String[]args) {
int guess = 0;
int random = 0;
random = (int)(Math.random()*21);
doit(guess,random);
if(i>2) {
System.out.println("Sorry! Your chance is used out!");
System.out.println();
System.out.println("****************************");
System.out.println("如果您向继续游戏,请按 1 回车!");
System.out.println("****************************");
System.out.println();
if(1==choice()) {
doit(guess,random);
}
System.out.println("Sorry,you are lost!!!");
}
}
public static int choice() {
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
public static void doit(int guess,int random) {
for(i=0;i<3;i++) {
System.out.println("请输入您猜的数: ");
Scanner sc = new Scanner(System.in);
guess = sc.nextInt();
if(guess==random) {
System.out.println("guess right! Congratulations to you!!! ");
System.out.println();
break;
}
if(guess<random) {
System.out.println("guess smaller!");
System.out.println();
continue;
}
if(guess>random) {
System.out.println("guess larger!");
System.out.println();
continue;
}
}
}
}