用JAVA编写输入一个正整数n, 再输入n个整数,输出最大值

输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
需要运行成功的代码

public static void main(String[] args) throws Exception{
int count = getCount();
int max = 0;
int index = 1;
for(int i=0;i<count;i++){
System.out.println("请输入第"+index+"个正整数:");
String num = scanner.next();
if(num.matches("-?\\d+")){
max = max>Integer.parseInt(num)?max:Integer.parseInt(num);
index++;
}else{
count++;
}
}
scanner.close();
System.out.println("最大值:"+max);
}
private static int getCount(){
String count ="";
do{
System.out.println("请输入整数个数:");
count = scanner.next();
}while(!count.matches("\\d+"));
return Integer.parseInt(count);
}
这个该满足你的要求了,而且会避开错误输入,直到输入有效count个数的整数为止
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-26
import java.util.Scanner;

class Test{
public static void main(String [] args) throws Exception {
System.out.println("请输入要输输入字数的个数:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int max=0;
System.out.println("请输入数字:");
for(int i=0;i<n;i++){
Scanner s=new Scanner(System.in);
int m=s.nextInt();
a[i]=m;
if(a[i]>max){
max=a[i];
}
}
System.out.println("您输入的数字中最大的是:"+max);
}
}

不知道能不能满足你的要求。本回答被网友采纳
第2个回答  2012-11-28
我看“生物信息研究者”的回答就满足你的要求了。