2

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!

5
  • 1
    Possible duplicate of Is Java "pass-by-reference" or "pass-by-value"? Commented Dec 28, 2016 at 14:41
  • Pretty much this question could be the dupe, i fell like you are using it as it would pass a reference to the int's, in this case success, but you need to store it somewhere as success = returnTen(...) Commented Dec 28, 2016 at 14:42
  • Your edit should probably be a new question, since it likely identifies a symptom from a different problem. Commented Dec 28, 2016 at 14:48
  • I agree with Kevin, returnTen(mystery, subend, result, success); this call clearly forget to store the return value so this expect that the success pass will be change after the call. Commented Dec 28, 2016 at 14:49
  • 1
    And before creating a new question, since this is totaly different, this probably need to be investigate a bit from your part ;) Commented Dec 28, 2016 at 14:50

3 Answers 3

3

Your returnTen() function is returning the value, but your code is not using that value. To use the return value, the line:

returnTen(mystery, subend, result, success);

should be:

success = returnTen(mystery, subend, result, success);
Sign up to request clarification or add additional context in comments.

Comments

3

The problem isn't that return success; isn't working (it is), it's that you're not using the return value. When you call the function, you need to use the return value:

    success = returnTen(mystery, subend, result, success);
//  ^^^^^^^^^^

Comments

0

In order to make use of a return a value from a function you must assign it to a variable or use it as a test condition, etc.

if(returnTen(mystery, subend, result, success) > 0){
    System.out.println("returnTen returned an int value");
} 

Currently your code is discarding the return value since it is not assigned to anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.