求java大神帮忙看下这题怎么做

2024-11-21 13:04:18
推荐回答(4个)
回答(1):

import java.util.Scanner;

public class GuessNumber {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int order = (int) (Math.random() * 5 + 1);
boolean flag = true;
for (;flag;) {
System.out.print("I have a number between 1 and 5. Please enter your guess:");
int guess = sc.nextInt();
if (guess != order) {
System.out.println("Wrong - better luck next time.");
}else{
System.out.println("Good guess!");
flag = false;
}
}
}
}

回答(2):

public static void main(String [] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println(“请输入你的数字:”);
  int num = sc.nextInt();
if (1 < num < 5){
System.out.println("Good guess");
} else {
System.out.println("Try again");
}
}

回答(3):

import java.util.Random;
import java.util.Scanner;


public class Test
{
    public static void main(String[] args)
    {
        Random rand = new Random();
        rand.setSeed(1);
        Scanner sc = new Scanner(System.in);  
        while(true)
        {
            int num = rand.nextInt(5)+1;
            System.out.println("I have a number between 1 and 5.Please enter your guess:..");
            
            int input = sc.nextInt();
            if(input == num)
            {
                System.out.println("Good guess.");
            }
            else
            {
                System.out.println("Wrong - better luck next time.");
            }
        }
    }
}


回答(4):

boolean result = false ;

Random random = new Random();

int yourguess = 0 ;

int myNum = random.nextInt(5)+1 ;

while(!result){

System.out.print("I have a number between 1 and 5. Please enter your guess:");

Scanner scanner = new Scanner(System.in);

yourguess = scanner.nextInt();

if(yourguess != myNum){

System.out.println("Wrong - better luck next time.");

continue ;

}else{

System.out.println("Good guess.");

result = true;

}

}