
一个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.
In your command window, you need to show that the user plays the random number guessing game for 2 times, which the random numbers are different among the games. It means you need to run this program for two times in the command window, without the same random number in each run. 展开
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.
In your command window, you need to show that the user plays the random number guessing game for 2 times, which the random numbers are different among the games. It means you need to run this program for two times in the command window, without the same random number in each run. 展开
5个回答
展开全部
程序我懒得写,我可以帮你翻译一下。大概意思就是让电脑选一个随机数,然后你来猜,输入的数大了电脑就提示数太大了,输入的数小了电脑就提示数小了。猜对了电脑就提示恭喜,你猜对了。
追问
别介,帮忙写一下么,我写的不好,我想把别人的借鉴一下
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class EqualsText {
public static void main(String[] args) {
//I'm thinking of a number.
System.out.println("I'm thinking of a number. ");
int randomNumber = (int)(Math.random()*10);
//System.out.println(randomNumber);
System.out.println("Guess what it is ?");
System.out.println("Please input a number to guess it.");
int count = 1;
boolean flag = false;
while(true){
flag = processPart1(randomNumber,count);
count ++;
if(flag){
break;
}
}
}
public static boolean processPart1(int randomNumber,int count){
Scanner sc = new Scanner(System.in);
String str = sc.next();
if(str.matches("^[0-9]$")){
int userGuessNumber = new Integer(str);
if(userGuessNumber == randomNumber ){
System.out.println("Congratulations! You guessed it!");
System.out.println("I was thinking of the number "+randomNumber );
System.out.println("You got it right in "+ count+" guesses");
return true;
}else if(userGuessNumber > randomNumber ){
System.out.println("Too high, try again.");
}else{
System.out.println("Too low, try again.");
}
//count ++;
}
return false;
}
}
public class EqualsText {
public static void main(String[] args) {
//I'm thinking of a number.
System.out.println("I'm thinking of a number. ");
int randomNumber = (int)(Math.random()*10);
//System.out.println(randomNumber);
System.out.println("Guess what it is ?");
System.out.println("Please input a number to guess it.");
int count = 1;
boolean flag = false;
while(true){
flag = processPart1(randomNumber,count);
count ++;
if(flag){
break;
}
}
}
public static boolean processPart1(int randomNumber,int count){
Scanner sc = new Scanner(System.in);
String str = sc.next();
if(str.matches("^[0-9]$")){
int userGuessNumber = new Integer(str);
if(userGuessNumber == randomNumber ){
System.out.println("Congratulations! You guessed it!");
System.out.println("I was thinking of the number "+randomNumber );
System.out.println("You got it right in "+ count+" guesses");
return true;
}else if(userGuessNumber > randomNumber ){
System.out.println("Too high, try again.");
}else{
System.out.println("Too low, try again.");
}
//count ++;
}
return false;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你按照步骤来就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询