import java.util.Scanner;
public class FixedWidthPrinting {
public static void print(String[] theWords, int width) {
/////////////////////////////////////////////////////////
//-- start is the beginning position of the current line
//-- end is the end position of the current line (exclusive)
//-- lineCounter is the number of lines
/////////////////////////////////////////////////////////
int start = 0, end = 0, lineCounter = 0;
int[] gaps;
/////////////////////////////////////////////////////////
//-- the loop is executed until start becomes theWords.length
/////////////////////////////////////////////////////////
while (start < theWords.length) {
//////////////////////////////////////////////////////
// this is the print length with the current position only
//////////////////////////////////////////////////////
// FILL
//////////////////////////////////////////////////////
// the end position is set to the start position + 1
//////////////////////////////////////////////////////
// FILL
//////////////////////////////////////////////////////
// test whether the word at the end position can be added
// to the group, if so, add and move the position by 1
//////////////////////////////////////////////////////
// FILL
//////////////////////////////////////////////////////
// if there is only one word, just print it
//////////////////////////////////////////////////////
// FILL
//////////////////////////////////////////////////////
// other, if this is the last line, print flushed to the left
//////////////////////////////////////////////////////
// FILL
//////////////////////////////////////////////////////
// otherwise, compute the gaps and print
//////////////////////////////////////////////////////
// FILL
////////////////////////////////////////////////////
// update lineCounter and start
////////////////////////////////////////////////////
lineCounter ++;
start = end;
}
}
public static void main(String[] args) {
int width = Integer.parseInt(args[0]);
String[] text = {
"Well,", "I", "woke", "up", "to",
"get", "me", "a", "cold", "pop",
"and", "then", "I", "thought", "somebody",
"was", "barbecuing.", "I", "said,", "\"Oh,",
"Lord,", "Jesus,", "It's", "a", "fire!\"",
"Then", "I", "ran", "out.", "I",
"didn't", "grab", "no", "shoes", "or",
"nothin',", "Lord", "Jesus.", "I", "ran",
"for", "my", "life.", "And", "then",
"the", "smoke", "got", "me.", "I",
"got", "bronchitis.", "Ain't", "nobody", "got",
"time", "for", "that."
};
print(text, 15);
}
}
I have to write a code in Java that prints texts in a fixed character width per line. It's basically a big while loop where the number of words to place in the next line is computed, then the sizes of the gaps are computed,and the line is produced, and then the starting position is updated.
The main is already written, I just have to write the print method. It should consist of two parts, a String array to be printed and an int that specifies the character width, which can be changed.
I've included all my notes about what should happen where, but I just am not sure what exactly I should be writing for the code.
Aucun commentaire:
Enregistrer un commentaire