使用用户输入计算分数

时间:2015-10-21 13:07:00

标签: java

我需要能够完成这个程序。错过课后我完全迷失了。我需要能够输出总计,百分比,最高,最低和等级的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");
             }
 }
}

修改

3 个答案:

答案 0 :(得分:3)

怎么样

// 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();
        }
    }

然后写下你想要的任何内容。

答案 1 :(得分:1)

这就是你要找的东西;)

    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();

答案 2 :(得分:0)

您可以使用PrintWriter

try {

    PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
    writer.println("The first line");
    writer.println("The second line");
    writer.close();
}
catch(IOException ex) {
    System.out.println("Error writing to file");
}

基本上,只需在writer.println()语句中抛出您想要放入文件的内容即可。