【请问这个怎么打印】

握草 2017-11-03 10:53:33
package ass002;

import java.util.Scanner;

public class Ass002 {
private static Scanner sca;

public static void main(String[] args) {
System.out.print("Please enter two integers (1~9999): ");
sca = new Scanner(System.in);

int a = sca.nextInt();
int b = sca.nextInt();

String str1 = String.valueOf(a);
String str2 = String.valueOf(b);

char[] aArr = str1.toCharArray();
char[] bArr = str2.toCharArray();

int[][][] results = new int[str2.length()][str1.length()][2];
int[] x = new int[str1.length()];
int[] y = new int[str2.length()];

for (int i = 0; i < str2.length(); ++i) {
for (int j = 0; j < str1.length(); ++j) {
int tmp1 = Integer.valueOf(aArr[j]) - 48;
int tmp2 = Integer.valueOf(bArr[i]) - 48;
int tmp = tmp1 * tmp2;
if (tmp < 10) {
results[i][j][0] = 0;
results[i][j][1] = tmp;
} else {
results[i][j][0] = tmp / 10;
results[i][j][1] = tmp % 10;
}

}
}



System.out.print("+");
for (int k = 1; k <= str1.length(); ++k) {
System.out.print("-----");
}
System.out.print("+");

System.out.println();
System.out.print("|");
System.out.print(" ");
for (int i = 0; i < str1.length(); ++i) {
System.out.print(str1.charAt(i));
System.out.print(" ");
}
System.out.print("|");
for (int i = 0; i < str2.length(); ++i) {
// #001
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("+");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("---+");
}
System.out.print(" ");
System.out.print("|");

// #002
System.out.println();
System.out.print("|");
if(i==0) {
System.out.print(" ");
}
else {
System.out.print("/");
}
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print(results[i][j][0]);
System.out.print(" ");
System.out.print("/");
System.out.print("|");
}
System.out.print(" ");
System.out.print("|");
// #003
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print(" ");
System.out.print("/");
System.out.print(" ");
System.out.print("|");
}
System.out.print(bArr[0]);
System.out.print("|");
// #004
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("/");
System.out.print(" ");
System.out.print(results[i][j][1]);
System.out.print("|");
}
System.out.print(" ");
System.out.print("|");
}
// #009
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("+");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("---+");
}
System.out.print(" ");
System.out.print("|");

System.out.println();
System.out.print("|");
for(int j=0; j<str1.length();++j) {
System.out.print("/");
System.out.print(" ");
System.out.print("3");
System.out.print(" ");
}
System.out.print(" ");
System.out.print("|");
System.out.println();
System.out.print("+");
for (int k = 1; k <= str1.length(); ++k) {
System.out.print("-----");
}
System.out.print("+");
}
}
[/code]

这是我的运行结果,还没有完成。。

以上的代码就是打印出来一个表格的形式,难点有二:
1.斜着的/杠杠的延伸处的数字是同斜行的和(3是我加上去的别管他!),如第一个数就是1,第二个数是1+5+2=8;以此类推。问题是这个斜着的和是怎么打印出来的,或者说是怎么和原来的三维数组的下标能扯上关系的??
2.这个
for (int k = 1; k <= str1.length(); ++k) {
System.out.print("-----");
}

不用说也能看出来是用来防止a(第一个输入的数,横着的那个)的位数过大二造成的“+----+”不够长的尴尬。。但是对于横行可以,对于纵行我就不知道该怎么办了,我是把总行控制行数的写在for循环外面,就感觉这个会有影响,如果纵行>2的话表格的格式就打乱了(横行没问题关键是纵行不知道怎么才能使“ | ”单位像“+----+”单位一样的随行数的变化而变化。。)

一共两个问题,有谁能至少回答出一个问题的话,本人感激不尽!
...全文
105 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
握草 2017-11-03
好吧,这个才是缩进后的版本。。
  • 打赏
  • 举报
回复
握草 2017-11-03
//As follow, I'll try to use the most stupid way, which means printing the form from the top to the bottom.
package ass002;

import java.util.Scanner;

public class Ass002 {
	private static Scanner sca;

