Properly executing a method in a method?

Viewed 56

I am trying to magically learn Java in 5 weeks because of a college class. That is not enough time to learn anything. I'm trying to complete this lab in ZyBooks, but I can't figure it out and I have no information to help. I am trying to call this checkEntry method. My results are constantly 0. Please someone explain how to do this properly.

Instructions:

Ask the user for input using the following prompt (precisely): "Please enter a number between 15 and 45. Enter 1 to exit." Store the user's input in a variable with the integer data type. Use a while loop to repeat the program, checking the user's entry in case they entered a 1 to exit the program. (1) Within the loop, place the prompt (user instructions) described above. (2) Next, within the loop, collect the user's input and store it in a variable. (3) Finally, still within the loop, display the output string precisely as follows, then call the checkEntry method, passing in the variable containing the user's input. "Output: " (do not forget the space after the colon) A separate method called "checkEntry" has been created for you in the starting template below (note: You can restore the default code if you wish to start over by clicking the "Load default template…" link). Within this method, create an IF and an ELSE, using a condition for the IF that checks to see if the value is greater than or equal to 35. If the user enterd a value of less than 35, the checkEntry method must multiply that value by 5 and return the result to main(). If the user enterd a value greater than or equal to 35, the program must instead add 10 to their number and return the result to main().

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        /* Type your code below this line. Create additional lines as needed. */
        Scanner scnr = new Scanner(System.in);
        int userNum;
        userNum = 0;
        while (userNum != 1) {
            System.out.println("Please enter a number between 15 and 45. Enter 1 to exit.");
            userNum = scnr.nextInt();
            System.out.println("Output: " + checkEntry(userNum));
        }
    } // do not delete this line

    public static int checkEntry(int incoming) {
        /* Type your code for the checkEntry method below this line. Create additional lines as needed. */
        int userNum;
        userNum = 0;
        if (userNum >= 35) {
            userNum = userNum + 10;
        } else {
            userNum = userNum * 5;
        }
        return userNum;
    } // do not delete this line
} // do not delete this line
2 Answers

Notice that your method checkEntry() never makes use of incoming; instead, it initializes userNum to 0, which means it always returns 0. It should therefore be:

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        /* Type your code below this line. Create additional lines as needed. */
        Scanner scnr = new Scanner(System.in);
        int userNum;
        userNum = 0;
        while (userNum != 1) {
            System.out.println("Please enter a number between 15 and 45. Enter 1 to exit.");
            userNum = scnr.nextInt();
            System.out.println("Output: " + checkEntry(userNum));
        }
    } // do not delete this line

    public static int checkEntry(int incoming) {
        /* Type your code for the checkEntry method below this line. Create additional lines as needed. */
        int userNum=0;
        if (incoming >= 35) {
            userNum = incoming + 10;
        } else {
            userNum = incoming * 5;
        }
        return userNum;
    } // do not delete this line
} // do not delete this line

A simpler version would be: import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        /* Type your code below this line. Create additional lines as needed. */
        Scanner scnr = new Scanner(System.in);
        int userNum;
        userNum = 0;
        while (userNum != 1) {
            System.out.println("Please enter a number between 15 and 45. Enter 1 to exit.");
            userNum = scnr.nextInt();
            System.out.println("Output: " + checkEntry(userNum));
        }
    } // do not delete this line

    public static int checkEntry(int incoming) {
        /* Type your code for the checkEntry method below this line. Create additional lines as needed. */
        if (incoming >= 35)
            return incoming + 10;
        return incoming*5;
    } // do not delete this line
} // do not delete this line

You need to assign incoming arguments in the checkEntry method.

Replace the lines int userNum; and userNum = 0; with the following line:

int userNum = incoming;
Related