格式化阵列的输出,到柱 [英] Formatting the output of arrays, into a column

查看:94
本文介绍了格式化阵列的输出,到柱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要在输出格式为特定的时尚一些帮助。我满足了分配的所有要求,除了输出。

我对这篇文章的格式错误道歉,任何建议和帮助是很大的AP preciated。

我需要的输出如下:


  

先生。随机 - $ 500强结果
  BobRandom先生 - $ 250结果
  等-8次结果
  (总和所有美元)结果


我的继承人code:

 进口javax.swing.JOptionPane中;
进口java.util.Arrays中;公共类PeerTutors {
    公共静态无效的主要(字串[] args){
        的String [] = myNameArray新的String [2];
        的String [] = myGradeArray新的String [2];
        双[] = myStudentArray新的双[2];
        双[] =津贴,新的双[2];
        INT总和= 0;        对(INT I = 0; I&2;我++){
            myNameArray [I] =(JOptionPane的
                    .showInputDialog(NULL,
                            输入对等导师名字,姓氏再其次是名字。));        }
        Arrays.sort(myNameArray,String.CASE_INSENSITIVE_ORDER);
        JOptionPane.showMessageDialog(空的学生辅导员的名称是:
                + Arrays.toString(myNameArray));        为(int类型的= 0;一个2;一个++){
            myGradeArray [A] = JOptionPane.showInputDialog(NULL,
                    请输入最大程度赢得了
                            + myNameArray [一个]);
        }
        JOptionPane.showMessageDialog(空的学生辅导员的名称是:
                + Arrays.toString(myNameArray)+ Arrays.toString(myGradeArray));        对(INT B = 0; B&2; b ++){
            myStudentArray并[b] = Double.parseDouble(JOptionPane.showInputDialog(
                    空,请输入学生的数量
                            + myNameArray [B] +协助));
        }
        JOptionPane.showMessageDialog(NULL,
                该学生辅导员的报告:+ Arrays.toString(myNameArray)
                        +和+ Arrays.toString(myStudentArray)
                        +)儿童家教        对于(INT C = 0; C&2; C ++){
            如果(myGradeArray [C] .equals(BS)){
                助学金[C] = 9.5 * myStudentArray [C];            }否则如果(myGradeArray [C] .equals(MS)){
                助学金[C] = 15 * myStudentArray [C];            }其他{
                助学金[C] = 20 * myStudentArray [C];
            }
        }
        对于(双D:津贴){
            总和+ = D;
        }        为(中间体D = 0; D&2; d ++){
            JOptionPane.showMessageDialog(NULL,myNameArray [D]。+ -
                    +助学金[D]。+\\ n);        }
    }
}


解决方案

基本上,每一列,你需要一些类型的列表(数组或preferably一个列表)。您还需要保持对每个列的最大宽度信息(我通常把这个在另一列表)。

您需要循环的每一行,打破下来到它的列元素,并把每个元素相应的列列表。你也应该为你去计算每列的宽度要求。

一旦你有了这一切构成了,你需要再重构出来放,使用类似一个的StringBuilder 来重建输出的每一行,但让每列具有所需的额外的间隔,以允许柱中流动。

更新,其中例如

