gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:29 24 4
gpt4 key购买 nike

我 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

对不起,我警告过你,代码乱七八糟。

最佳答案

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

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

关于java - 当 if 语句失败时返回上一个 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com