args 变量中整数的最大和最大差
我正在编写一段代码,计算 args 变量中的最大数字以及 args 中最高和最低整数之间的最大差异。
目前我的代码如下所示:
public int max(int [] args) {//array of ints
int m = args[0]; // first element
//initialisation; condition; update
for (int j = 1; j < args.length; ++j) {
// statement in a block:
if (m < args[j]) {
m = nums[j];
// if m is less than the j-th element
// then store this new smaller value
}
}
return m;
}
public int min(int [] args) {//array of integerss
int mi = nums[0]; // first element
//initialisation; condition; update
for (int j = 1; j < args.length; ++j) {
// statement in a block:
if (mi > args[j]) {
mi = args[j];
// if m is greater than the j-th element
// then store this new largest value
}
}
return mi;
} //通过将数字之和除以计数来计算平均值
public void main(String [] args) {
System.out.println(args[0]);
SimpleCalc fm = new SimpleCalc();
**System.out.println(fm.max(nums));**
**System.out.println(fm.max(nums) - fm.min(nums));**
当我使用数组时它返回值,但它似乎无法使用args进行编译。我不知道如何解决这个问题。
I am writing a code that calculates the maximum number in my args variable as well as the largest difference between the highest and lowest integer in args.
Currently my code looks like this:
public int max(int [] args) {//array of ints
int m = args[0]; // first element
//initialisation; condition; update
for (int j = 1; j < args.length; ++j) {
// statement in a block:
if (m < args[j]) {
m = nums[j];
// if m is less than the j-th element
// then store this new smaller value
}
}
return m;
}
public int min(int [] args) {//array of integerss
int mi = nums[0]; // first element
//initialisation; condition; update
for (int j = 1; j < args.length; ++j) {
// statement in a block:
if (mi > args[j]) {
mi = args[j];
// if m is greater than the j-th element
// then store this new largest value
}
}
return mi;
}
//compute average by dividing sum of numbers over the count
public void main(String [] args) {
System.out.println(args[0]);
SimpleCalc fm = new SimpleCalc();
**System.out.println(fm.max(nums));**
**System.out.println(fm.max(nums) - fm.min(nums));**
It was returning values when i used arrays, but it doesnt seem to compile with args. Im not sure how to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它无法编译,因为
main
方法中的args
是一个String
数组。您的方法采用int
数组作为参数。因此,您应该将String
的args
数组转换为int
数组,并传递这个新的int
数组code> 到您的min
和max
方法。使用Integer.parseInt
将String
转换为int
It doesn't compile because
args
, in themain
method is an array ofString
s. Your methods take an array ofint
s as argument. You should thus transform theargs
array ofString
s into an array ofint
s, and pass this new array ofint
s to yourmin
andmax
methods. UseInteger.parseInt
to transform aString
into anint
看来您在
max()
中有一个拼写错误:m = nums[j]
应该是m = args[j]
。同样在
min()
中,int mi = nums[0];
可能应该是int mi = args[0];
另外,在哪里
nums
[in main] 来自?您应该先创建此数组,然后再将其传递给max()
和min()
it seems you have a typo in
max()
:m = nums[j]
should bem = args[j]
.also in
min()
,int mi = nums[0];
should probably beint mi = args[0];
also, where does
nums
[in main] comes from? you should create this array before passing it tomax()
andmin()