【问题标题】:Using Java make our random number from 1 to 10 (not 1 to 100) [closed]使用 Java 使我们的随机数从 1 到 10(不是 1 到 100)[关闭]
【发布时间】:2022-11-18 00:15:49
【问题描述】:
package lab7guess;
/**
 *
 * @author Your Name
 */
import java.util.Random;

public class Lab7Guess {
    public static void main(String[] args) {
        // pick a random number
        Random random = new Random();
        int number = random.nextInt(10) + 1;
        System.out.println(number);

        // you need a Scanner to get the next int from the user

    }
}

编写一个程序,选择一个随机数,提示用户猜测它,并显示猜测的数字与所选数字之间的差异。

通过一次添加少量代码并边做边测试,修改程序以告诉用户猜测是太高还是太低,然后 提示用户再次猜测。该程序应继续运行,直到用户猜对或猜错 3 次。如果用户猜对了数字,则显示一条消息并终止程序。

【问题讨论】:

标签: java


【解决方案1】:
Scanner in = new Scanner(System.in);
System.out.println("enter number user");
int choice = in.nextInt();
if(choice == number) System.out.println("right number");
if(choice > number) System.out.println(choice - number+" diffrence");
if(choice < number) System.out.println( number - choice +" diffrnece");

希望这就是你要找的

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 2012-02-27
  • 2013-12-21
  • 1970-01-01
  • 2017-08-20
相关资源
最近更新 更多