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

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

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

问题

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

  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class Topic_HW5 {
  4. public static String oneGameScore(int x){
  5. String scr = " ";
  6. if (0 <= x && x < 50)
  7. scr = "Horrible";
  8. if (50 <= x && x <= 99)
  9. scr = "Poor";
  10. if (100 <= x && x <= 139)
  11. scr = "Good";
  12. if (140 <= x && x <= 199)
  13. scr = "Very Good";
  14. if (200 <= x && x <= 249)
  15. scr = "Excellent";
  16. if (250 <= x && x <= 300)
  17. scr = "Pro";
  18. if (x < 0)
  19. scr = "Illegal <0";
  20. if (300 < x)
  21. scr = "Illegal <300";
  22. return scr;
  23. }
  24. public static int avg3Scores(int x, int y, int z) {
  25. int avg = (x + y + z) / 3;
  26. return avg;
  27. }
  28. public static boolean validGroup(int x, int y, int z, PrintWriter outputFile) {
  29. boolean validGroup = false;
  30. if (0 <= x && x <= 300)
  31. if (0 <= y && y <= 300)
  32. if (0 <= z && z <= 300)
  33. validGroup = true;
  34. else
  35. validGroup = false;
  36. return validGroup;
  37. }
  38. public static void main(String[] args) throws IOException {
  39. // TODO Auto-generated method stub
  40. // 创建输出文件
  41. PrintWriter outputFile = new PrintWriter("hw5out.txt");
  42. File myinputFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\hw5input.txt");
  43. Scanner inputFile = new Scanner(myinputFile);
  44. int score1, score2, score3, avg, avgrtg;
  45. int t, f = 0;
  46. boolean valid;
  47. String rtg, rtg2, rtg3, grpavg = " ";
  48. outputFile.printf("%50s", "TABLE OF BOWLING SCORES");
  49. // outputFile.printf("\nScore 1\tScore 2\tScore 3\t Valid?\tRating1\tRating2\tRating3");
  50. outputFile.printf("\n%1s %7s %7s %7s %8s %8s %8s %12s %14s", "\nScore 1",
  51. "Score 2", "Score 3", "Valid?", " Rating1", " Rating2", "Rating3", "Avg Score", "Avg Rating");
  52. // "\n%1s %7s %7s %7s %8s %8s %8s", "\nScore 1", "Score 2", "Score 3", "Valid?", "Rating1", "Rating2", "Rating3"
  53. System.out.println("What was score 1? ");
  54. score1 = inputFile.nextInt();
  55. while (score1 != -1) {
  56. System.out.println("What was score 2? ");
  57. score2 = inputFile.nextInt();
  58. System.out.println("What was score 3? ");
  59. score3 = inputFile.nextInt();
  60. valid = validGroup(score1, score2, score3, outputFile);
  61. rtg = oneGameScore(score1);
  62. rtg2 = oneGameScore(score2);
  63. rtg3 = oneGameScore(score3);
  64. avg = avg3Scores(score1, score2, score3);
  65. avgrtg = avg3Scores(score1, score2, score3);
  66. grpavg = oneGameScore(avgrtg);
  67. outputFile.println("\n");
  68. outputFile.printf("%1d %7d %7d %9b %-12s %-8s %-8s %12d %16s",
  69. score1, score2, score3, valid, rtg, rtg2, rtg3, avg, grpavg);
  70. System.out.println("What was score 1? ");
  71. score1 = inputFile.nextInt();
  72. }
  73. outputFile.println("\nThe program is complete!");
  74. inputFile.close();
  75. outputFile.close();
  76. outputFile.flush();
  77. }
  78. }

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

为了解决此问题,您可以使用格式化字符串中的字段宽度来确保列对齐。在 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

  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class Topic_HW5 {
  4. public static String oneGameScore(int x){
  5. String scr = &quot; &quot;;
  6. if (0&lt;=x&amp;&amp;x&lt;50)
  7. scr = &quot;Horrible&quot;;
  8. if (50&lt;=x&amp;&amp;x&lt;=99)
  9. scr = &quot;Poor&quot;;
  10. if (100&lt;=x&amp;&amp;x&lt;=139)
  11. scr = &quot;Good&quot;;
  12. if (140&lt;=x&amp;&amp;x&lt;=199)
  13. scr = &quot;Very Good&quot;;
  14. if (200&lt;=x&amp;&amp;x&lt;=249)
  15. scr = &quot;Excellent&quot;;
  16. if (250&lt;=x&amp;&amp;x&lt;=300)
  17. scr = &quot;Pro&quot;;
  18. if(x&lt;0)
  19. scr= &quot;Illegal &lt;0&quot;;
  20. if(300&lt;x)
  21. scr=&quot;Illegal &lt;300&quot;;
  22. return scr;
  23. }
  24. public static int avg3Scores(int x, int y, int z) {
  25. int avg=(x+y+z)/3;
  26. return avg;
  27. }
  28. public static boolean validGroup(int x,int y, int z, PrintWriter outputFile) {
  29. boolean validGroup=false;
  30. if(0&lt;=x&amp;&amp;x&lt;=300)
  31. if(0&lt;=y&amp;&amp;y&lt;=300)
  32. if (0&lt;=z&amp;&amp;z&lt;=300)
  33. validGroup=true;
  34. else
  35. validGroup=false;
  36. return validGroup;
  37. }
  38. public static void main(String[] args) throws IOException {
  39. // TODO Auto-generated method stub
  40. //create outputFile
  41. PrintWriter outputFile= new PrintWriter(&quot;hw5out.txt&quot;);
  42. File myinputFile = new File(&quot;C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\hw5input.txt&quot;);
  43. Scanner inputFile = new Scanner(myinputFile);
  44. int score1, score2, score3,avg,avgrtg;
  45. int t,f=0;
  46. boolean valid;
  47. String rtg,rtg2,rtg3,grpavg= &quot; &quot;;
  48. outputFile.printf(&quot;%50s&quot;,&quot;TABLE OF BOWLING SCORES&quot;);
  49. //outputFile.printf(&quot;\nScore 1\tScore 2\tScore 3\t Valid?\tRating1\tRating2\tRating3&quot;);
  50. outputFile.printf(&quot;\n%1s %7s %7s %7s %8s %8s %8s %12s %14s&quot;, &quot;\nScore 1&quot;,
  51. &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;);
  52. //&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;
  53. System.out.println(&quot;What was score 1? &quot;);
  54. score1 = inputFile.nextInt();
  55. while(score1 != -1) {
  56. System.out.println(&quot;What was score 2? &quot;);
  57. score2 = inputFile.nextInt();
  58. System.out.println(&quot;What was score 3? &quot;);
  59. score3 = inputFile.nextInt();
  60. valid=validGroup(score1,score2,score3,outputFile);
  61. rtg=oneGameScore(score1);
  62. rtg2=oneGameScore(score2);
  63. rtg3=oneGameScore(score3);
  64. avg=avg3Scores(score1,score2,score3);
  65. avgrtg=avg3Scores(score1,score2,score3);
  66. grpavg=oneGameScore(avgrtg);
  67. outputFile.println(&quot;\n&quot;);
  68. outputFile.printf(&quot;%1d %7d %7d %9b %-12s %-8s %-8s %12d %16s&quot;,
  69. score1,score2,score3,valid,rtg,rtg2,rtg3,avg, grpavg);
  70. System.out.println(&quot;What was score 1? &quot;);
  71. score1 = inputFile.nextInt();
  72. }
  73. outputFile.println(&quot;\nThe program is complete!&quot;);
  74. inputFile.close();
  75. outputFile.close();
  76. outputFile.flush();
  77. }
  78. }

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条件中有一个小改进:

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

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

对于格式掩码:

  1. String headerFormatMask = "%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s";
  2. String formatMask = "%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s";
  3. outputFile.printf(headerFormatMask,
  4. "Score 1",
  5. "Score 2",
  6. "Score 3",
  7. "Valid?",
  8. "Rating1",
  9. "Rating2",
  10. "Rating3",
  11. "Avg Score",
  12. "Avg Rating");
  13. ...
  14. while (inputFile.hasNextInt()) {
  15. ...
  16. outputFile.println("\n");
  17. outputFile.printf(formatMask,
  18. score1,
  19. score2,
  20. score3,
  21. valid,
  22. rtg,
  23. rtg2,
  24. rtg3,
  25. avg,
  26. grpavg);
  27. }

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

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

  1. // todo change input / output files as necessary
  2. PrintWriter outputFile = new PrintWriter("output.txt");
  3. File myinputFile = new File("input.txt");
  4. Scanner inputFile = new Scanner(myinputFile);
  5. int score1, score2, score3, avg, avgrtg;
  6. int t, f = 0;
  7. boolean valid;
  8. String rtg, rtg2, rtg3, grpavg = " ";
  9. outputFile.printf("%50s", "TABLE OF BOWLING SCORES");
  10. outputFile.printf("\n");
  11. String headerFormatMask = "%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s";
  12. String formatMask = "%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s";
  13. outputFile.printf(headerFormatMask,
  14. "Score 1",
  15. "Score 2",
  16. "Score 3",
  17. "Valid?",
  18. "Rating1",
  19. "Rating2",
  20. "Rating3",
  21. "Avg Score",
  22. "Avg Rating");
  23. while(inputFile.hasNextInt()) {
  24. System.out.println("What was score 1? ");
  25. score1 = inputFile.nextInt();
  26. System.out.println("What was score 2? ");
  27. score2 = inputFile.nextInt();
  28. System.out.println("What was score 3? ");
  29. score3 = inputFile.nextInt();
  30. valid = validGroup(score1, score2, score3, outputFile);
  31. rtg = oneGameScore(score1);
  32. rtg2 = oneGameScore(score2);
  33. rtg3 = oneGameScore(score3);
  34. avg = avg3Scores(score1, score2, score3);
  35. avgrtg = avg3Scores(score1, score2, score3);
  36. grpavg = oneGameScore(avgrtg);
  37. outputFile.println("\n");
  38. outputFile.printf(formatMask,
  39. score1,
  40. score2,
  41. score3,
  42. valid,
  43. rtg,
  44. rtg2,
  45. rtg3,
  46. avg,
  47. grpavg);
  48. }
  49. outputFile.println("The program is complete!");
  50. inputFile.close();
  51. outputFile.close();
  52. outputFile.flush();

