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

Java 创建打印两个指定数字之间的数字的方法,java,eclipse,if-statement,methods,Java,Eclipse,If Statement,Methods,所以我创建了一个方法,它应该打印两个指定数字之间的数字。我以前在main方法中设置过它,但是我可以找出如何创建一个方法并将其调用到main方法中。我的程序现在只打印“int-between”等于什么。我不希望任何人只是为我输入代码,我只是想知道我应该改变什么。上次我问了一个问题,有人只是用代码来回答,这对我学到任何东西都没有帮助 我的问题是,是什么导致程序只显示between等于多少?我知道if循环需要一个值来返回一些东西,但它需要返回num1和num2之间的数字。我的教授还说,该方法需要是“公

所以我创建了一个方法,它应该打印两个指定数字之间的数字。我以前在main方法中设置过它,但是我可以找出如何创建一个方法并将其调用到main方法中。我的程序现在只打印“int-between”等于什么。我不希望任何人只是为我输入代码,我只是想知道我应该改变什么。上次我问了一个问题,有人只是用代码来回答,这对我学到任何东西都没有帮助

我的问题是,是什么导致程序只显示between等于多少?我知道if循环需要一个值来返回一些东西,但它需要返回num1和num2之间的数字。我的教授还说,该方法需要是“公共静态无效打印机(intnum1,intnum2)”这是可能的吗?我一直收到一个错误,所以我切换到“int打印机”

包nortonaw_分配8;
公共类Nortonaw_分配8{
公共静态void main(字符串[]args){
int介于之间;
之间=打印机(5,20);
System.out.println(介于之间);
}
公共静态整型打印机(整型num1,整型num2){
int在=0之间;

对于(;num1目前,您的方法设计为只返回一个数字,因此您可以返回一组数字,然后在
打印机中打印这些数字,在这种情况下,您可以使用教授建议的方法签名

所以我会这样写:

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);
    }
}
publicstaticvoidmain(字符串[]args){
打印机(5,20);
}
公共静态无效打印机(int num1、int num2){
对于(inti=num1;i1),输出值和返回值之间存在差异。
当您在函数中“打印”一个值时,您只是将该值写入屏幕或其他介质,但当您使用return语句时,您将返回的内容传递给函数的调用方以及控件

**我希望这对你有意义


2) “公共静态无效打印机(int num1,int num2)”是可能的。

您的老师希望您从打印机方法而不是直接在主方法中打印。主方法应该做的就是调用打印机方法。 以下是完整的讽刺:

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);
     }
 }
}
包nortonaw_分配8;
公共类Nortonaw_分配8{
公共静态void main(字符串[]args){
打印机(5,20);
}
公共静态无效打印机(int num1、int num2){
对于(int i=num1+1;i
包打印任务;
公共类打印机{
公共静态void main(字符串[]args){
打印介于(25,30);//将打印26->29
}
/*
*如果您只想将内容打印到控制台,则无需返回。
*因此,您可以使用void作为返回类型
*/
公共静态无效打印中间(最终整型左边界,最终整型右边界){
/**
*计算应打印的第一个数字。
*这个数字将是leftBoundery加1
*这个数字将是循环的起点
*/
最终int firstNumber=leftBoundary+1;
//final int firstNumber=leftBoundary;//如果要包括左边界
为了(
int currentNumber=firstNumber;//将循环的计数器(currentNumber)设置为第一个有效数字
currentNumber//currentNumber如果第一个数字大于第二个数字会发生什么?例如
打印机(20,5)我想知道这个用例。我想让用户输入这些数字,然后声明他们需要先输入低数。对于@ DrWiknydy的观点:我建议方法抛出一个信息,声明第一个参数应该是或使用if语句到CH。检查第一个数字是否大于第二个数字。如果为真,只需递减而不是递增。@Drewmann您考虑过接受答案吗?嗯,我仍然感到困惑。我理解打印值,但不理解返回值的区别。您可以再详细说明一下吗?thanks@Drewmann当一个函数调用Wild call sayName()时,程序的控制权将转移到函数sayName()。程序的控制权将保留在sayName()上,直到它完成执行或命中return语句。因此,所有的打印操作都是在return传输sayName()的结果时打印到屏幕是什么触发了它以及对程序的控制然后将被执行,因为它现在已经控制了程序?很抱歉,我没有很好地理解它,我需要做更多的研究来正确理解返回值是如何工作的。谢谢,这是正确的,但是你能向我解释为什么你添加了“int i”?@Drewmann我引入了一个额外的计数器变量I,因为我认为num1和num2不应该更改,因为它们定义了范围的边界。我编辑了代码,它现在可以工作了。注意:num2在初始打印机调用中必须始终大于num1。否则,您将看到一个无限循环。要解决这个问题,您可以在Pri中添加一个If语句nter方法:if(num2>num1){}@Drewmann
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);
     }
 }
}
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);
        }
    }
}