英文:
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 = " ";
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
//create outputFile
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();
}
}
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("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();
...
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 = "%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);
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("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("\nThe program is complete!");
inputFile.close();
outputFile.close();
outputFile.flush();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论