希望这些帮助!

英文:

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

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

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:

  1. String headerFormatMask = &quot;%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s&quot;;
  2. String formatMask = &quot;%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s&quot;;
  3. outputFile.printf(headerFormatMask,
  4. &quot;Score 1&quot;,
  5. &quot;Score 2&quot;,
  6. &quot;Score 3&quot;,
  7. &quot;Valid?&quot;,
  8. &quot;Rating1&quot;,
  9. &quot;Rating2&quot;,
  10. &quot;Rating3&quot;,
  11. &quot;Avg Score&quot;,
  12. &quot;Avg Rating&quot;);
  13. ...
  14. while (inputFile.hasNextInt()) {
  15. ...
  16. outputFile.println(&quot;\n&quot;);
  17. outputFile.printf(formatMask,
  18. score1,
  19. score2,
  20. score3,
  21. valid,
  22. rtg,
  23. rtg2,
  24. rtg3,
  25. avg,
  26. 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:

  1. // todo change input / output files as necessary
  2. PrintWriter outputFile= new PrintWriter(&quot;output.txt&quot;);
  3. File myinputFile = new File(&quot;input.txt&quot;);
  4. Scanner inputFile = new Scanner(myinputFile);
  5. int score1, score2, score3,avg,avgrtg;
  6. int t,f=0;
  7. boolean valid;
  8. String rtg,rtg2,rtg3,grpavg= &quot; &quot;;
  9. outputFile.printf(&quot;%50s&quot;,&quot;TABLE OF BOWLING SCORES&quot;);
  10. outputFile.printf(&quot;\n&quot;);
  11. String headerFormatMask = &quot;%1s %7s %7s %-6s %-12s %-12s %-12s %1s %10s&quot;;
  12. String formatMask = &quot;%1s %7s %7s %10s %-12s %-12s %-12s %1s %16s&quot;;
  13. outputFile.printf(headerFormatMask,
  14. &quot;Score 1&quot;,
  15. &quot;Score 2&quot;,
  16. &quot;Score 3&quot;,
  17. &quot;Valid?&quot;,
  18. &quot;Rating1&quot;,
  19. &quot;Rating2&quot;,
  20. &quot;Rating3&quot;,
  21. &quot;Avg Score&quot;,
  22. &quot;Avg Rating&quot;);
  23. while(inputFile.hasNextInt()) {
  24. System.out.println(&quot;What was score 1? &quot;);
  25. score1 = inputFile.nextInt();
  26. System.out.println(&quot;What was score 2? &quot;);
  27. score2 = inputFile.nextInt();
  28. System.out.println(&quot;What was score 3? &quot;);
  29. score3 = inputFile.nextInt();
  30. valid=validGroup(score1,score2,score3,outputFile);
  31. rtg=oneGameScore(score1);
  32. rtg2=oneGameScore(score2);
  33. rtg3=oneGameScore(score3);
  34. avg=avg3Scores(score1,score2,score3);
  35. avgrtg=avg3Scores(score1,score2,score3);
  36. grpavg=oneGameScore(avgrtg);
  37. outputFile.println(&quot;\n&quot;);
  38. outputFile.printf(formatMask,
  39. score1,
  40. score2,
  41. score3,
  42. valid,
  43. rtg,
  44. rtg2,
  45. rtg3,
  46. avg,
  47. grpavg);
  48. }
  49. outputFile.println(&quot;\nThe program is complete!&quot;);
  50. inputFile.close();
  51. outputFile.close();
  52. outputFile.flush();
  53. }

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:

确定