Java 使用用户输入计算分数

Java 使用用户输入计算分数,java,Java,我需要能够完成这个项目。我错过了一节课,完全不知所措。我需要能够把一个txt文件的总数,百分比,最高,最低和等级。我有它,我可以输入给我的5分,但其余的不起作用。我完全迷路了,不知道我需要从我所拥有的去哪里。我可以使用循环,但它不是必需的 import java.util.Scanner; public class BenefieldProgram4 { public static void main(String[ ] args) { Scanner keyboard

我需要能够完成这个项目。我错过了一节课,完全不知所措。我需要能够把一个txt文件的总数,百分比,最高,最低和等级。我有它,我可以输入给我的5分,但其余的不起作用。我完全迷路了,不知道我需要从我所拥有的去哪里。我可以使用循环,但它不是必需的

import java.util.Scanner;

public class BenefieldProgram4 {

 public static void main(String[ ] args)  {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Enter 5 Grades:");
         System.out.println("");
         keyboard.nextDouble();
         double a, b, c, d, e; //5 Grades
         double sum = 0;     // total
         double avg;        // percentage
         double min = 1;    // lowest grade
         double max = 100;  // highest grade

         //Calculations
         a = keyboard.nextDouble();
         b = keyboard.nextDouble();
         c = keyboard.nextDouble();
         d = keyboard.nextDouble();
         e = keyboard.nextDouble();
         keyboard.close();

         sum = (a+b+c+d+e);
         System.out.println("Total Score=:"+sum);

         avg =(a+b+c+d+e)/500;
         System.out.println("Percentage =: "+avg); 

         max = Double.MAX_VALUE;  
         System.out.println("Highest Score= " +max);

         min = Double.MIN_VALUE;
         System.out.println("Lowest Score= " +min);

         if (avg >= 90)
            System.out.println("Grade= A");
            else if (avg >=80)
            System.out.println("Grade= B");
            else if (avg >= 70)
            System.out.println("Grade= C");
            else if (avg >= 60)
            System.out.println("Grade= D");
            else
            System.out.println("Grade= F");
             }
 }
}
编辑

怎么样

// The name of the file to open.
        String fileName = "temp.txt";

        try {
            // Assume default encoding.
            FileWriter fileWriter =
                new FileWriter(fileName);

            // Always wrap FileWriter in BufferedWriter.
            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);

            // Note that write() does not automatically
            // append a newline character.
            bufferedWriter.write("Hello there,");
            bufferedWriter.write(" here is some text.");
            bufferedWriter.newLine();
            bufferedWriter.write("We are writing");
            bufferedWriter.write(" the text to the file.");

            // Always close files.
            bufferedWriter.close();
        }
        catch(IOException ex) {
            System.out.println(
                "Error writing to file '"
                + fileName + "'");
            // Or we could just do this:
            // ex.printStackTrace();
        }
    }
你想写什么就写什么。

怎么样

// The name of the file to open.
        String fileName = "temp.txt";

        try {
            // Assume default encoding.
            FileWriter fileWriter =
                new FileWriter(fileName);

            // Always wrap FileWriter in BufferedWriter.
            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);

            // Note that write() does not automatically
            // append a newline character.
            bufferedWriter.write("Hello there,");
            bufferedWriter.write(" here is some text.");
            bufferedWriter.newLine();
            bufferedWriter.write("We are writing");
            bufferedWriter.write(" the text to the file.");

            // Always close files.
            bufferedWriter.close();
        }
        catch(IOException ex) {
            System.out.println(
                "Error writing to file '"
                + fileName + "'");
            // Or we could just do this:
            // ex.printStackTrace();
        }
    }

你想写什么就写什么。

这就是你要找的

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter 5 Grades:");
    System.out.println("");

    FileWriter fw = new FileWriter("/var/tmp/test.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.newLine();

    double a, b, c, d, e; //5 Grades
    double sum = 0;     // total
    double avg;        // percentage
    double min = 1;    // lowest grade
    double max = 100;  // highest grade

    //Calculations
    a = keyboard.nextDouble();
    b = keyboard.nextDouble();
    c = keyboard.nextDouble();
    d = keyboard.nextDouble();
    e = keyboard.nextDouble();
    keyboard.close();

    ArrayList<Double> list = new ArrayList<>();
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);
    list.add(e);
    sum = (a+b+c+d+e);
    System.out.println("Total Score=:"+sum);
    bw.append("Total Score=:"+sum);
    bw.newLine();
    avg =(a+b+c+d+e)/5;
    System.out.println("Percentage =: "+avg); 
    bw.append("Percentage =: "+avg);
    bw.newLine();
    max = Collections.max(list);  
    System.out.println("Highest Score= " +max);
    bw.append("Highest Score= " +max);
    bw.newLine();
    min = Collections.min(list);
    System.out.println("Lowest Score= " +min);
    bw.append("Lowest Score= " +min);
    bw.newLine();
    if (avg >= 90){
       System.out.println("Grade= A");
       bw.append("Grade= A");
       bw.newLine();
    }
       else if (avg >=80){
       System.out.println("Grade= B");
       bw.append("Grade= B");
       bw.newLine();
       }
       else if (avg >= 70){
       System.out.println("Grade= C");
       bw.append("Grade= C");
       bw.newLine();
       }
       else if (avg >= 60){
       System.out.println("Grade= D");
       bw.append("Grade= D");
       bw.newLine();
       }
       else {
       System.out.println("Grade= F");
       bw.append("Grade= F");
       bw.newLine();
       }
    bw.close();

