繁体   English   中英

正确执行方法中的方法?

[英]Properly executing a method in a method?

由于大学 class,我正试图在 5 周内神奇地学习 Java。 没有足够的时间来学习任何东西。 我正在尝试在 ZyBooks 中完成此实验,但我无法弄清楚,也没有任何信息可以提供帮助。 我正在尝试调用此 checkEntry 方法。 我的结果始终为 0。请有人解释如何正确执行此操作。

指示:

使用以下提示(准确)要求用户输入:“请输入 15 到 45 之间的数字。输入 1 退出。” 将用户的输入存储在具有 integer 数据类型的变量中。 使用 while 循环重复程序,检查用户的输入以防他们输入 1 以退出程序。 (1) 在循环中,放置上述提示(用户说明)。 (2) 接下来,在循环中,收集用户的输入并将其存储在一个变量中。 (3) 最后,仍然在循环内,精确显示output 字符串如下,然后调用checkEntry 方法,传入包含用户输入的变量。 “输出:”(不要忘记冒号后面的空格)在下面的起始模板中为您创建了一个名为“checkEntry”的单独方法(注意:如果您想重新开始,您可以通过单击“恢复默认代码”加载默认模板...”链接)。 在此方法中,创建一个 IF 和一个 ELSE,使用 IF 条件检查值是否大于或等于 35。如果用户输入的值小于 35,则 checkEntry 方法必须乘以该值乘以 5 并将结果返回给 main()。 如果用户输入的值大于或等于 35,则程序必须将其数字加 10 并将结果返回给 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

请注意,您的方法 checkEntry() 从不使用传入; 相反,它将 userNum 初始化为 0,这意味着它始终返回 0。因此它应该是:

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

一个更简单的版本是: 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

您需要在 checkEntry 方法中分配传入的 arguments。

替换行int userNum; userNum = 0; 使用以下行:

int userNum = incoming;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM