我以前有一些基本的java技能但是自从我使用它们已经有几年了。我的程序会执行我想要的一切,但是我需要它来运行程序1000次,并保持rtCorrect
和rtIncorrect
的总计。这两个总数实际上是我最终需要的唯一东西(即我只需要最后评论的声明)。我不确定我是否应该在我的main方法中添加一个外部循环,或者我是否应该有一个单独的方法。我并不担心它很漂亮所以我宁愿只用一个简单的循环在main方法中完成所有操作。
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或者它在一次迭代x 1000上得到的任何RT,另一个转换为0
答案 0 :(得分:2)
要么。我建议(因为你是新手)只是在你的代码周围放一个for循环,当它工作时,如果你愿意,可以将它重构成单独的方法。
如果你将它重构为一种新方法,我建议你在main()
中进行 MyClass myclass = new MyClass();
myclass.runProgram();
然后让runProgram()执行循环,并对已有的东西执行另一种方法,并将当前的Tally保持为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){
...
答案 1 :(得分:0)
在这种情况下,因为您知道代码循环的频率,所以使用for loop
。语法如下:
for(counter; termination condition; incremental step)
例如:
for(int x = 0; x < 1000; x++)
在你的代码的上下文中,你只需将它包装在你的方法中,如下所示:
for(int x =0; x < 1000; x++) {
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;
}
}
}
然后,当代码完成后,打印出你的值,你就会得到1000
个案例的抽样数据,虽然从更科学的角度来说,如果你是为论文写这个,那么你就是处理PRNG(伪随机数生成器)然后你想要一个比它更大,更大的采样帧。 :)