Java 如何从另一个包含整数的类调用方法?
基本上,我有两门课:Java 如何从另一个包含整数的类调用方法?,java,class,methods,Java,Class,Methods,基本上,我有两门课: 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
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);
}
}
我需要让程序扫描数字,然后在第二个类中进行操作,这样它就可以把数字倒过来。如何让主类调用整数并在操作后显示数字?我也尝试过其他方法,比如把整数放在第一个类和扫描器中,但这些都不起作用。我能做什么?您的代码中有许多错误。首先,我相信它不会编译(我没有尝试过),因为调用方法set_nums时没有参数,并且声明它接收4个int参数 即使在Main类中传递变量,也不会起作用,因为在Java中,参数是按值传递的,更改不会返回 要使该代码正常工作,您需要:
为了获得更好的编码风格,请将Scanner实例化移动到Main类,并在那里调用nextInt,将扫描的号码传递给set nums。欢迎使用SO。“调出整数并显示数字”是什么意思。“呼叫”是指“从终端读取”还是“显示”是指“打印”?您的
set\u nums
方法似乎有未使用的参数。它们是干什么用的?@sprinter请解释一下你对这个问题的编辑。删除import
语句不是“格式化”。为什么要在.nextInt()中添加所有空格,尤其是中的空格?
?import语句是代码的必要部分。nums类有一个import语句,只是在复制它时没有出现。该程序需要做的是:扫描数字,进行运算,结果必须分配给整数,以便打印在主类中。请发布一份完整的代码副本,包括您所做的更改。另一位用户曾经要求我这样做,尽管我的更改非常简单;您的更改要复杂得多。