麻烦大家帮我用代码写出这个JAVA程序题,谢谢啦!!
编写程序,实现猜数小游戏。随机生成一个0~99(包括0和99)的数字,从控制台输入猜测的数字,输出提示太大还是太小,继续猜测,直到猜到为止,游戏过程中,记录猜对所需的次数...
编写程序,实现猜数小游戏。随机生成一个0~99(包括0和99)的数字,从控制台输入猜测的数字,输出提示太大还是太小,继续猜测,直到猜到为止,游戏过程中,记录猜对所需的次数,游戏结束后公布结果。程序运行结果如图2所示。猜测次数与游戏结果对照见表1所示。
表1 猜测次数与游戏结果对照表
次数 结果
1 你太聪明了!
2~5 不错,再接再厉!
大于等于6 要努力啊!
提示:产生0-99之间的随机数字:int number = (int)(Math.random()*100); 展开
表1 猜测次数与游戏结果对照表
次数 结果
1 你太聪明了!
2~5 不错,再接再厉!
大于等于6 要努力啊!
提示:产生0-99之间的随机数字:int number = (int)(Math.random()*100); 展开
5个回答
展开全部
import java.util.Scanner;
public class GuessNumber {
int number=(int)(Math.random()*100);//随机数
int guess; //你猜的数
int times=0; //猜的次数
public void start(){
System.out.println("猜一个0~99的整数,请输入:");
Scanner sc=new Scanner(System.in);
//System.out.println(number); //去掉注释就直接输出结果,测试一次猜中的情况。
while(true){
guess=sc.nextInt();
times++;
if(guess==number){
System.out.println("猜对了");
result(times);
break;
}else if(guess>number){
System.out.println("猜大了,继续输入:");
}else{
System.out.println("猜小了,继续输入:");
}
}
}
public void result(int times){
this.times=times;
System.out.print("你共猜了"+times+"次,");
switch(times){
case 1:
System.out.println("你太聪明了!");
break;
case 2:
case 3:
case 4:
case 5:
System.out.println("不错,再接再励!");
break;
default:
System.out.println("再努力啊!");
}
}
public static void main(String[] args) {
new GuessNumber().start();
}
}
public class GuessNumber {
int number=(int)(Math.random()*100);//随机数
int guess; //你猜的数
int times=0; //猜的次数
public void start(){
System.out.println("猜一个0~99的整数,请输入:");
Scanner sc=new Scanner(System.in);
//System.out.println(number); //去掉注释就直接输出结果,测试一次猜中的情况。
while(true){
guess=sc.nextInt();
times++;
if(guess==number){
System.out.println("猜对了");
result(times);
break;
}else if(guess>number){
System.out.println("猜大了,继续输入:");
}else{
System.out.println("猜小了,继续输入:");
}
}
}
public void result(int times){
this.times=times;
System.out.print("你共猜了"+times+"次,");
switch(times){
case 1:
System.out.println("你太聪明了!");
break;
case 2:
case 3:
case 4:
case 5:
System.out.println("不错,再接再励!");
break;
default:
System.out.println("再努力啊!");
}
}
public static void main(String[] args) {
new GuessNumber().start();
}
}
2012-11-16
展开全部
都写一起了 没另外写方法,没用String.format
import java.util.Scanner;
public class test1{
public static void main(String[] args){
new Runnable(){
@Override
public void run() {
boolean done = false;
int number = (int)(Math.random()*100);
int count = 0;
while (!done){
Scanner in = new Scanner(System.in);
if (count == 0)
System.out.print("输入一个数字: ");
else
System.out.print("再输入一次: ");
int i = in.nextInt();
if (i == number){
count++;
done = true;
}
else if ( i > number){
System.out.println("太大");
count++;
}
else {
System.out.println("太小");
count++;
}
}
System.out.println("次数 结果");
if (count == 1){
System.out.println(count+ " 你太聪明了");
}
else if (count <= 5 && count >=2){
System.out.println(count+ " 不错再接再厉额");
}
else
System.out.println(count+ " 要努力啊");
}
}.run();
}
}
import java.util.Scanner;
public class test1{
public static void main(String[] args){
new Runnable(){
@Override
public void run() {
boolean done = false;
int number = (int)(Math.random()*100);
int count = 0;
while (!done){
Scanner in = new Scanner(System.in);
if (count == 0)
System.out.print("输入一个数字: ");
else
System.out.print("再输入一次: ");
int i = in.nextInt();
if (i == number){
count++;
done = true;
}
else if ( i > number){
System.out.println("太大");
count++;
}
else {
System.out.println("太小");
count++;
}
}
System.out.println("次数 结果");
if (count == 1){
System.out.println(count+ " 你太聪明了");
}
else if (count <= 5 && count >=2){
System.out.println(count+ " 不错再接再厉额");
}
else
System.out.println(count+ " 要努力啊");
}
}.run();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void m1(){
int num = (int) (Math.random() * 100);
int n,count = 0;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("please enter your number : ");
n = sc.nextInt();
count ++;
if(n < num){
System.out.println(" too small !");
}else if(n > num){
System.out.println(" too big !");
}else{
System.out.println(count);
if (count < 2) {
System.out.println("Wow, you are so smart . ");
}else if(count <= 5){
System.out.println("Well, make persistent efforts .");
}else{
System.out.println("Right, also need to work hard .");
}
break;
}
}
}
// main 中 调用 m1();
int num = (int) (Math.random() * 100);
int n,count = 0;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("please enter your number : ");
n = sc.nextInt();
count ++;
if(n < num){
System.out.println(" too small !");
}else if(n > num){
System.out.println(" too big !");
}else{
System.out.println(count);
if (count < 2) {
System.out.println("Wow, you are so smart . ");
}else if(count <= 5){
System.out.println("Well, make persistent efforts .");
}else{
System.out.println("Right, also need to work hard .");
}
break;
}
}
}
// main 中 调用 m1();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
软件培训。。。还不好好学
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int number = (int) (Math.random() * 100);
Scanner scn = new Scanner(System.in);
System.out.println("请输出你猜测的数字:");
int a = scn.nextInt();
int temp = 1;
while (number != a) {
if (a > number) {
System.out.println("太大");
} else {
System.out.println("太小");
}
a = scn.nextInt();
temp++;
}
if (temp == 1) {
System.out.println("你太聪明了! ");
} else if (temp > 1 && temp < 6) {
System.out.println("不错,再接再厉! ");
} else if (temp >= 6) {
System.out.println("要努力啊! ");
}
Scanner scn = new Scanner(System.in);
System.out.println("请输出你猜测的数字:");
int a = scn.nextInt();
int temp = 1;
while (number != a) {
if (a > number) {
System.out.println("太大");
} else {
System.out.println("太小");
}
a = scn.nextInt();
temp++;
}
if (temp == 1) {
System.out.println("你太聪明了! ");
} else if (temp > 1 && temp < 6) {
System.out.println("不错,再接再厉! ");
} else if (temp >= 6) {
System.out.println("要努力啊! ");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |