有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在数组中显示等级?

教授的样本输出: Sample output given scores.txt file

到目前为止,我的代码是:

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

        System.out.println("Which file would you like to read?");

        Scanner keyboard = new Scanner(System.in);
        String name = keyboard.next();
        Scanner reader = new Scanner(new File(name));

        double numbers[][] = new double[reader.nextInt()+1][reader.nextInt()+1]; //+1 for averages

        //loop over all the rows
        for (int i=0; i<numbers.length; i++) { 
            //loop over all the columns in row i
            for (int j=0; j<numbers[i].length; j++) { 
                //this code will execute on each and every cell row i, column j
                numbers[i][j] = reader.nextDouble();
            }
            double sum = 0;
            double average = (sum/(numbers.length));
        }
        //loop over all the rows
        for (int i = 0; i<numbers.length-1; i++) { 
            System.out.println("Assignment #: ");
            System.out.println("array 1, 2, 3, 4, 5, 6, Avg");
            System.out.println("Student 1: ");
            System.out.println("Student 2: ");
            System.out.println("Student 3: ");
            System.out.println("Student 4: ");
            System.out.println("Student 5: ");
            System.out.println("Average: ");

            for (int j=0; j<numbers[i].length-1; j++) { //loop over all the columns in row i
                System.out.print(numbers[i][j] + " "); //print out the cell value and then a space
            }
            //System.out.println("The overall class average is " + (totalResults/(numbers.length))); //the class average
            DecimalFormat numPattern = new DecimalFormat("##.#");
        }
        //overall average code here
    }
}

为了正确显示所有内容,需要显示数组,以便标题和打印行都有意义。为此,我在不了解如何显示数据的地方放置了一些占位符

虽然上面的代码已经编译好了,但是当分数出现时,它给了我一个异常错误。输入txt文件作为名称——工作目录包含该文件,所以我也不确定为什么它在程序中没有移动过去

我想,如果我能克服这个障碍,我将能够很好地显示数组,但我寻求堆栈对我的代码语法外观的建议。有什么改进意见吗


共 (3) 个答案

  1. # 1 楼答案

    你可以使用String.format()

    System.out.print(String.format("%10s", numbers[i][j]+""));
    

    你可以试试看

      System.out.println(String.format("%50s","Assignment #: "));
      System.out.println(String.format("%10s%10s%10s%10s%10s%10s%10s%10s","array", "1", "2", "3", "4", "5", "6", "Avg"));
      System.out.println(String.format("%10s","Student 1: "));
      System.out.println(String.format("%10s","Student 2: "));
      System.out.println(String.format("%10s","Student 3: "));
      System.out.println(String.format("%10s","Student 4: "));
      System.out.println(String.format("%10s","Student 5: "));
      System.out.println(String.format("%10s","Student 6: "));
    

    顺便说一句,我认为你打印表格的for循环是错误的
    在代码中,“赋值#::”和“数组1,2,3,4,5,6,Avg”将重复
    此外,在开始新的一行之前,你应该打印学生的分数

    打印标记的for循环的伪代码:

    print "Assignment #:"
    print " array 1, 2, 3, 4, 5, 6, Avg "
    for i= 1-5 //rows
        print "Student " + i
        for j= 1-6 //column
            print score of assignment j of student i
        end for
        print avg
    end for
    print "Overall Average" + overallAverage
    
  2. # 2 楼答案

    使用\t在文本中放置制表符

    System.out.println("\t\t1\t2\t3\t4\t5\t6\tAvg");
    

    将输出:

              1    2    3    4    5    6    Avg
    

    您还可以添加一个焊盘:

    public static void main(String args[]) throws Exception {
     System.out.println(padRight("Howto", 20) + "*");
     System.out.println(padLeft("Howto", 20) + "*");
    }
    /*
      output :
         Howto               *
                        Howto*
    */
    

    Check this question了解有关填充的更多信息String

    提示:为了获得结果,你必须打印每行的所有信息,并将其排成一行将每个学生的信息放在一个变量中,并在最后打印出来

    Check this answer知道如何操作String和使用StringBuilder

  3. # 3 楼答案

    要格式化输出,请使用String.format().

    System.out.println(String.format("%4d", 5));// for int
    System.out.println(String.format("%-20s= %s" , "label", "content" ));//for string
    

    在哪里

    %s is a placeholder for you string.

    The '-' makes the result left-justified.

    20 is the width of the first string

    see link

    另一个问题是关于文件打开的问题

    只给出文件名没有帮助。试着给出整条路

    使用System.getProperty("user.dir");获取路径

    String name = keyboard.next();
    String path = System.getProperty("user.dir");
    Scanner reader = new Scanner(new File(path + " \\ " + name));