用java 语言写个猜数字游戏会的帮下谢谢

2024-11-15 06:56:46
推荐回答(2个)
回答(1):

给个简单的:
import java.util.Scanner;
public class GuessNumber
{
public static void main(String args[])
{
System.out.println("给出1到100之间的数,请猜出这个数");
int realnumber=(int)(Math.random()*100)+1;
int yourguess=0;
Scanner scanner=new Scanner(System.in);
String str=scanner.next();
yourguess=Integer.parseInt(str);
while(yourguess>=1&&yourguess<=100)
{
if(yourguess>realnumber)
{
System.out.println("猜大了,再输入你的猜测");
yourguess=scanner.nextInt();
continue;

}
else if(yourguess {
System.out.println("猜小了,再输入你的猜测");

yourguess=scanner.nextInt();
continue;

}
else
System.out.println("祝贺你,猜对了");
yourguess=scanner.nextInt();
}
System.out.println("数字不对");

}

}

回答(2):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

public void go()
{
while (true)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = 0;
int num = (int)(Math.random()*101);
int guess = -1;
System.out.print("Please input the number you guess:");
while(true)
{
count++;
try
{
guess = (Integer.parseInt(br.readLine()));
}
catch(IOException e)
{

}
if(guess>num)
{
System.out.println("Too big!!");
System.out.print("Please input the number you guess again:");
continue;
}else if(guess {
System.out.println("Too small!!");
System.out.print("Please input the number you guess again:");
continue;
}else
{
System.out.println("Right");
System.out.print("You guess" + " " + count + " " + "times.");
break;
}
}
System.out.print("Continue?(Y/N):");
String flag = "";
try
{
flag = br.readLine();
}
catch(IOException e)
{

}
if(flag.equalsIgnoreCase("y"))
{
continue;
}else
{
break;
}
}
System.out.println("Game over!");
}

public static void main(String[] args)
{
Test game = new Test();
game.go();
}
}

我n久以前的学习文档里有,给你吧,呵呵~