如何从另一个类中调用带有整数参数的方法?

huangapple go评论150阅读模式
英文:

How can I call a method from another class with integers in it?

问题

基本上我有这两个类

    public class Main {
        public static void main(String[] args) {
            int units = 0;
            int tenth = 0;
            int hunds = 0;
            int thousands = 0;
            Nums met = new Nums();
            System.out.println("插入4位数:");
            met.set_nums();
            System.out.println("逆序后的数字为:" + units + tenth + hunds + thousands);
        }
    }
    
    public class Nums {
        Scanner in = new Scanner(System.in);
        int num;
        int thousands;
        int hunds;
        int tenths;
        int units;
        public void set_nums(int num, int thousands, int hunds, int tenths, int units) {
            num = in.nextInt();
            thousands = num / 1000;
            hunds = num / 100 - ((num / 1000) * 10);
            tenths = num / 10 - ((num / 100) * 10);
            units = num - ((num / 10) * 10);
        }
    }

我需要让程序扫描数字然后在第二个类中进行操作以便将数字颠倒过来如何使主类调用整数并在操作后显示数字我尝试过其他方法比如将整数和扫描器都放在第一个类中但都没有成功我该怎么办
英文:

Basically, I have these two classes:

public class Main {
    public static void main(String[] args) {
        int units = 0;
        int tenth = 0;
        int hunds = 0;
        int thousands = 0;
        Nums met = new Nums();
        System.out.println("Insert 4-digit number ");
        met.set_nums();
        System.out.println("The number " + num + " in reverse is " + units + tenth + hunds + thousands);
    }
}

public class Nums {
    Scanner in = new Scanner(System.in);
    int num;
    int thousands;
    int hunds;
    int tenths;
    int units;
    public void set_nums(int num, int thousands, int hunds, int tenths, int units) {
        num = in .nextInt();
        thousands = num / 1000;
        hunds = num / 100 - ((num / 1000) * 10);
        tenths = num / 10 - ((num / 100) * 10);
        units = num - ((num / 10) * 10);
    }
}

I need to make the program scan the number and then make the operations in the second class so it can put the number in reverse. How can I make the main class call out the integers and show up the numbers after the operation? I have tried other ways like putting the integers in the first class and the scanner too, but none of these have worked. What can I do?

答案1

得分: 1

你的代码中有许多错误。首先,我认为它无法编译(我没有尝试过),因为方法set_nums在调用时没有传递参数,但是它被声明为接收4个整数参数。

即使你在Main类中传递了变量,它也不会起作用,因为在Java中,参数是按值传递的,不会返回更改。

要使该代码起作用,你需要:

  1. 从Main类中删除units、tenth、hunds和thousands的声明。

  2. 从set_nums方法声明中删除所有参数。

  3. 打印Nums类实例的值:

    System.out.println("数字" + num + "反转后为" + met.units
    + met.tenths + met.hunds + met.thousands);

为了更好的编码风格,将Scanner的实例化移到Main类中,并在那里调用nextInt,将扫描到的数字传递给set_nums方法。

英文:

There are many errors in your code. Before all, I belive it does not compile (I have not tried), because method set_nums is called without arguments and it is declared to receive 4 int parameters.

Even if you pass the variables in Main class, it will not work, because in Java, arguments are passed by value and changes does not return.

To make that code works, you will need:

  1. remove declaration of units, tenth, hunds and thousands from Main class.

  2. remove all parameters from set_nums method declaration.

  3. print values from your instance on Nums class:

    System.out.println ( "The number " + num + " in reverse is " + met.units
    + met.tenths + met.hunds + met.thousands);

For a better coding style, move Scanner instantiation to the Main class and call nextInt there, passing the scanned number to set_nums.

huangapple
  • 本文由 发表于 2020年10月26日 08:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64530092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定