回 帖 发 新 帖 刷新版面

主题:求助:关于输入数组以及排序的问题,请帮忙修改

题目:等待用户输入数组(输入指定字符结束),判断数组max 元素及其出现次数。
  谢谢!

我写了部分,但是编译时出安全警告,运行时出现不能读取输入终止的错误,请大家帮忙!

import java.io.*;
import java.util.*;
public class Test{ 
public static void main(String args[]) throws IOException

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
ArrayList al=new ArrayList();
int count=0;
while(!(br.readLine().equals("w")))
{
al.add(Integer.parseInt(br.readLine()));
}
int max = Integer.parseInt(al.get(0).toString());
Iterator iterator = al.iterator();
while (iterator.hasNext()) 
{
    String object =iterator.next().toString();
    int temp = Integer.parseInt(object);
//    System.out.println(temp);
    if (max <= temp) 
    {
        max = temp;
         if(max<temp)
         {
             count=1;
         }
         else
         {
             count++;
         }
    }
}
System.out.println(max);
System.out.println(count);

}

回复列表 (共2个回复)

沙发

请大家帮忙!

板凳

将你while 循环体内的
al.add(Integer.parseInt(br.readLine()));
变成
al.add( br.readLine() );


Integer.parseInt(br.readLine()) 是一个int型变量。

我来回复

您尚未登录,请登录后再回复。点此登录或注册