现在,通常情况下,我想每一行存储为单个对象,因为它可以更容易维护的行总数的一致想法,但是,你使用code作为一个跳点...

 进口java.text.NumberFormat中;公共类TestTextColumnFormatting {    公共静态无效的主要(字串[] args){
        的String [] = myNameArray新的String [] {
            Samete先生
            Torbura女士
            Perem先生
            Rothaw先生
            小姐。Endemos
            Chomoshon先生
            Aldir夫人,};
        双[] =津贴新的双[] {
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        的NumberFormat NF = NumberFormat.getCurrencyInstance();
        双理货= 0;        INT列宽[] =新INT [2];
        对于(INT指数= 0;指数 - LT; myNameArray.length;指数++){
            列宽[0] = Math.max(列宽[0],myNameArray [指数]。长度());
            列宽[1] = Math.max(列宽[1],nf.format(津贴[指数])长());
            理货+ =津贴[指数]
        }        列宽[1] = Math.max(列宽[1],nf.format(讯号)。长度());        StringBuilder的SB =新的StringBuilder(128);
        对于(INT指数= 0;指数 - LT; myNameArray.length;指数++){            字符串名称= myNameArray [指数]
            字符串津贴= nf.format(津贴[指数]);            sb.append(名称).append(填充(姓名,列宽[0],''))附加()。
            sb.append(补(津贴,列宽[1],''))追加(津贴)。
            sb.append(\\ n);        }
        sb.append(填充(,列宽[0],''))附加。();
        sb.append(填充(,列宽[1],'='))附加。(\\ n);
        sb.append(填充(,列宽[0],''))附加。();
        sb.append(补(nf.format(估计),列宽[1],''))追加(nf.format(讯号))。
        sb.append(\\ n);        的System.out.println(sb.toString());    }    公共静态字符串填充(S值的字符串,INT iMinLength,焦炭含){        StringBuilder的充满=新的StringBuilder(iMinLength);        而(filled.length()&下; iMinLength - sValue.length()){            filled.append(含);        }        返回filled.toString();    }
}

这将输出类似...

 先生。 Samete $ 500.00
Torbura女士$ 200.00
Perem先生$ 300.00
Rothaw先生$ 1,000.00
小姐。Endemos $ 250.00
Chomoshon先生$ 125.00
Aldir夫人$ 600.00
              =========
              $ 2,975.00

更新

我是个白痴,我只是意识到你正在使用的JOptionPane :P

最简单的解决办法是建立一个HTML表格,让摇摆使其为你...

 进口java.text.NumberFormat中;
进口javax.swing.JOptionPane中;公共类TestTextColumnFormatting {    公共静态无效的主要(字串[] args){
        的String [] = myNameArray新的String [] {
            Samete先生
            Torbura女士
            Perem先生
            Rothaw先生
            小姐。Endemos
            Chomoshon先生
            Aldir夫人,};
        双[] =津贴新的双[] {
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        的NumberFormat NF = NumberFormat.getCurrencyInstance();
        双理货= 0;        StringBuilder的SB =新的StringBuilder(128);
        sb.append(< HTML和GT;<表格边框='0'>中);
        对于(INT指数= 0;指数 - LT; myNameArray.length;指数++){            字符串名称= myNameArray [指数]
            字符串津贴= nf.format(津贴[指数]);            sb.append(< TR>< TD ALIGN =左>中);
            sb.append(名);
            sb.append(< / TD>< TD ALIGN ='右'>中);
            sb.append(津贴);
            sb.append(&下; / TD>&下; / TR>中);            理货+ =津贴[指数]        }
        sb.append(< TR>< TD ALIGN =中心>中);
        sb.append(< / TD>< TD ALIGN ='右'>中);
        sb.append(nf.format(估计));
        sb.append(&下; / TD>&下; / TR>中);        JOptionPane.showMessageDialog(NULL,sb.toString());    }
}

I just needed some assistance on formatting the output into a specific fashion. I satisfied all the requirements of the assignment, except the output.

I apologize for the bad format of this post, any suggestions and help is greatly appreciated.

I need the output to look like:

Mr. Random - $500
Mr. BobRandom - $250
etc-8 more times
(sum total of all dollars)

Heres my code:

import javax.swing.JOptionPane;
import java.util.Arrays;

public class PeerTutors {
    public static void main(String[] args) {
        String[] myNameArray = new String[2];
        String[] myGradeArray = new String[2];
        double[] myStudentArray = new double[2];
        double[] stipend = new double[2];
        int sum = 0;

        for (int i = 0; i < 2; i++) {
            myNameArray[i] = (JOptionPane
                    .showInputDialog(null,
                            "Enter the Peer Tutor name, last name then followed by the first name."));

        }
        Arrays.sort(myNameArray, String.CASE_INSENSITIVE_ORDER);
        JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
                + Arrays.toString(myNameArray));

        for (int a = 0; a < 2; a++) {
            myGradeArray[a] = JOptionPane.showInputDialog(null,
                    "Please enter the highest degree earned for: "
                            + myNameArray[a]);
        }
        JOptionPane.showMessageDialog(null, "The Peer Tutors Names are :"
                + Arrays.toString(myNameArray) + Arrays.toString(myGradeArray));

        for (int b = 0; b < 2; b++) {
            myStudentArray[b] = Double.parseDouble(JOptionPane.showInputDialog(
                    null, "Please enter the number of students"
                            + myNameArray[b] + " has assisted"));
        }
        JOptionPane.showMessageDialog(null,
                "The Peer Tutors Report: " + Arrays.toString(myNameArray)
                        + "with" + Arrays.toString(myStudentArray)
                        + " of children tutoring");

        for (int c = 0; c < 2; c++) {
            if (myGradeArray[c].equals("BS")) {
                stipend[c] = 9.5 * myStudentArray[c];

            } else if (myGradeArray[c].equals("MS")) {
                stipend[c] = 15 * myStudentArray[c];

            } else {
                stipend[c] = 20 * myStudentArray[c];
            }
        }
        for (double d : stipend) {
            sum += d;
        }

        for (int d = 0; d < 2; d++) {
            JOptionPane.showMessageDialog(null, myNameArray[d] + "-"
                    + stipend[d] + "\n");

        }
    }
}

解决方案

Basically, for each column, you need some kind of list (either an array or preferably an List). You also need to maintain information on each columns max-width (I typically put this in another List).

You need to loop each line, breaking to down into it's column elements and place each element in the corresponding column List. You should also be calculating the width requirements for each column as you go.

Once you have this all structured out, you need to then reconstruct the out put, using something like a StringBuilder to rebuild each line of the output but so that each column has the required additional spacing to allow the columns to flow.

Updated with example

Now, normally, I would store each row as an individual object, as it makes it easier to maintain a consistent idea of the total number of rows, but, using you code as a jumping off point...

import java.text.NumberFormat;

public class TestTextColumnFormatting {

    public static void main(String[] args) {
        String[] myNameArray = new String[]{
            "Mr. Samete",
            "Ms. Torbura",
            "Mr. Perem",
            "Mr. Rothaw",
            "Miss. Endemos",
            "Mr. Chomoshon",
            "Mrs. Aldir",};
        double[] stipends = new double[]{
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        double tally = 0;

        int columnWidths[] = new int[2];
        for (int index = 0; index < myNameArray.length; index++) {
            columnWidths[0] = Math.max(columnWidths[0], myNameArray[index].length());
            columnWidths[1] = Math.max(columnWidths[1], nf.format(stipends[index]).length());
            tally += stipends[index];
        }

        columnWidths[1] = Math.max(columnWidths[1], nf.format(tally).length());

        StringBuilder sb = new StringBuilder(128);
        for (int index = 0; index < myNameArray.length; index++) {

            String name = myNameArray[index];
            String stipend = nf.format(stipends[index]);

            sb.append(name).append(fill(name, columnWidths[0], ' ')).append(" ");
            sb.append(fill(stipend, columnWidths[1], ' ')).append(stipend);
            sb.append("\n");

        }


        sb.append(fill("", columnWidths[0], ' ')).append(" ");
        sb.append(fill("", columnWidths[1], '=')).append("\n");
        sb.append(fill("", columnWidths[0], ' ')).append(" ");
        sb.append(fill(nf.format(tally), columnWidths[1], ' ')).append(nf.format(tally));
        sb.append("\n");

        System.out.println(sb.toString());

    }

    public static String fill(String sValue, int iMinLength, char with) {

        StringBuilder filled = new StringBuilder(iMinLength);

        while (filled.length() < iMinLength - sValue.length()) {

            filled.append(with);

        }

        return filled.toString();

    }
}

This will then output something like...

Mr. Samete      $500.00
Ms. Torbura     $200.00
Mr. Perem       $300.00
Mr. Rothaw    $1,000.00
Miss. Endemos   $250.00
Mr. Chomoshon   $125.00
Mrs. Aldir      $600.00
              =========
              $2,975.00

Updated

I'm such an idiot, I just realised that you're using a JOptionPane :P

The simplest solution would be to construct a HTML table and let Swing render it for you...

import java.text.NumberFormat;
import javax.swing.JOptionPane;

public class TestTextColumnFormatting {

    public static void main(String[] args) {
        String[] myNameArray = new String[]{
            "Mr. Samete",
            "Ms. Torbura",
            "Mr. Perem",
            "Mr. Rothaw",
            "Miss. Endemos",
            "Mr. Chomoshon",
            "Mrs. Aldir",};
        double[] stipends = new double[]{
            500,
            200,
            300,
            1000,
            250,
            125,
            600,
            338
        };
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        double tally = 0;

        StringBuilder sb = new StringBuilder(128);
        sb.append("<html><table border='0'>");
        for (int index = 0; index < myNameArray.length; index++) {

            String name = myNameArray[index];
            String stipend = nf.format(stipends[index]);

            sb.append("<tr><td align='left'>");
            sb.append(name);
            sb.append("</td><td align='right'>");
            sb.append(stipend);
            sb.append("</td></tr>");

            tally += stipends[index];

        }
        sb.append("<tr><td align='center'>");
        sb.append("</td><td align='right'>");
        sb.append(nf.format(tally));
        sb.append("</td></tr>");

        JOptionPane.showMessageDialog(null, sb.toString());

    }
}

这篇关于格式化阵列的输出,到柱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