数字中求最大数和第二大数?请指点一下。第二大数的代码,想了好久,不知道怎么写import javax.swing.JOptionPane;
public class j {
public static void main(String[] args)
{
String a = JOptionPane.showInputDialog(null, "请输入学生的数量 ");
int d = Integer.parseInt(a);
int max=Integer.MIN_VALUE;
int maxIndex =-1;
int temp=0;
for(int i = 1; i <= d; i++){
String k = JOptionPane.showInputDialog(null, "请输入第 "+i+ "位学生的成绩 ");
temp =Integer.parseInt(k);
if(temp> max){
max = temp;
maxIndex = i;
}
}
JOptionPane.showMessageDialog(null,"第 "+maxIndex+"位学生的成绩最高,成绩是:"+max);
}
}
public class j {
public static void main(String[] args)
{
String a = JOptionPane.showInputDialog(null, "请输入学生的数量 ");
int d = Integer.parseInt(a);
int max=Integer.MIN_VALUE;
int maxIndex =-1;
int temp=0;
for(int i = 1; i <= d; i++){
String k = JOptionPane.showInputDialog(null, "请输入第 "+i+ "位学生的成绩 ");
temp =Integer.parseInt(k);
if(temp> max){
max = temp;
maxIndex = i;
}
}
JOptionPane.showMessageDialog(null,"第 "+maxIndex+"位学生的成绩最高,成绩是:"+max);
}
}
public class j {
public static void main(String[] args)
{
String a = JOptionPane.showInputDialog(null, "请输入学生的数量 ");
int d = Integer.parseInt(a);
int[][] num=new int[2][2];
System.out.println(Arrays.deepToString(num));
for(int i = 1; i <= d; i++){
int k = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入第 "+i+ "位学生的成绩 "));
if(i==1){
num[0][1]=k;
num[0][0]=1;
num[1][1]=k;
num[1][0]=1;
}
if(k>num[1][1]){
if(k>num[0][1]){
num[1][1]=num[0][1];
num[1][0]=num[0][0];
num[0][1]=k;
num[0][0]=i;
}else{
num[1][1]=k;
num[1][0]=i;
}
}
System.out.println(Arrays.deepToString(num));
}
JOptionPane.showMessageDialog(null,"第 "+num[0][0]+"位最高,成绩是:"+num[0][1]+",其次是第"+num[1][0]+"位,成绩是"+num[1][1]);
}
}
API里好像有一个max方法
你重写一下?