如果语句

时间:2017-11-30 15:22:00

标签: java

我只在1周前开始用Java编程,所以很抱歉所有的错误代码。 我一直在从我以前玩的游戏中重新创建一个“精炼系统”,因为我觉得这对初学者项目来说是一个好主意。该系统基于成功的百分比来改进齿轮。随着你获得更高的提升,百分比几率下降。

在我的项目中进行精炼的一种方法是每次失败的细化都不会将细化级别重置为0,而是每次失败时将细化级别降低1。

我已经成功创建了一个在失败时重置为0的方法,但似乎无法弄清楚如何在失败时将级别降低1。

所以我的问题是,如何在失败时将我的“细化级别”降低1,而不是重置为0.此外,控制台需要在失败时返回到先前的IF语句,否则它将无法工作。

这是我的尝试:

import java.util.Random;

public class Tisha {

    public static void main(String[] args) throws InterruptedException {

        if (new Random().nextDouble() <= 0.535) {
            System.out.println("Refine successful  -  Refine level 1");
        } else {
            System.out.println("Refine failed  -  Refine reset");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 2");
        } else {
            System.out.println("Refine failed  -  Refine level 1");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 3");
        } else {
            System.out.println("Refine failed  -  Refine level 2");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 4");
        } else {
            System.out.println("Refine failed  -  Refine level 3");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 5");
        } else {
            System.out.println("Refine failed  -  Refine level 4");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 6");
        } else {
            System.out.println("Refine failed  -  Refine level 5");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 7");
        } else {
            System.out.println("Refine failed  -  Refine level 6");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 8");
        } else {
            System.out.println("Refine failed  -  Refine level 7");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.285) {
            System.out.println("Refine successful  -  Refine level 9");
        } else {
            System.out.println("Refine failed  -  Refine level 8");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.235) {
            System.out.println("Refine successful  -  Refine level 10");
        } else {
            System.out.println("Refine failed  -  Refine level 9");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.155) {
            System.out.println("Refine successful  -  Refine level 11");
        } else {
            System.out.println("Refine failed  -  Refine level 10");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.085) {
            System.out.println("Refine successful  -  Refine level 12");
        } else {
            System.out.println("Refine failed  -  Refine level 11");
        }
    }

}

输出:

Refine successful  -  Refine level 1
Refine failed  -  Refine level 1
Refine failed  -  Refine level 2
Refine failed  -  Refine level 3
Refine failed  -  Refine level 4
Refine successful  -  Refine level 6
Refine failed  -  Refine level 6
Refine failed  -  Refine level 7
Refine successful  -  Refine level 9
Refine failed  -  Refine level 9
Refine successful  -  Refine level 11
Refine successful  -  Refine level 12

Process finished with exit code 0

对于凌乱的代码,我很抱歉。

4 个答案:

答案 0 :(得分:4)

将细化级别设置为变量,并相应地增加或减少。

int refinementLevel = 0;

if (new Random().nextDouble() <= 0.155) {
  refinementLevel++;
  System.out.println("Refine successful  -  Refine level "+refinementLevel);
} else {
  refinementLevel--;
  System.out.println("Refine failed  -  Refine level "+refinementLevel);
}

答案 1 :(得分:0)

您可能希望将其放入一个类中,以便该对象包含当前级别,并且&#34;知道&#34;针对哪个阈值检查下一个随机来确定成功或失败。请注意检查以确保数组grade不会超出其限制。

public class Refinement {
    private static final double[] grade =
        { 0.535, 0.335, 0.335, 0.335,
        0.335, 0.335, 0.335, 0.335,
        0.285, 0.235, 0.155, 0.085 };
    private int level = 0;
    private Random rand = new Random();

    public int getLevel(){
        return level;
    }

    public void step(){
        int testlevel = level == grade.length ? grade.length - 1 : level;
        if( rand.nextDouble() <= grade[level] ){
            if( ++level >= grade.length ) level = grade.length;
        } else {
            if( --level < 0 ) level = 0;
        }
    }

    public static void main( String[] args ){
        Refinement refinement = new Refinement();
        for( int i = 1; i <= 20; ++i ){
            refinement.step();
            System.out.println( "level: " + refinement.getLevel() );
        }
    }
}

请注意,概率很难达到更高的水平。

答案 2 :(得分:0)

我不是一个非常有经验的开发人员,所以我的建议可能不是最好的,也不是最有效的。

我想过要创建一个无限循环。在该循环内部,我将放置一个switch语句,该语句检查首先用0初始化的int x的值。

您可以在switch语句之外创建一个细化变量,并在接下来的swich语句中添加程序的所有后续步骤。在每种情况下,您可以增加或减少细化值,也可以增加或减少在switch语句中检查的值(x的值)。细化成功后,您可以更改细化值并增加x的值,以便在下一个循环中处理下一个案例。如果细化失败,则x的值递减,并且在下一个while循环中正在处理前一个案例。

我想重复一遍,因为我是新手,这可能不是最好的解决方案。如果它不是,它可以帮助你找到一些新的想法。

答案 3 :(得分:0)

我尝试这个问题,随时问一下是否有任何不清楚的地方。我使用do-while循环来解决你的“回归”问题以及一些使程序动态化的变量

class Tisha {

    // An integer field to store the current level of refinement.
    int level = 0;

    // An array of probabilities for each level of refinement.
    double[] probs = new double[]{
        0.535, 0.335, 0.335, 0.335, 0.335,
        0.335, 0.335, 0.335, 0.285, 0.235,
        0.155, 0.085
    };

    // The random number generator
    Random random = new Random();

    // This function tries to perform a refinement
    public void tryRefinement() {
        if (random.nextDouble() <= probs[level]) {
            level++;
            System.out.println("Refine successful  -  Refine level " + level);
        } else {
            level--;
            if (level >= 0)
                System.out.println("Refine failed  -  Refine level " + level);
            else
                System.out.println("Refine failed  -  Refine reset");
        }
    }

    // The main method
    public static void main(String[] args) throws InterruptedException {

        // Create a 'Tisha' object (are you familiar with OOP?)
        Tisha tisha = new Tisha();

        // Keep refininig until the level reaches 0
        do {
            tisha.tryRefinement();
            Thread.sleep(250);
        } while (tisha.level > 0);
    }
}