【问题标题】:Min Max Average Data input最小值 最大值 平均值 数据输入
【发布时间】:2018-07-07 10:02:42
【问题描述】:

仍在为此苦苦挣扎。需要找到输入数据的最小值、最大值和平均值并将其打印到外部数据文件中。任何人都可以在我的代码中发现问题。它不会打印任何内容。

它需要读取具有以下内容的文件

min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6

但需要按以下格式打印出来

The min of [1, 2, 3, 5, 6] is 1.
The max of [1, 2, 3, 5, 6] is 6.
The avg of [1, 2, 3, 5, 6] is 3.4.




public class Test {

public static void main(String[] args) throws IOException {



    FileInputStream fi = new FileInputStream("F:\\Test\\file.txt");
    FileOutputStream fo = new FileOutputStream("F:\\Test\\output.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fi));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

    String strLine;
    while ((strLine = br.readLine()) != null)   {

          String[] arr = strLine.split(" ");
          String[] nos = arr[1].split(",");

          Set<Integer> set = new HashSet<Integer>();  
          for(int i = 0; i<nos.length; i++){
              int no = Integer.parseInt(nos[i]);
                   set.add(no); 
                }
          TreeSet<Integer> sortedSet = new TreeSet<Integer>(set); 

          switch(arr[0]) {

          case "Min:":
              String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
              bw.write(msg1);
              bw.newLine();

              break;

          case "Max:":
              String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
              bw.write(msg2);
              bw.newLine();
              break;

          case "Avg:":
              Object[] noarray = sortedSet.toArray();
              int noarraysize = noarray.length-1;
              int sum=0;
              for(int i=0;i<=noarraysize;i++) {

                  int no=Integer.valueOf(noarray[i].toString());
                  sum = sum + no;
                  if(i==noarraysize) {
                      String msg3="The Avg of [" +arr[1]+ "] is  " +(double)sum/noarray.length;
                      bw.write(msg3);
                      bw.newLine();
                                              }
              }
              break;

          case "Sum:":
              Object[] noarray1 = sortedSet.toArray();
              int noarraysize1 = noarray1.length-1;
              int sum1=0;
              for(int i=0;i<=noarraysize1;i++) {
                  int no=Integer.valueOf(noarray1[i].toString());
                  sum1 = sum1 + no;
                  if(i==noarraysize1) {
                      String msg4="The Sum of [" +arr[1]+ "] is  " +sum1;
                      bw.write(msg4);
                      bw.newLine();
                                              }
              }
              break;

        }

}
    br.close();
    bw.close();

}

}

如果我在代码主体之外添加一个打印语句,它可以工作,但我输入的任何消息都不会从外部打印到文本文件。

【问题讨论】:

  • 你有没有遇到类似 java.io.FileNotFoundException 的异常?可能是文件不存在

标签: java output max average min


【解决方案1】:

这行看起来是你的第一个问题:

String[] nos = arr[1].split(",");

arr 仅包含一个元素,因此您可以在数组边界之外访问(请记住数组索引为 0,因此第一个元素是 arr[0])。

这里还有另一个问题:

int no = Integer.parseInt(nos[i]);

在尝试从输入解析 int 之前,您需要使用输入的 min: 部分。

Edit 进行了一些更改,使其与您提供的输入一起工作:

public static void main(String[] args) throws IOException {

        FileInputStream fi = new FileInputStream("file.txt");
        FileOutputStream fo = new FileOutputStream("output.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fi));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

        String strLine;
        while ((strLine = br.readLine()) != null)   {

              String[] arr = strLine.split(":");
              String[] nos = arr[1].split(",");

              Set<Integer> set = new HashSet<Integer>();  
              for(int i = 0; i<nos.length; i++){
                  int no = Integer.parseInt(nos[i]);
                       set.add(no); 
              }
              TreeSet<Integer> sortedSet = new TreeSet<Integer>(set); 

              switch(arr[0]) {

              case "min":
                  String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
                  bw.write(msg1);
                  bw.newLine();

                  break;

              case "max":
                  String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
                  bw.write(msg2);
                  bw.newLine();
                  break;

              case "avg":
                  Object[] noarray = sortedSet.toArray();
                  int noarraysize = noarray.length-1;
                  int sum=0;
                  for(int i=0;i<=noarraysize;i++) {

                      int no=Integer.valueOf(noarray[i].toString());
                      sum = sum + no;
                      if(i==noarraysize) {
                          String msg3="The Avg of [" +arr[1]+ "] is  " +(double)sum/noarray.length;
                          bw.write(msg3);
                          bw.newLine();
                                                  }
                  }
                  break;

              case "sum":
                  Object[] noarray1 = sortedSet.toArray();
                  int noarraysize1 = noarray1.length-1;
                  int sum1=0;
                  for(int i=0;i<=noarraysize1;i++) {
                      int no=Integer.valueOf(noarray1[i].toString());
                      sum1 = sum1 + no;
                      if(i==noarraysize1) {
                          String msg4="The Sum of [" +arr[1]+ "] is  " +sum1;
                          bw.write(msg4);
                          bw.newLine();
                                                  }
                  }
                  break;

            }

    }
        br.close();
        bw.close();

    }

【讨论】:

  • String[] arr = strLine.split(""); System.out.println(Arrays.toString(arr)); String[] nos = arr[4].split(","); System.out.println(Arrays.toString(nos));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 2016-11-04
  • 2016-12-29
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多