格式已使用,但输出仍不在整齐的列中

huangapple go评论103阅读模式
英文:

formatting used, but output is still not in a neat column

问题

以下是您提供的代码的翻译部分:

import java.io.*;
import java.util.Scanner;

public class Topic_HW5 {
    
    public static String oneGameScore(int x){
        String scr = " ";
        if (0 <= x && x < 50) 
            scr = "Horrible";
        if (50 <= x && x <= 99) 
            scr = "Poor";
        if (100 <= x && x <= 139) 
            scr = "Good";
        if (140 <= x && x <= 199) 
            scr = "Very Good";
        if (200 <= x && x <= 249) 
            scr = "Excellent";
        if (250 <= x && x <= 300) 
            scr = "Pro";
        if (x < 0)
            scr = "Illegal <0";
        if (300 < x)
            scr = "Illegal <300";
        return scr;
    }
    
    public static int avg3Scores(int x, int y, int z) {
        int avg = (x + y + z) / 3;
        return avg;
    }

    public static boolean validGroup(int x, int y, int z, PrintWriter outputFile) {
        boolean validGroup = false;
        
        if (0 <= x && x <= 300)
        if (0 <= y && y <= 300)
        if (0 <= z && z <= 300)
            validGroup = true;
        else
            validGroup = false;
        return validGroup;
    }
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        // 创建输出文件
        PrintWriter outputFile = new PrintWriter("hw5out.txt");
        File myinputFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\hw5input.txt");
        Scanner inputFile = new Scanner(myinputFile);
        int score1, score2, score3, avg, avgrtg;
        int t, f = 0;
        boolean valid;
        String rtg, rtg2, rtg3, grpavg = " ";
        outputFile.printf("%50s", "TABLE OF BOWLING SCORES");
        // outputFile.printf("\nScore 1\tScore 2\tScore 3\t Valid?\tRating1\tRating2\tRating3");
        outputFile.printf("\n%1s %7s %7s %7s %8s %8s %8s     %12s %14s", "\nScore 1", 
                "Score 2", "Score 3", "Valid?", "  Rating1", "     Rating2", "Rating3", "Avg Score", "Avg Rating");
        // "\n%1s %7s %7s %7s %8s %8s %8s", "\nScore 1", "Score 2", "Score 3", "Valid?", "Rating1", "Rating2", "Rating3"
        System.out.println("What was score 1? ");
        score1 = inputFile.nextInt();
        while (score1 != -1) {
            System.out.println("What was score 2? ");
            score2 = inputFile.nextInt();
            System.out.println("What was score 3? ");
            score3 = inputFile.nextInt();
            valid = validGroup(score1, score2, score3, outputFile);
            rtg = oneGameScore(score1);
            rtg2 = oneGameScore(score2);
            rtg3 = oneGameScore(score3);
            avg = avg3Scores(score1, score2, score3);
            avgrtg = avg3Scores(score1, score2, score3);
            grpavg = oneGameScore(avgrtg);
            outputFile.println("\n");
            outputFile.printf("%1d %7d %7d %9b     %-12s %-8s %-8s %12d %16s",
                    score1, score2, score3, valid, rtg, rtg2, rtg3, avg, grpavg);
            
            System.out.println("What was score 1? ");
            score1 = inputFile.nextInt();
        }
        outputFile.println("\nThe program is complete!");
        inputFile.close();
        outputFile.close();
        outputFile.flush();
    }
}

在这段代码中,最后两列可能出现排版问题。您提到尝试使用格式化而不是制表符和空格来打印它们整齐,但它们似乎被之前的数据推向一侧。这种问题可能是因为您在格式化输出时,使用了不同长度的字符串,导致列对齐不正确。

为了解决此问题,您可以使用格式化字符串中的字段宽度来确保列对齐。在 outputFile.printf 语句中,您可以调整 %12s%16s 的字段宽度以使它们与之前的列对齐。根据您的需求,可以适当增加或减少这些字段宽度,以获得所需的列对齐效果。

英文:

I am writing code to get the information of bowling scores and print it into a neattable. I am trying to get the average rating and average score to print in a neat column. Here is my code.my input file my output file

