我创建了一个三菱徽标构建器,如下所示。它从logo.txt
文件中获取输入,并使用ints/chars
创建logo.txt
文件中标识的许多徽标。我只是觉得我的代码是非常重复的,我想把它清理成单独的方法,而不是一遍又一遍地重复,是否有任何关于如何清理它的建议?我将不胜感激任何反馈。甚至只是一个起点。提前谢谢。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
input = new Scanner(new FileInputStream("config.txt"));
//initialize ithe variable's input and output
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
}
int size = Integer.parseInt(inputStream.nextLine());
int numLogos = Integer.parseInt(inputStream.nextLine());
char letter1 = inputStream.next().charAt(0);
char letter2 = inputStream.next().charAt(0);
for(int bee = numLogos; bee > 0; bee--){
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
System.out.println();
}
for(int y = 0; y < size; y++) {
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < y; x++) {
System.out.print(letter1);
}
for(int x = size; x > y; x--) {
System.out.print(letter2);
}
System.out.println();
}
}
}
}
答案 0 :(得分:0)
您在代码中使用了很多次:
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < y; x++) {
System.out.print(letter2);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter2);
}
可以用
代替 for(int x = 0; x < 2*size; x++) {
System.out.print(letter2);
}
另外,这个:
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
for(int x = 0; x < size - y; x++) {
System.out.print(letter1);
}
可以替换为
for(int x = 0; x < 2*(size - y); x++) {
System.out.print(letter1);
}