如何根据特定情况重复执行我的程序? [英] How do I make my program repeat according to certain circumstances?

查看:89
本文介绍了如何根据特定情况重复执行我的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

public class MyFirstGame {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please Enter A Number: ");
        double s = scanner.nextDouble();
        double randomNumber = Math.random();
        double realNumber = randomNumber*10;
        double realerNumber = Math.round(realNumber);
        System.out.println(realerNumber);

        if(s==realerNumber) {
            System.out.println("You Win!");
        } else {
            System.out.println("Try Again...");
        }
    }
}

所以我想做的是为我的Java类制作一个游戏".我生成了一个1到10之间的随机数,用户必须输入一个数字,如果输入和随机数相同,则它们获胜".如果输了,他们会再试一次...?首先,我做了所有我什至不完全了解的扫描仪必需的东西.我只是抄袭了教授.因此,程序说要输入一个数字,并且程序会生成一个介于0.0和1.0之间的数字.我将该数字乘以10使其在1到10之间.然后将数字四舍五入到最接近的整数.如果输入与此数字匹配,则程序会说您赢了.如果没有,它会说再试一次.

So what I am trying to do is make a "game" for my Java class. I have generate a random number between 1 and 10 and the user has to input a number and if the input and the random number are the same, they "win." If they lose, they try again...? First, I did all the necessary scanner stuff that I don't even fully understand. I just copied the professor. So the program says to enter a number and the program generates a number between 0.0 and 1.0. I multiply that number by 10 to make it between 1 and 10. Then I round the number to the nearest integer. If the input matches this number, the program says you win. If not, it'll say try again.

问题是如何使程序重复执行而无需用户使用cmd重新启动程序?我需要重复输入,随机数生成器,然后是结果.我需要做什么?另外,我的程序怎么样?我的第二个大个子...是的...大个子但是,认真的说,我怎样才能使它变得不那么复杂,或者如何加以改进.谢谢.

The problem is how do I make the program repeat itself without the user having to reboot the program with the cmd? I need to repeat the input, random number generator, and then the result. What do I need to do? Also, how is my program? My second big one...yeah right...big. But seriously, how can I make it less complex or anything to improve it. Thanks.

推荐答案

使用

已经有一个生成随机数,您可以使用它:

There is already a class to generate random numbers, you could use it:

// TODO: move to constant
int MAX = 10;
// nextInt(int n) generates a number in the range [0, n)
int randomNumber = new Random().nextInt(MAX + 1)

这篇关于如何根据特定情况重复执行我的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