📌  相关文章
📜  如何计算最小值,最大值和平均值并将输出写入java代码示例中的文本文件

📅  最后修改于: 2022-03-11 14:52:44.543000             🧑  作者: Mango

代码示例2
import java.io.File;
import java.util.Formatter;
import java.util.Scanner;

public class ExternalData {

public static void main(String[] args) {

    try {
        System.out.print("Enter the file name with extension : ");

        Scanner input = new Scanner(System.in);

        File file = new File(input.nextLine());

        input = new Scanner(file);

        while (input.hasNextLine()) {
            int min = input.nextInt();
            while(input.hasNextInt());
            int num = input.nextInt();
            if(num < min) {
                min = num;
            }

            while (input.hasNextLine()) {
                int max = input.nextInt();
                while(input.hasNextInt());
                int num2 = input.nextInt();
                if(num2 > min) {
                    min = num;

            String avg = input.nextLine();




            Formatter f = new Formatter("C:\\Users\\Kevin\\Dropbox\\Kevin 
            Carter-8042\\Intro to Soft Eng\\Task 12\\outputTest.txt");

            // print the formatted strings to the file
            f.format("The min of [1,2,3,4,5,6] is " + min);

            f.format("\n\nThe max of [1,2,3,4,5,6] is " + max);

            f.format("\n\nThe avg of [1,2,3,4,5,6] is " + avg);
            f.close();    
            }     

            } catch (Exception ex) {
              ex.printStackTrace();
            }

          }

       }