简体   繁体   English

当if语句失败时,返回上一个if语句

[英]Going back to previous if statement when fail if statement

I only started programming in Java 1 week ago so sorry for all the bad code. 我只在1周前开始用Java编程,所以对所有糟糕的代码感到抱歉。 I have been recreating a "refinement system" from a game I used to play just because I thought it would be a good idea for a beginner project. 我一直在从我以前玩的游戏中重新创建一个“精炼系统”,因为我觉得这对初学者项目来说是一个好主意。 This system improves gear based on a percentage chance of success. 该系统基于成功的百分比来改进齿轮。 As you get a higher refinement, the percentage chance goes down. 随着您获得更高的细化,百分比几率下降。

One method of refining in my project is where every failed refinement doesn't reset your refinement level to 0, but instead decreases the refinement level by 1 every time you fail. 在我的项目中改进的一种方法是每次失败的细化都不会将细化级别重置为0,而是每次失败时将细化级别降低1。

I have already successfully created a method that resets to 0 upon fail, but can't seem to figure out how to decrease the level by 1 upon fail. 我已经成功创建了一个在失败时重置为0的方法,但似乎无法弄清楚如何在失败时将级别降低1。

So my question is, how can I get my "refinement level" to decrease by 1 upon fail, rather than reset to 0. Also, the console needs to return to the previous IF statement upon fail otherwise it will not work. 所以我的问题是,如何在失败时将我的“细化级别”降低1,而不是重置为0.此外,控制台需要在失败时返回到先前的IF语句,否则它将无法工作。

This was my attempt: 这是我的尝试:

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

}

Output: 输出:

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

Sorry for the messy code, I warned you. 对不起凌乱的代码,我警告过你。

Set your refinement level as a variable, and increase or decrease accordingly. 将细化级别设置为变量,并相应地增大或减小。

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

You might want to put this into a class so that the object contains the current level and "knows" against which threshold to check the next random to determine success or failure. 您可能希望将其放入一个类中,以便该对象包含当前级别并“知道”哪个阈值检查下一个随机来确定成功或失败。 Note the checks to ensure that the array grade isn't accessed beyond its limits. 请注意检查以确保不会超出其限制访问阵列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() );
        }
    }
}

Note that the probabilities are such that it is rather difficult to reach higher levels. 请注意,概率很难达到更高的水平。

I'm not a very experienced developer so my suggestion might not be the best of all and nor the most efficient one. 我不是一个非常有经验的开发人员,所以我的建议可能不是最好的,也不是最有效的。

I thought of creating an infinite loop. 我想到了创建一个无限循环。 Inside of that loop I would put a switch statement which checks the value of int x that is firstly initialized with 0. 在该循环内部,我将放置一个switch语句,该语句检查首先用0初始化的int x的值。

You could create a refinement variable outside of the switch statement and put all the next steps of your program in next cases of swich statement. 您可以在switch语句之外创建一个细化变量,并在接下来的swich语句中添加程序的所有后续步骤。 Inside of each case you could increase or decrease the value of refinement and also increment or decrement the value that is checked in switch statement (value of x ) . 在每种情况下,您可以增加或减少细化值,也可以增加或减少在switch语句中检查的值( x值)。 When refinement is successful you change the value of refinement and increment the value of x so in next loop the next case is being processed. 细化成功后,您可以更改细化值并增加x的值,以便在下一个循环中处理下一个案例。 If refinement was failed, the value of x is decremented and in next while loop the previous case is being processed. 如果细化失败,则x的值递减,而在下一个while循环中,正在处理前一个案例。

I want to repeat that as I am novice, this can be not the best solutions. 我想重复一遍,因为我是新手,这可能不是最好的解决方案。 If it's not maybe it can help you find some new ideas. 如果它不是它可以帮助你找到一些新的想法。

My try on this problem, feel free to ask if anything is not clear. 我尝试这个问题,随时问一下是否有什么不清楚。 I used a do-while loop to solve your "go-back" problem and some variables to make the program dynamic 我使用了一个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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM