Skip to main content
deleted 6 characters in body
Source Link
maaartinus
  • 13.5k
  • 1
  • 34
  • 73

As you might have noted, methods which do printing are of no use in a bigger piece of code. While your lengthy method tries to say something to the user, it fails to communicate the outcome to the caller.

You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something like, call them, and react by some printing.

Try to start at the top level like

while (true) {
    printQuestion();
    char c = readInput();
    if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
    if (isCorrectAnswer(c)) {
        print("Incorrect"Correct!\n\n");
    } else {
        print("Correct"Inorrect!\n\n");
    }
}

Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.

As you might have noted, methods which do printing are of no use in a bigger piece of code. While your lengthy method tries to say something to the user, it fails to communicate the outcome to the caller.

You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something like, call them, and react by some printing.

Try to start at the top level like

while (true) {
    printQuestion();
    char c = readInput();
    if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
    if (isCorrectAnswer(c)) {
        print("Incorrect!\n\n");
    } else {
        print("Correct!\n\n");
    }
}

Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.

As you might have noted, methods which do printing are of no use in a bigger piece of code. While your lengthy method tries to say something to the user, it fails to communicate the outcome to the caller.

You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something, call them, and react by some printing.

Try to start at the top level like

while (true) {
    printQuestion();
    char c = readInput();
    if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
    if (isCorrectAnswer(c)) {
        print("Correct!\n\n");
    } else {
        print("Inorrect!\n\n");
    }
}

Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.

Source Link
maaartinus
  • 13.5k
  • 1
  • 34
  • 73

As you might have noted, methods which do printing are of no use in a bigger piece of code. While your lengthy method tries to say something to the user, it fails to communicate the outcome to the caller.

You could add some return value to the printing... but that's like adding insult to injury. Try to write methods computing something like, call them, and react by some printing.

Try to start at the top level like

while (true) {
    printQuestion();
    char c = readInput();
    if (!isLegalInput(c)) continue; // i.e., jump to the loop start to ask again
    if (isCorrectAnswer(c)) {
        print("Incorrect!\n\n");
    } else {
        print("Correct!\n\n");
    }
}

Now, there's a s*load of methods to write... but all of them are pretty trivial. You may also find out that they need more arguments or whatever... there are many ways to go. Just don't print in methods which may need to be reused. Every method should do a single thing.