一道java编程题,跪求大神帮忙编一下,马上要交的作业啊。

Writeaprogramthatgeneratesarandomintegernumberandaskstheusertoguesswhatthenumberis.In... Write a program that generates a random integer number and asks the user to guess what the number is.
In this program, a constant integer is defined to limit the maximum random number. A random integer number for user is chosen from 1 to this maximum constant. By default, the constant is equal to 10.
At the beginning, the program will show
 "I'm thinking of a number. "
 "Guess what it is ", and then ask the user to input a number. (Here we assume user’s input is always valid.).
Next the system will respond to the user’s guess.
 If the user’s guess is higher than the random number, the program should display “Too high, try again.”
 If the user’s guess is lower than the random number, the program should display “Too low, try again.”
The program should use a loop that repeats until the user correctly guesses the random number.
The program also keeps a count of the number of guesses that user makes.
When the user correctly guesses the random number, the program should display
1. “Congratulations! You guessed it!”
2. “I was thinking of the number X.”, where X is the random number.
3. “You got it right in Y guesses.”, where Y is the number of guesses.
展开
 我来答
houseandmouse0
2012-11-02 · 超过32用户采纳过TA的回答
知道答主
回答量:161
采纳率:0%
帮助的人:89.7万
展开全部
import java.util.Random;import java.util.Scanner;
public class Game {
public static int MAX = 10;
public static void main(String[] args) {
System.out.println("I'm thinking of a number.");
int count=0;
int random = (new Random()).nextInt(MAX)+1;
System.out.println("Guess what it is.");
Scanner console = new Scanner(System.in);
int guess;
while(true){
guess = console.nextInt();
count++;
if(guess==random){
System.out.println("Congratulations! You guessed it!");
System.out.println("I was thinking of the number "+random+".");
System.out.println("You got it right in "+count+" guesses.");
break;
}
if(guess>random){
System.out.println("Too high, try again.");
}else{
System.out.println("Too low, try again.");
}
}
}
}
生物信息研究者
2012-11-02 · TA获得超过524个赞
知道小有建树答主
回答量:274
采纳率:0%
帮助的人:205万
展开全部
it is so easy~!
package myTest;

import java.util.Scanner;

public class user2 extends users{
public static void main(String [] args){
int constant=10;
int input=0;
int count=0;
Scanner sc=new Scanner (System.in);
System.out.println("I'm thinking of a number\nGuess what it is");
while(input!=constant){
input=sc.nextInt();
if(input<constant){
System.out.println("Too low, try again");
count++;
}
if(input>constant){
System.out.println("Too high, try again");
count++;
}
}
System.out.println("Congratulations! You guessed it!");
System.out.println("I was thinking of the number"+constant);
System.out.println("You got it right in Y guesses"+count);
}

}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式