import java.io.*;
import java.util.Scanner;
public class Topic_HW5 {
public static String oneGameScore(int x){
String scr = &quot; &quot;;
if (0&lt;=x&amp;&amp;x&lt;50) 
scr = &quot;Horrible&quot;;
if (50&lt;=x&amp;&amp;x&lt;=99) 
scr = &quot;Poor&quot;;
if (100&lt;=x&amp;&amp;x&lt;=139) 
scr = &quot;Good&quot;;
if (140&lt;=x&amp;&amp;x&lt;=199) 
scr = &quot;Very Good&quot;;
if (200&lt;=x&amp;&amp;x&lt;=249) 
scr = &quot;Excellent&quot;;
if (250&lt;=x&amp;&amp;x&lt;=300) 
scr = &quot;Pro&quot;;
if(x&lt;0)
scr= &quot;Illegal &lt;0&quot;;
if(300&lt;x)
scr=&quot;Illegal &lt;300&quot;;
return scr;
}
public static int avg3Scores(int x, int y, int z) {
int avg=(x+y+z)/3;
return avg;
}
public static boolean validGroup(int x,int y, int z, PrintWriter outputFile) {
boolean validGroup=false;
if(0&lt;=x&amp;&amp;x&lt;=300)
if(0&lt;=y&amp;&amp;y&lt;=300)
if (0&lt;=z&amp;&amp;z&lt;=300)
validGroup=true;
else
validGroup=false;
return validGroup;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//create outputFile
PrintWriter outputFile= new PrintWriter(&quot;hw5out.txt&quot;);
File myinputFile = new File(&quot;C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\hw5input.txt&quot;);
Scanner inputFile = new Scanner(myinputFile);
int score1, score2, score3,avg,avgrtg;
int t,f=0;
boolean valid;
String rtg,rtg2,rtg3,grpavg= &quot; &quot;;
outputFile.printf(&quot;%50s&quot;,&quot;TABLE OF BOWLING SCORES&quot;);
//outputFile.printf(&quot;\nScore 1\tScore 2\tScore 3\t Valid?\tRating1\tRating2\tRating3&quot;);
outputFile.printf(&quot;\n%1s %7s %7s %7s %8s %8s %8s     %12s %14s&quot;, &quot;\nScore 1&quot;, 
&quot;Score 2&quot;, &quot;Score 3&quot;, &quot;Valid?&quot;, &quot;  Rating1&quot;,&quot;     Rating2&quot;, &quot;Rating3&quot;, &quot;Avg Score&quot;, &quot;Avg Rating&quot;);
//&quot;\n%1s %7s %7s %7s %8s %8s %8s&quot;, &quot;\nScore 1&quot;, &quot;Score 2&quot;, &quot;Score 3&quot;, &quot;Valid?&quot;, &quot;Rating1&quot;,&quot;Rating2&quot;, &quot;Rating3&quot;
System.out.println(&quot;What was score 1? &quot;);
score1 = inputFile.nextInt();
while(score1 != -1) {
System.out.println(&quot;What was score 2? &quot;);
score2 = inputFile.nextInt();
System.out.println(&quot;What was score 3? &quot;);
score3 = inputFile.nextInt();
valid=validGroup(score1,score2,score3,outputFile);
rtg=oneGameScore(score1);
rtg2=oneGameScore(score2);
rtg3=oneGameScore(score3);
avg=avg3Scores(score1,score2,score3);
avgrtg=avg3Scores(score1,score2,score3);
grpavg=oneGameScore(avgrtg);
outputFile.println(&quot;\n&quot;);
outputFile.printf(&quot;%1d %7d %7d %9b     %-12s %-8s %-8s %12d %16s&quot;,
score1,score2,score3,valid,rtg,rtg2,rtg3,avg, grpavg);
System.out.println(&quot;What was score 1? &quot;);
score1 = inputFile.nextInt();
}
outputFile.println(&quot;\nThe program is complete!&quot;);
inputFile.close();
outputFile.close();
outputFile.flush();
}
}

AS you can see, the last two columns are all out of wack. I tried using formatting instead of \t's and spaces to print them neatly, expecting them to print in a straight column. Instead, they seem to be getting pushed to the side by previous data.I was wondering why this happens and how I can fix it

答案1

得分: 1

首先,在while条件中有一个小改进:

