简体   繁体   English

如何使用Scanner在Main中输入两个数字并使其与方法一起使用(升序Java)?

[英]How to input two numbers in Main using Scanner and make it work with a method (Ascending order java)?

I'm trying to make this program that will print out numbers in ascending order between two input numbers. 我正在尝试制作此程序,该程序将在两个输入数字之间按升序打印出数字。 I made it work as a method and I can call the method in Main, but what I really want to do is to input the two numbers in Main and not in the method. 我将其作为方法工作,可以在Main中调用该方法,但是我真正想做的是在Main中而不是在方法中输入两个数字。

So something like this: 所以像这样:

System.out.println(”Two numbers in ascending order”); System.out.println(“两个数字升序”);

(input two numbers in console) (在控制台中输入两个数字)

And then after this, call the method that will print in ascending order between the chosen numbers of Main. 然后,此后,调用将在所选的Main编号之间以升序打印的方法。

I'm new to this and i've tried several things, but I can't seem to figure out what to do. 我对此并不陌生,我已经尝试了几件事,但是似乎无法弄清楚该怎么做。 Would appreciate som help. 希望索姆帮助。

This is how the code looks now. 现在的代码就是这样。

import java.util.Scanner;

public class AscendingOrder {

    public static void main(String[] args) {

        // calling method

        int ascending1 = ascending();
    }

    public static int ascending() {

        int min;
        int max;
        int total = 0;

        Scanner sc = new Scanner(System.in);
        System.out.println("Two numbers in ascending order");
        min = sc.nextInt();
        max = sc.nextInt();

        for (int i = min; i < max + 1; i++) {
            System.out.print(i + " ");
            total += i;

        }
        return total;
    }
}

Pass in the two inputs as parameters to your method, something like : 将两个输入作为参数传递给您的方法,例如:

public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     System.out.println("Two numbers in ascending order");
     int min = sc.nextInt();
     int max = sc.nextInt();

     int ascending1 = ascending(min, max);
     System.out.println(ascending1);
}

And now : 现在 :

public static int ascending(int min, int max) {
    int total = 0;
    for (int i = min; i < max + 1; i++) {
       System.out.print(i + " ");
       total += i;
     }
     return total;
}

Notice that now the definition of ascending() takes in two parameters of type int . 注意,现在ascending()的定义接受了int类型的两个参数 The values are passed from the main method to the method. 值从main方法传递到该方法。

You can input the numbers in the main method and then pass them to another method: 您可以在main方法中输入数字,然后将它们传递给另一个方法:

public class AscendingOrder {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Two numbers in ascending order");
        int min = sc.nextInt();
        int max = sc.nextInt();

        int ascending1 = ascending(min, max);
    }

    public static int ascending(int min, int max) {
        int total = 0;

        for (int i = min; i < max + 1; i++) {
            System.out.print(i + " ");
            total += i;
        }
        return total;
    }
}

You can read the command line arguments or place the scanner in main function and pass the arguments 您可以读取命令行参数或将扫描仪放置在main函数中并传递参数

import java.util.Scanner;

public class AscendingOrder {

    public static void main(String[] args) {
        System.out.println("Entered first number is: "+args[0]);  
        System.out.println("Entered Secomd number is: "+args[1]);  
        int ascending1 = ascending(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
    }

    public static int ascending(int min,int max) {

        int total = 0;

        for (int i = min; i < max + 1; i++) {
            System.out.print(i + " ");
            total += i;

        }
        return total;
    }
}

You can get the numbers in main method and sort them in the main method itself. 您可以在main方法中获取数字,并在main方法本身中对其进行排序。 Java 8 makes it so easy with streams. Java 8使使用流变得如此容易。

    public static void main( String[] args ) throws Exception {

    Framework.startup( );

    Scanner sc = new Scanner( System.in );
    System.out.println( "Two numbers in ascending order" );
    int num1= sc.nextInt( );
    int num2= sc.nextInt( );

    Arrays.asList( num1, num2).stream( ).sorted( ).forEach( System.out::println );
    }

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

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