【问题标题】:Create method that prints the numbers between two specified numbers创建打印两个指定数字之间的数字的方法
【发布时间】:2017-02-20 09:53:14
【问题描述】:

所以我正在创建一个方法,该方法应该打印两个指定数字之间的数字。我之前在 main 方法中设置了它,但我可以弄清楚如何创建一个方法并将其调用到 main 方法中。我的程序现在所做的是它只打印“int between”等于什么。我不希望任何人只为我输入代码,我只是在寻找关于我应该改变什么的提示。上次我问了一个问题,有人继续用代码回答,但这并没有帮助我学习任何东西。

所以我的问题是是什么导致程序只显示之间等于?我知道我需要一个 if 循环的值来返回一些东西,但它需要返回 num1 和 num2 之间的数字。我的教授还说该方法需要是“public static void Printer(int num1, int num2)”这是否可能?我不断收到错误,所以我切换到“int Printer”。

package nortonaw_Assignment8;

public class Nortonaw_Assignment8 {

    public static void main(String[] args) {
        int between;
        between = Printer(5, 20);
        System.out.println(between);
    }
public static int Printer (int num1, int num2){
    int between = 0;
    for (;num1<=num2; num1++);
    return between;
    }
}

【问题讨论】:

  • 如果第一个数字大于第二个数字会怎样?例如Printer(20, 5);。可能需要考虑该用例。
  • 我也想知道同样的事情。我打算让用户输入数字并声明他们需要先输入低数字。
  • 致@DrewKennedy 的观点:我建议该方法抛出IllegalArgumentException 并带有一条消息,指出第一个参数应该是
  • 或者使用 if 语句来检查第一个数字是否大于第二个。如果为真,只需递减而不是递增。
  • @Drewmann 你考虑过接受答案吗?

标签: java eclipse if-statement methods


【解决方案1】:
package printingTasks;

public class Printer {

    public static void main(String[] args) {

        printInBetween(25, 30); //Would print 26 -> 29

    }

    /*
     * You don't need to return something if you just want to print it out to the console.
     * So you can use void as return type
     */
    public static void printInBetween(final int leftBoundary, final int rightBoundary){
        /**
         * Calculate the first number which should be printed.
         * This number would be the leftBoundery plus one
         * This number will be the starting point of your loop
         */
        final int firstNumber = leftBoundary + 1;
//      final int firstNumber = leftBoundary; //if you want to include the left boundary
        for (
                int currentNumber = firstNumber; //Set the counter of the the loop (currentNumber) to the first valid number
                currentNumber < rightBoundary;   //Run the loop while the loop counter is less than the rightBoundary
//              currentNumber <= rightBoundary;  //If you want to include the right boundary                    
                currentNumber++                  //Increment the loop counter with each iteration
                ){
            /**
             * In each iteration you will print the current value of the counter to the console
             * Because your counter (currentNumber) will be incremented from the first valid number
             * to the last number before the right boundary you will get all numbers between the two
             * boundaries. 
             */
            System.out.println(currentNumber);
        }
    }
}

【讨论】:

    【解决方案2】:

    您的老师希望您从 Printer 方法而不是直接在 main 方法中进行打印。应该做的所有主要方法就是调用 Printer 方法。 这是完整的代码片段:

    package nortonaw_Assignment8;
    public class Nortonaw_Assignment8 {
    
     public static void main(String[] args) {
         Printer(5, 20);
     }
     public static void Printer (int num1, int num2) {
         for (int i = num1+1;i<=num2-1; i++) {
             System.out.println(i);
         }
     }
    }
    

    【讨论】:

    • 我编辑了代码,现在可以使用了。注意:在初始打印机调用中,num2 必须始终大于 num1。否则你会看到一个无限循环。要解决此问题,您可以在 Printer 方法中添加一个 If 语句:if (num2>num1) {}。 @德鲁曼
    【解决方案3】:

    目前您的方法设计为仅返回一个数字,因此您要么返回一组数字,要么打印 Printer 内的数字,在这种情况下,您可以使用教授建议的方法签名。

    所以我会这样写:

    public static void main(String[] args) {
        Printer(5, 20);
    }
    public static void Printer (int num1, int num2) {
        for (int i=num1;i<=num2; i++) {
            System.out.println(i);
        }
    }
    

    编辑: 请注意,我引入了一个额外的计数器变量 i,因为我认为 num1num2 不应更改,因为它们定义了您的范围边界。

    【讨论】:

    • 感谢它正常工作,但您能向我解释一下为什么添加“int i”吗?
    • @Drewmann 我引入了一个额外的计数器变量 i 因为我认为 num1 和 num2 不应更改,因为它们定义了您的范围的边界
    【解决方案4】:

    1) 打印出一个值和返回一个值是有区别的。 当您在函数中“打印”一个值时,您只是将值写入屏幕或其他介质,但是当您使用 return 语句时,您将返回的内容传递给函数的调用者以及控制.

    ** 我希望这对你有意义?

    2)“public static void Printer(int num1, int num2)”是可能的。

    【讨论】:

    • 嗯,我还是很困惑。我了解打印出一个值,但不了解返回值的区别。你能详细说明一下吗?谢谢
    • @Drewmann 当调用 sayName() 的函数时,程序的控制权转移到函数 sayName()。程序的控制权将保留在 sayName() 中,直到它完成执行或遇到 return 语句。所以所有的 print 都是打印到屏幕上,而 return 将 sayName() 的结果传递给触发它的东西以及程序的控制。
    • 所以函数 sayName() 中的任何内容都将被执行,因为它现在控制了程序?抱歉,我不太了解它,我需要做更多的研究才能正确理解返回值的工作原理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多