while(inputFile.hasNextInt()) {
    System.out.println("What was score 1? ");
    score1 = inputFile.nextInt();
    System.out.println("What was score 2? ");
    score2 = inputFile.nextInt();
    System.out.println("What was score 3? ");
    score3 = inputFile.nextInt();
    ...

这样你就不必在每次循环结束时检查是否有另一个整数或与-1进行比较了。

对于格式掩码:

String headerFormatMask = "%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s";
String formatMask = "%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s";
outputFile.printf(headerFormatMask,
        "Score 1",
        "Score 2",
        "Score 3",
        "Valid?",
        "Rating1",
        "Rating2",
        "Rating3",
        "Avg Score",
        "Avg Rating");
...
while (inputFile.hasNextInt()) {
   ...
   outputFile.println("\n");
   outputFile.printf(formatMask,
           score1,
           score2,
           score3,
           valid,
           rtg,
           rtg2,
           rtg3,
           avg,
           grpavg);
}

注意标题格式掩码和每行的格式掩码是平衡的。

总的来说,在适应后的main方法中的代码如下:

// todo change input / output files as necessary
PrintWriter outputFile = new PrintWriter("output.txt");
File myinputFile = new File("input.txt");
Scanner inputFile = new Scanner(myinputFile);
int score1, score2, score3, avg, avgrtg;
int t, f = 0;
boolean valid;
String rtg, rtg2, rtg3, grpavg = " ";
outputFile.printf("%50s", "TABLE OF BOWLING SCORES");
outputFile.printf("\n");
String headerFormatMask = "%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s";
String formatMask = "%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s";
outputFile.printf(headerFormatMask,
        "Score 1",
        "Score 2",
        "Score 3",
        "Valid?",
        "Rating1",
        "Rating2",
        "Rating3",
        "Avg Score",
        "Avg Rating");
while(inputFile.hasNextInt()) {
    System.out.println("What was score 1? ");
    score1 = inputFile.nextInt();
    System.out.println("What was score 2? ");
    score2 = inputFile.nextInt();
    System.out.println("What was score 3? ");
    score3 = inputFile.nextInt();
    valid = validGroup(score1, score2, score3, outputFile);
    rtg = oneGameScore(score1);
    rtg2 = oneGameScore(score2);
    rtg3 = oneGameScore(score3);
    avg = avg3Scores(score1, score2, score3);
    avgrtg = avg3Scores(score1, score2, score3);
    grpavg = oneGameScore(avgrtg);
    outputFile.println("\n");
    outputFile.printf(formatMask,
        score1,
        score2,
        score3,
        valid,
        rtg,
        rtg2,
        rtg3,
        avg,
        grpavg);
}
outputFile.println("The program is complete!");
inputFile.close();
outputFile.close();
outputFile.flush();

希望这些帮助!

英文:

First, there is a small improvement to make in the while condition:

while(inputFile.hasNextInt()) {
System.out.println(&quot;What was score 1? &quot;);
score1 = inputFile.nextInt();
System.out.println(&quot;What was score 2? &quot;);
score2 = inputFile.nextInt();
System.out.println(&quot;What was score 3? &quot;);
score3 = inputFile.nextInt();
...

In this way you don't have to check at the end of each cycle for another integer or compare with -1.

For the format masks:

String headerFormatMask = &quot;%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s&quot;;
String formatMask       = &quot;%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s&quot;;
outputFile.printf(headerFormatMask,
&quot;Score 1&quot;,
&quot;Score 2&quot;,
&quot;Score 3&quot;,
&quot;Valid?&quot;,
&quot;Rating1&quot;,
&quot;Rating2&quot;,
&quot;Rating3&quot;,
&quot;Avg Score&quot;,
&quot;Avg Rating&quot;);
...
while (inputFile.hasNextInt()) {
...
outputFile.println(&quot;\n&quot;);
outputFile.printf(formatMask,
score1,
score2,
score3,
valid,
rtg,
rtg2,
rtg3,
avg,
grpavg);

Notice how the header format mask and the one for each row balance each other out.

In total, the code in the adapted main method is:

// todo change input / output files as necessary
PrintWriter outputFile= new PrintWriter(&quot;output.txt&quot;);
File myinputFile = new File(&quot;input.txt&quot;);
Scanner inputFile = new Scanner(myinputFile);
int score1, score2, score3,avg,avgrtg;
int t,f=0;
boolean valid;
String rtg,rtg2,rtg3,grpavg= &quot; &quot;;
outputFile.printf(&quot;%50s&quot;,&quot;TABLE OF BOWLING SCORES&quot;);
outputFile.printf(&quot;\n&quot;);
String headerFormatMask = &quot;%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s&quot;;
String formatMask       = &quot;%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s&quot;;
outputFile.printf(headerFormatMask,
&quot;Score 1&quot;,
&quot;Score 2&quot;,
&quot;Score 3&quot;,
&quot;Valid?&quot;,
&quot;Rating1&quot;,
&quot;Rating2&quot;,
&quot;Rating3&quot;,
&quot;Avg Score&quot;,
&quot;Avg Rating&quot;);
while(inputFile.hasNextInt()) {
System.out.println(&quot;What was score 1? &quot;);
score1 = inputFile.nextInt();
System.out.println(&quot;What was score 2? &quot;);
score2 = inputFile.nextInt();
System.out.println(&quot;What was score 3? &quot;);
score3 = inputFile.nextInt();
valid=validGroup(score1,score2,score3,outputFile);
rtg=oneGameScore(score1);
rtg2=oneGameScore(score2);
rtg3=oneGameScore(score3);
avg=avg3Scores(score1,score2,score3);
avgrtg=avg3Scores(score1,score2,score3);
grpavg=oneGameScore(avgrtg);
outputFile.println(&quot;\n&quot;);
outputFile.printf(formatMask,
score1,
score2,
score3,
valid,
rtg,
rtg2,
rtg3,
avg,
grpavg);
}
outputFile.println(&quot;\nThe program is complete!&quot;);
inputFile.close();
outputFile.close();
outputFile.flush();
}

huangapple
  • 本文由 发表于 2023年3月8日 15:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定