一道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 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. 展开
2个回答
展开全部
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.");
}
}
}
}
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.");
}
}
}
}
展开全部
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);
}
}
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);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询