【发布时间】:2020-10-26 00:08:26
【问题描述】:
基本上,我有这两个类:
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);
}
}
我需要让程序扫描数字,然后在第二类中进行操作,以便它可以将数字反转。如何让主类调用整数并在操作后显示数字?我尝试过其他方法,例如将整数放在头等舱和扫描仪中,但这些都没有奏效。我能做什么?
【问题讨论】:
-
欢迎来到 SO。 “调出整数并显示数字”是什么意思。 “呼出”是指“从终端读取”,“显示”是指“打印”吗?您似乎有未使用的
set_nums方法的参数。它们是干什么用的? -
@sprinter 请解释您对问题的编辑。删除
import语句不是“格式化”。为什么要添加所有空格,尤其是in .nextInt()中的空格? -
import 语句是代码的必要部分。
-
nums类有import语句,只是我复制的时候没有出来。程序需要做的是:扫描数字,进行操作,结果必须分配给整数,以便打印在主类中