用java定义并初始化一个长度为十的整形数组数组,打印其中的最大数及其位置号

用java定义并初始化一个长度为十的整形数组数组,打印其中的最大数及其位置号,第1张

public static void main(String[] args) {
int a[]=new int[10],max,t=0;
Systemoutprint("这个数组为:");
for(int i=0;i<alength;i++)   //用随机数初始化数组a
{
a[i]=(int)(Mathrandom()100);
Systemoutprint(a[i]+"  ");
}
Systemoutprintln();
max=a[0];
for(int i=0;i<alength;i++)//求最大值
{
if(a[i]>max)
{
max=a[i];
t=i;
}
}
Systemoutprintln("最大数是第"+(t+1)+"个数a["+t+"]="+max);
}
}

//我也是刚刚学java,这道题当做习题啦,望采纳

package Util;
public class MathUtil {
public static int max(int[] iList) {
int max = 0;
for (int i : iList) {
if (i > max)
max = i;
}
return max;
}
public static int max(Integer num) {
int max = 0;
if (num != null & numlength > 0) {
for (int i = 0; i < numlength; i++) {
if (num[i] > max)
max = num[i];
}
}
return max;
}
}
给你写了个方法,调用的方法是:
MathUtilmax();
这样就行了,考虑到方便原因,封装了两个方法,你可以传一个数组进去。
如MathUtilmax(numList);
也可以传入任意个你想要的数字,都会得出最大数字,如:
MathUtilmax(3,4,11,9);
MathUtilmax(100,200);
都可以。

import javamathBigInteger;
public class BigIntegerGet {
public String getAdd(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1add(BigInt2);
Str3=BigInt1toString();
return Str3;
}
public String getSubtract(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1subtract(BigInt2);
Str3=BigInt1toString();
return Str3;
}
public String getMultiply(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1multiply(BigInt2);
Str3=BigInt1toString();
return Str3;
}
public String getDivide(String Str1,String Str2){
String Str3=new String();
BigInteger BigInt1=new BigInteger(Str1);
BigInteger BigInt2=new BigInteger(Str2);
BigInt1=BigInt1divide(BigInt2);
Str3=BigInt1toString();
return Str3;
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13410597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-30
下一篇 2023-07-30

发表评论

登录后才能评论

评论列表(0条)

保存