创建方法,在两个指定的数字之间打印数字

时间:2016-10-11 18:34:38

标签: java eclipse if-statement methods

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

所以我的问题是什么导致程序只显示等于?我知道我需要if循环的值来返回一些东西,但它需要返回num1和amp;之间的数字。 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;
    }
}

4 个答案:

答案 0 :(得分:0)

目前你的方法只能返回一个数字,所以如果你在Printer内打印数字,你要么返回一个数字集合,在这种情况下你可以使用教授建议的方法签名。 / p>

所以我会这样写:

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不应该更改,因为它们定义了范围的边界。

答案 1 :(得分:0)

1)打印输出值和返回值之间存在差异。 当您在函数中“打印”一个值时,您只是将值写入屏幕或其他介质,但是当您使用return语句时,您将把返回的内容传递给函数的调用者以及控件

**我希望你有意义吗?

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

答案 2 :(得分:0)

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);
        }
    }
}

答案 3 :(得分:-1)

您的老师希望您不要直接在main方法中使用Printer方法进行打印。所有主要方法应该是调用Printer方法。 这是完整的snippit:

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);
     }
 }
}