这就是你要找的

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter 5 Grades:");
    System.out.println("");

    FileWriter fw = new FileWriter("/var/tmp/test.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.newLine();

    double a, b, c, d, e; //5 Grades
    double sum = 0;     // total
    double avg;        // percentage
    double min = 1;    // lowest grade
    double max = 100;  // highest grade

    //Calculations
    a = keyboard.nextDouble();
    b = keyboard.nextDouble();
    c = keyboard.nextDouble();
    d = keyboard.nextDouble();
    e = keyboard.nextDouble();
    keyboard.close();

    ArrayList<Double> list = new ArrayList<>();
    list.add(a);
    list.add(b);
    list.add(c);
    list.add(d);
    list.add(e);
    sum = (a+b+c+d+e);
    System.out.println("Total Score=:"+sum);
    bw.append("Total Score=:"+sum);
    bw.newLine();
    avg =(a+b+c+d+e)/5;
    System.out.println("Percentage =: "+avg); 
    bw.append("Percentage =: "+avg);
    bw.newLine();
    max = Collections.max(list);  
    System.out.println("Highest Score= " +max);
    bw.append("Highest Score= " +max);
    bw.newLine();
    min = Collections.min(list);
    System.out.println("Lowest Score= " +min);
    bw.append("Lowest Score= " +min);
    bw.newLine();
    if (avg >= 90){
       System.out.println("Grade= A");
       bw.append("Grade= A");
       bw.newLine();
    }
       else if (avg >=80){
       System.out.println("Grade= B");
       bw.append("Grade= B");
       bw.newLine();
       }
       else if (avg >= 70){
       System.out.println("Grade= C");
       bw.append("Grade= C");
       bw.newLine();
       }
       else if (avg >= 60){
       System.out.println("Grade= D");
       bw.append("Grade= D");
       bw.newLine();
       }
       else {
       System.out.println("Grade= F");
       bw.append("Grade= F");
       bw.newLine();
       }
    bw.close();
你可以用PrintWriter

基本上,只需在writer.println语句中抛出要放入文件中的任何内容

您可以使用PrintWriter


基本上,只需在writer.println语句中抛出要放入文件中的任何内容

好的,那么你现在要做的就是写一个文件?你能重新格式化你的代码吗?选择它并按CTRL+K。您能修复if-else-if块上的缩进吗?我本来打算编辑你的帖子,但我不想意外地添加一些本来就不存在的东西。所以现在你只需要写一个文本文件?但是我也看到你有6个键盘。下一步,当你说你只想读5年级的时候,顺便问一下,你说剩下的不起作用是什么意思?还有,你需要计算多少次平均值和其他数据?对于你正在阅读的五个年级来说,这只是一次吗?按CTRL+K组合键对格式设置没有任何作用,但我还是这样做了。常数a-e只有5个,不确定第6个是指什么。一旦我输入给定的5个分数并点击返回,它应该计算每个总数、百分比等,它什么也不做。至于if-else语句中的缩进,程序是如何编写它们的,但是如果我需要以某种方式为您修复它们,请告诉我您希望如何使用它们。每个测试的结果都需要写入一个文本文件,但这是我想首先修复的最后一步。谢谢,所以你现在要做的就是写一个文件?你能重新格式化你的代码吗?选择它并按CTRL+K。您能修复if-else-if块上的缩进吗?我本来打算编辑你的帖子,但我不想意外地添加一些本来就不存在的东西。所以现在你只需要写一个文本文件?但是我也看到你有6个键盘。下一步,当你说你只想读5年级的时候,顺便问一下,你说剩下的不起作用是什么意思?还有,你需要计算多少次平均值和其他数据?对于你正在阅读的五个年级来说,这只是一次吗?按CTRL+K组合键对格式设置没有任何作用,但我还是这样做了。常数a-e只有5个,不确定第6个是指什么。一旦我输入给定的5个分数并点击返回,它应该计算每个总数、百分比等,它什么也不做。至于if-else语句中的缩进,程序是如何编写它们的,但是如果我需要以某种方式为您修复它们,请告诉我您希望如何使用它们。每个测试的结果都需要写入一个文本文件,但这是我想首先修复的最后一步。谢谢这不是OP要求的。这不是OP要求的。