gpt4 book ai didi

java - 外部循环运行我的程序 1000 次并保持计数 - Java

转载 作者:行者123 更新时间:2023-12-02 06:36:40 25 4
gpt4 key购买 nike

我曾经有一些基本的java技能,但我已经有几年没有使用它们了。我的程序一次就能完成我想要的所有操作,但我需要它运行该程序 1000 次并保留 rtCorrectrtIn Correct 的总数。这两个总数实际上是我最终需要的唯一东西(即我只需要最后注释掉的语句)。我不确定是否应该在主方法中添加外部循环,或者是否应该有一个单独的方法。我并不担心它是否漂亮,所以我宁愿在主方法中用一个简单的循环来完成这一切。

import java.util.Random;

public class Thesis {

public static void main (String [] args){

int correct = 0;
int incorrect = 0;
int rtCorrect = 0;
int rtIncorrect = 0;


while(correct<20 && incorrect<20){
Random rand = new Random();
int pickedNumber = rand.nextInt(100);
if (pickedNumber<52){
correct++;
}
else if (pickedNumber>51){
incorrect++;
}
if (correct == 20){
rtCorrect = rtCorrect + correct + incorrect;
}
else if (incorrect == 20){
rtIncorrect = rtIncorrect + incorrect + correct;
}
}

}
System.out.printf("Correct: %d Incorrect: %d", correct, incorrect);
System.out.printf ("\nCorrect Response Time: %d\nIncorrect Response Time: %d", rtCorrect, rtIncorrect);
//System.out.printf ("\nTotal Correct RT's: %d \nTotal Incorrect RT's: %d", totalRTCorrect, totalRTIncorrect);

}

}

这是我尝试过的:

import java.util.Random;

public class Thesis {

public static void main (String [] args){

int correct = 0;
int incorrect = 0;
int rtCorrect = 0;
int rtIncorrect = 0;


for (int j = 0; j < 1000;j++){
int totalRTIncorrect = 0;
int totalRTCorrect = 0;

while(correct<20 && incorrect<20){
Random rand = new Random();
int pickedNumber = rand.nextInt(100);
if (pickedNumber<52){
correct++;
}
else if (pickedNumber>52){
incorrect++;
}
if (correct == 20){
rtCorrect = rtCorrect + correct + incorrect;
}
else if (incorrect == 20){
rtIncorrect = rtIncorrect + incorrect + correct;
}
}

totalRTIncorrect = totalRTIncorrect + rtIncorrect;
totalRTCorrect = totalRTCorrect + rtCorrect;
System.out.printf ("\nTotal Correct RT's: %d \nTotal Incorrect RT's: %d", totalRTCorrect, totalRTIncorrect);
}

import java.util.Random;

public class Thesis {

public static void main (String [] args){

int correct = 0;
int incorrect = 0;
int rtCorrect = 0;
int rtIncorrect = 0;
int totalRTIncorrect = 0;
int totalRTCorrect = 0;

for (int j = 0; j < 1000;j++){

while(correct<20 && incorrect<20){
Random rand = new Random();
int pickedNumber = rand.nextInt(100);
if (pickedNumber<52){
correct++;
}
else if (pickedNumber>52){
incorrect++;
}
if (correct == 20){
rtCorrect = rtCorrect + correct + incorrect;
}
else if (incorrect == 20){
rtIncorrect = rtIncorrect + incorrect + correct;
}
}

totalRTIncorrect = totalRTIncorrect + rtIncorrect;
totalRTCorrect = totalRTCorrect + rtCorrect;
}
System.out.printf ("\nTotal Correct RT's: %d \nTotal Incorrect RT's: %d", totalRTCorrect, totalRTIncorrect);
//System.out.printf("Correct: %d Incorrect: %d", correct, incorrect);
//System.out.printf ("\nCorrect Response Time: %d\nIncorrect Response Time: %d", rtCorrect, rtIncorrect);


}

}

这些都没有给我我所需要的。第一个仅打印出程序的一次迭代 1000 次,第二个给我一个 RT 为 34000 或在一次迭代中达到的任何 RT x 1000,另一个 RT 为 0

最佳答案

要么。我建议(因为您是新手)在代码中放置一个 for 循环,当它起作用时,如果您愿意,可以将其重构为单独的方法。

如果你将其重构为一个新方法,我建议在你的 main() 中进行

 MyClass myclass = new MyClass();
myclass.runProgram();

然后让 runProgram() 执行循环,并对您已有的内容使用另一种方法,并将当前计数保留为 MyClass 的成员变量。避免链接大量静态调用并必须有返回值。

好的...这部分是为了解决以下评论:

    public static void main (String [] args){


int totalRTIncorrect = 0;
int totalRTCorrect = 0;

for (int j = 0; j < 1000;j++){

int correct = 0;
int incorrect = 0;
int rtCorrect = 0;
int rtIncorrect = 0;

while(correct<20 && incorrect<20){
...

关于java - 外部循环运行我的程序 1000 次并保持计数 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569619/

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