java - 二维数组格式问题。我试图用/t 来标记我的列,但这只会导致我的行消失

标签 java

我有一个 3 列的二维数组。当它打印时,我需要它来制表。我在下一行使用 /t/n ,但它显示在一条直线上,而不是停留在行中。我将输入我的代码。我该如何修复它?

package presidnetssort;
import java.io.*;

import java.util.*;

``import edu.princeton.cs.introcs.*;
    public class PresidentsSort 
    {



        public static void main(String[] args) throws FileNotFoundException 
        {
            //Create scanner object to read the file.
            Scanner x = new Scanner(new File("PresidentsGrades.txt"));

            //Create a string array to hold the data from the file.
            String[][] a = new String[44][3];

            //Create a loop to to add the file data to the array.
            for (int i=0;i<a.length;i++)
            {
                a[i][0]=x.next();
                a[i][1]=x.next();
                a[i][2]=x.next();
           }
            //Call the sorting methods. I am commenting them out. Uncomment     them one at a time to use them.
            //sortNames(a);
            //sortCourses(a);
            //sortGrades(a);
            //Call the print method to print the a array.
            print(a);
            StdOut.printf("/t,/t/n",a);

        }

    public static void sortNames(String[][] a)
    {


        Arrays.toString(a);
        StdOut.printf("/t",a);
    }
    public static void sortCourses(String[][] a)
    {

        Arrays.toString(a);
        print(a);
    }
    public static void sortGrades(String[][] a)
    {

        Arrays.toString(a);
        print(a);
    }
     static void print(String[][] a)
     {
            for (int i=0;i<a.length;i++)
            {
                for (int j=0;j<a[0].length;j++) 
                    System.out.print(a[i][j]+" ");
                 StdOut.printf("/t,/t/n",a);
            }
      }
     }

我的输出显示为: Wasington 算法 98/t,/t/nAdams 数学 89/t,/t/nJefferson Java 85/t,/t/nMadison C# 83/t,/t/nMonroe Web 88/t,/t/nAdams 算法 81/t ,/t/n

当我取消格式设置并且不使用 StdOut.print 时,它会按照应有的方式按行打印,但不会按选项卡显示,如下所示:

华盛顿算法 98 亚当斯数学 89 杰斐逊 Java 85 麦迪逊 C# 83 门罗网 88 亚当斯算法 81 jackson java 78 布伦 C# 75 哈里森计算机 45 泰勒算法 73 波尔克 java 68 泰勒算法 88 菲尔莫尔网 93 PIL 斯数学 56 布坎南算法 77 林肯算法 99 约翰逊 C# 76 格兰特算法 92 海耶斯网 76 加菲猫电脑 66 亚瑟·韦伯 77 克利夫兰 java 88 哈里森算法 75 克利夫兰 C# 91 麦金莱算法 100 罗斯福网 79 塔夫脱 C# 86 威尔逊 C# 84 哈丁网 67 柯立芝数学 67 胡佛历史 76 罗斯福历史 84 杜鲁门数学83 艾森豪威尔历史 100 肯尼迪 java 95 约翰逊算法 56 尼克松历史 23 福特历史 78 卡特数学 55 里根算法 99 布什算法 89 克林顿历史 45 布什数学 75 奥巴马历史 13

最佳答案

制表符和换行符分别为\t\n,更改

StdOut.printf("/t,/t/n",a);

类似于

StdOut.printf("\t,\t\n",a);

StdOut.printf("/t",a);

StdOut.printf("\t",a);

等等...

关于java - 二维数组格式问题。我试图用/t 来标记我的列,但这只会导致我的行消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665849/

相关文章:

java - 一次性密码安全风险

java - 如何按用户编号对用户的 ArrayList 进行排序

java - 将 44100hz、16 位单声道 wav 文件转换并分段为 16kHz、16 位单声道 wav 文件

java - 为什么 HashSet 对单个字母字符进行排序?

java.lang.IllegalArgumentException : The resource path [vsj. 属性] 无效

java - 如何将字符串日期转换为util.date格式?

java - java中的ArrayList数组

java - 在浏览器中调试 java applet - 在 Eclipse 中工作但在浏览器中不工作

java - JSTL 将字符串视为表达式语言

java - 如何将另一个包的类导入到我的包中