I am trying to get the following program to work:
public class funWithNumbers {
public static void main (String[] args) {
int ten = 10;
int subend = 7;
int result = ten - subend;
int success = 0;
int trials = 750;
for (int i=0; i<trials; i++) {
double randomNumber = Math.random();
randomNumber = randomNumber * 200;
randomNumber++;
int randNum = (int) randomNumber;
int mystery = randNum;
returnTen(mystery, subend, result, success);
}
int accuracy = (success / trials) * 100;
System.out.println("Trials Run: " + trials);
System.out.println("We can say that the number " + result + " indeed equals " + result + " with " + accuracy + "% accuracy.");
}
public static int returnTen(int mystery, int subend, int result, int success) {
int original = mystery;
mystery = mystery + mystery;
mystery = mystery * 5;
mystery = mystery / original;
mystery = mystery - subend;
if (mystery == result) {
success++;
}
return success;
}
}
Everything in this program works except that last statement (return success
). For whatever reason, I cannot return the value from the returnTen
function back to the main program. I tried debugging the program using System.out.println()
statements along the way (not included above) and within the returnTen
function itself, the success
variable is successfully incremented. But I cannot get that to pass back to the main function.
What should happen: You'll notice the function is basically a common number trick. The end result of mystery
should be 3 (which is what result is). Therefore, success
should always be incremented every time the function runs. Which means that accuracy should be 100%, and the statement printed out should read We can say that the number 3 indeed equals 3 with 100% accuracy
What is happening: The end statement currently prints We can say that the number 3 indeed equals 3 with 0% accuracy
which is obviously not true - success
is not being passed back and therefore, accuracy is being calculated as 0%.
EDIT: Changing returnTen(mystery, subend, result, success);
to success = returnTen(mystery, subend, result, success);
changes the end statement from 0% to 25000%.
UPDATE: Fixed the problem - success =
needed to be added and I had to change success / result
to success / trials
- oops!