	public static void main(String[] args) {
		System.out.print("Please enter two integers (1~9999): ");
		sca = new Scanner(System.in);

		int a = sca.nextInt();
		int b = sca.nextInt();

		String str1 = String.valueOf(a);
		String str2 = String.valueOf(b);

		char[] aArr = str1.toCharArray();
		char[] bArr = str2.toCharArray();

		int[][][] results = new int[str2.length()][str1.length()][2];
		int[] x = new int[str1.length()];
		int[] y = new int[str2.length()];

		for (int i = 0; i < str2.length(); ++i) {
			for (int j = 0; j < str1.length(); ++j) {
				int tmp1 = Integer.valueOf(aArr[j]) - 48;
				int tmp2 = Integer.valueOf(bArr[i]) - 48;
				int tmp = tmp1 * tmp2;
				if (tmp < 10) {
					results[i][j][0] = 0;
					results[i][j][1] = tmp;
				} else {
					results[i][j][0] = tmp / 10;
					results[i][j][1] = tmp % 10;
				}

			}
		}

		System.out.print("+");
		for (int k = 1; k <= str1.length(); ++k) {
			System.out.print("-----");
		}
		System.out.print("+");

		System.out.println();
		System.out.print("|");
		System.out.print("   ");
		for (int i = 0; i < str1.length(); ++i) {
			System.out.print(str1.charAt(i));
			System.out.print("   ");
		}
		System.out.print("|");
		for (int i = 0; i < str2.length(); ++i) {
			// #001
			System.out.println();
			System.out.print("|");
			System.out.print(" ");
			System.out.print("+");
			for (int j = 0; j < str1.length(); ++j) {
				System.out.print("---+");
			}
			System.out.print(" ");
			System.out.print("|");

			// #002
			System.out.println();
			System.out.print("|");
			if (i == 0) {
				System.out.print(" ");
			} else {
				System.out.print("/");
			}
			System.out.print("|");
			for (int j = 0; j < str1.length(); ++j) {
				System.out.print(results[i][j][0]);
				System.out.print(" ");
				System.out.print("/");
				System.out.print("|");
			}
			System.out.print(" ");
			System.out.print("|");
			// #003
			System.out.println();
			System.out.print("|");
			System.out.print(" ");
			System.out.print("|");
			for (int j = 0; j < str1.length(); ++j) {
				System.out.print(" ");
				System.out.print("/");
				System.out.print(" ");
				System.out.print("|");
			}
			System.out.print(bArr[0]);
			System.out.print("|");
			// #004
			System.out.println();
			System.out.print("|");
			System.out.print(" ");
			System.out.print("|");
			for (int j = 0; j < str1.length(); ++j) {
				System.out.print("/");
				System.out.print(" ");
				System.out.print(results[i][j][1]);
				System.out.print("|");
			}
			System.out.print(" ");
			System.out.print("|");
		}
		// #009
		System.out.println();
		System.out.print("|");
		System.out.print(" ");
		System.out.print("+");
		for (int j = 0; j < str1.length(); ++j) {
			System.out.print("---+");
		}
		System.out.print(" ");
		System.out.print("|");

		System.out.println();
		System.out.print("|");
		for (int j = 0; j < str1.length(); ++j) {
			System.out.print("/");
			System.out.print(" ");
			System.out.print("3");
			System.out.print(" ");
		}
		System.out.print("   ");
		System.out.print("|");
		System.out.println();
		System.out.print("+");
		for (int k = 1; k <= str1.length(); ++k) {
			System.out.print("-----");
		}
		System.out.print("+");
	}
}
  • 打赏
  • 举报
回复
握草 2017-11-03
package ass002;

import java.util.Scanner;

public class Ass002 {
private static Scanner sca;

public static void main(String[] args) {
System.out.print("Please enter two integers (1~9999): ");
sca = new Scanner(System.in);

int a = sca.nextInt();
int b = sca.nextInt();

String str1 = String.valueOf(a);
String str2 = String.valueOf(b);

char[] aArr = str1.toCharArray();
char[] bArr = str2.toCharArray();

int[][][] results = new int[str2.length()][str1.length()][2];
int[] x = new int[str1.length()];
int[] y = new int[str2.length()];

for (int i = 0; i < str2.length(); ++i) {
for (int j = 0; j < str1.length(); ++j) {
int tmp1 = Integer.valueOf(aArr[j]) - 48;
int tmp2 = Integer.valueOf(bArr[i]) - 48;
int tmp = tmp1 * tmp2;
if (tmp < 10) {
results[i][j][0] = 0;
results[i][j][1] = tmp;
} else {
results[i][j][0] = tmp / 10;
results[i][j][1] = tmp % 10;
}

}
}



System.out.print("+");
for (int k = 1; k <= str1.length(); ++k) {
System.out.print("-----");
}
System.out.print("+");

System.out.println();
System.out.print("|");
System.out.print("   ");
for (int i = 0; i < str1.length(); ++i) {
System.out.print(str1.charAt(i));
System.out.print("   ");
}
System.out.print("|");
for (int i = 0; i < str2.length(); ++i) {
// #001
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("+");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("---+");
}
System.out.print(" ");
System.out.print("|");

// #002
System.out.println();
System.out.print("|");
if(i==0) {
System.out.print(" ");
}
else {
System.out.print("/");
}
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print(results[i][j][0]);
System.out.print(" ");
System.out.print("/");
System.out.print("|");
}
System.out.print(" ");
System.out.print("|");
// #003
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print(" ");
System.out.print("/");
System.out.print(" ");
System.out.print("|");
}
System.out.print(bArr[0]);
System.out.print("|");
// #004
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("|");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("/");
System.out.print(" ");
System.out.print(results[i][j][1]);
System.out.print("|");
}
System.out.print(" ");
System.out.print("|");
}
// #009
System.out.println();
System.out.print("|");
System.out.print(" ");
System.out.print("+");
for (int j = 0; j < str1.length(); ++j) {
System.out.print("---+");
}
System.out.print(" ");
System.out.print("|");

System.out.println();
System.out.print("|");
for(int j=0; j<str1.length();++j) {
System.out.print("/");
System.out.print(" ");
System.out.print("3");
System.out.print(" ");
}
System.out.print("   ");
System.out.print("|");
System.out.println();
System.out.print("+");
for (int k = 1; k <= str1.length(); ++k) {
System.out.print("-----");
}
System.out.print("+");
}
}
不好意思,代码没有缩进——我重新贴出来,大家就看这个新的代码好了~
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2017-11-03 10:53
社区公告
暂无公告