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

2024-11-25 20:50:50
推荐回答(2个)
回答1:

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.");
}
}
}
}

回答2:

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 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);
}

}