gpt4 book ai didi

java - 设置一个变量来维护方法的返回并相应地更新自身

转载 作者:行者123 更新时间:2023-11-30 02:46:51 26 4
gpt4 key购买 nike

各位

我对 Java 很陌生,这是我在堆栈溢出中的第一篇文章。如果我有任何格式错误或其他任何问题,请原谅我。

这是一个学校项目。在这个项目中,我将模拟一场龟兔赛跑。

import java.util.concurrent.ThreadLocalRandom;
public class T_H_Race {
public static void main(String[] args) {
int t = 1;//initial position for Tortoise
int h = 1;//initial position for Hare
int end = 50;
int ran = ThreadLocalRandom.current().nextInt(1, 10 + 1);
int nt = tortoise(ran, h);//update position after 1st move
int nh = hare(ran, h);//update position after 1st move


System.out.println("\n" + "AND THRY'RE OFF!!");
for (int i = 1; i <= end; ++i) {
int random = ThreadLocalRandom.current().nextInt(1, 10 + 1);
int a = tortoise(random, nt);
int b = hare(random, nt);
if (a < b) {
System.out.println("H="+a+" "+b);
} else if (a > b) {
System.out.println("T="+a+" "+b);
} else if (a == b) {
System.out.println("OUTH!");
} else if (a >= 50) {
System.out.println("TORTOISE WIN!!");
} else if (b >= 50) {
System.out.println("HARE WIN!!");
}
}
}

/*
Method for Tortoise move
pre-condition current position of Tortoise
post-condition the position of that tortoise will move to base on the type of move(Random number)
*/
public static int tortoise(int random,int t) {
if (random <= 5) {
t = t + 3;//Tortoise has a fast plod that it will move 3 squares to right
} else if (6 <= random && random <= 8) {
t = t + 1;//Tortoise has a slow plod that it will move 1 squares to right
} else if (9 <= random && random <= 10) {
t = t - 6;//Tortoise has a slip that it will move 6 squares to left
}

if (t<0){
t=0;
}

return t;
}

/*
Method for Hare move
pre-condition current position of Hare
post-condition the position of that hare will move to base on the type of move(Random number)
*/
public static int hare(int random, int h) {
if (random == 1 | random == 2) {
h = h + 9;//Hare has a big hop that it will move 9 squares to right
} else if (3 <= random && random <= 5) {
h = h + 1;//Hare has a small hop that it will move 1 squares to right
} else if (random == 6) {
h = h - 12;//Hare has a big slip that it will move 12 squares to left
} else if (7 <= random && random <= 8) {
h = h - 2;//Hare has a small slip that it will move 2 squares to left
} else if (9 <= random && random <= 10) {
h = h;//Hare falls asleep that it will not move at all
}

if(h<0){
h=0;
}
return h;
}
}

我尝试创建两个变量(a,b),每个变量都维护乌龟和兔子的当前位置,并相应地更新自己。

例如,如果兔子移动 2 个位置,“a”将从 1 变为 3。然后,如果兔子移动 9 个位置,“a”将变为 12(因为 3+9)。但是,目前,我的代码始终使用初始位置(h=1)而不是当前位置。我知道这是因为我使用初始位置(h)作为该方法的输入。但我不知道应该如何改变我的方法并让输入成为当前位置。

非常感谢您的所有帮助!

最佳答案

您需要在 for 循环之前声明 ab,否则保存的值将被重新初始化。

在你的循环中它会像

a += tortoise(random, nt);

也许只是一条评论,但是,请考虑这段代码

            if (a < b) {
System.out.println("H="+a+" "+b);
} else if (a > b) {
System.out.println("T="+a+" "+b);
} else if (a == b) {
System.out.println("OUTH!");
}

以上三种情况相当于100%的可能情况,所以下面的else代码永远不会发生

            else if (a >= 50) {
System.out.println("TORTOISE WIN!!");
} else if (b >= 50) {
System.out.println("HARE WIN!!");
}

尝试将其更改为

        if (a >= 50) {
System.out.println("TORTOISE WIN!!");
break;
}
if (b >= 50) {
System.out.println("HARE WIN!!");
break;
}

关于java - 设置一个变量来维护方法的返回并相应地更新自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992786/

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