Java 设置一个保持方法返回的变量,并相应地更新自身
伙计们 我真的是Java新手,这是我在堆栈溢出方面的第一篇文章。如果我有任何格式错误或其他问题,请原谅我 这是一个学校项目。在这个项目中,我将模拟龟兔赛跑Java 设置一个保持方法返回的变量,并相应地更新自身,java,Java,伙计们 我真的是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;
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;
}
}
import java.util.concurrent.ThreadLocalRandom;
公务舱赛马{
公共静态void main(字符串[]args){
int t=1;//乌龟的初始位置
int h=1;//兔子的初始位置
int end=50;
int ran=ThreadLocalRandom.current().nextInt(1,10+1);
int nt=tortoise(ran,h);//第一次移动后更新位置
int nh=hare(ran,h);//第一次移动后更新位置
System.out.println(“\n”+”和THRY's OFF!!”;
对于(int i=1;i b){
System.out.println(“T=“+a+”“+b”);
}如果(a==b),则为else{
System.out.println(“OUTH!”);
}如果(a>=50),则为else{
System.out.println(“乌龟赢了!!”;
}否则,如果(b>=50){
System.out.println(“兔子赢!!”;
}
}
}
/*
龟步法
先决条件乌龟的当前位置
后置条件该乌龟的位置将根据移动类型(随机数)移动
*/
公共静态整数乌龟(整数随机,整数t){
如果(随机您需要在for循环之前声明a
和b
,否则将重新初始化保存的值
在你的循环中,它会像
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!");
}
试着把它改成
if (a >= 50) {
System.out.println("TORTOISE WIN!!");
break;
}
if (b >= 50) {
System.out.println("HARE WIN!!");
break;
}
更改:
int a = tortoise(random, nt);
int b = hare(random, nt);
进入:
在循环之前声明a
和b
:
int a=h, b=h;
我建议
移除第一步。它没有任何作用
int nt=tortoise(ran,h);//第一次移动后更新位置
int nh=hare(ran,h);//第一次移动后更新位置
第一个位置应该在循环内,但变量应该在循环外声明
int a=1; // initial position, can be 0 if you want, declare outside loop
int b= 1;// initial position, can be 0 if you want, declare outside loop
a = tortoise(random, a);
b = hare(random, b);
删除for循环。它不会告诉您何时退出。移动50次后您将退出。在另一个答案中建议使用带中断条件的Do-while循环要好得多
如果(a>=50){
System.out.println(“乌龟赢了!!”;
打破
}
大家好。非常感谢你们的帮助。以下是我所做的:1.删除nt,nh。2.更改投票接受更改:int a=乌龟(随机,nt);int b=兔子(随机,nt);to:int t=乌龟(随机,t);int h=兔子(随机,h);使用while循环而不是for循环。问题已解决!再次感谢!
int a=h, b=h;
int a=1; // initial position, can be 0 if you want, declare outside loop
int b= 1;// initial position, can be 0 if you want, declare outside loop
a = tortoise(random, a);
b = hare(random, b);