目录
- 前言
- 具体代码
- 完整代码
前言
背乘法口诀表我没记错话,应该是我们在上小学二年级的时候,相信大家对乘法表相当熟悉,那你知道如何用java打印这个漂亮的表吗?下面咱们一起来学习学习。
具体代码
- 数字乘法表
关键代码:
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
if (j == 2 && (i == 3 || i == 4)) {
System.out.print(j + "×" + i + "=" + (i * j) + " ");
continue;
}
System.out.print(j + "×" + i + "=" + (i * j) + " ");
}
System.out.println();
}
分析:
使用两层循环
第一层循环代表行号,取值范围 1-9
第二层循环代表列号,取值范围1-当前行号
为了打印整齐使用 if(j2 && (i3 || i==4)){
在行号为 3和4列号为2是多打印一个空格,其它情况正常打印。
打印效果:
优化代码
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
//格式化字符串,代码更简洁
String format = String.format(j+"×"+i+"=%2d"+(j==i?"%n":" "), i*j);
System.out.print(format);
}
}
打印效果一样
2.汉字乘法表
数字转汉字方法
/**
* 数字(小于100)转汉字
* @param number
* @return
*/
public static String numberToChinese2(int number) {
String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九","十"};
StringBuilder sb = new StringBuilder();
while (number > 0) {
int digit = number % 10;
//去掉零
if(digit != 0){
sb.append(chineseNumbers[digit]);
}
number /= 10;
//拼接十
if(number > 0) {
sb.append(chineseNumbers[10]);
}
//十位是1且个位不是0是去掉1
if(digit !=0 && number==1){
break;
}
}
return sb.reverse().toString();
}
打印汉字乘法表方法
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
//格式化字符串,代码更简洁
String jchanese = numberToChinese2(j);
String iChanese = numberToChinese2(i);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-6s"+(j==i?"%n%n":""),jchanese+iChanese+"得"+jiChanese);
}else {
format = String.format("%-6s"+(j==i?"%n%n":""), jchanese+iChanese+jiChanese);
}
System.out.print(format);
}
}
打印效果
- 倒着打印乘法表
关键代码
for(int i=1;i<=9;i++){
for(int n=1;n<=i-1;n++){
System.out.print(String.format("%-10s"," "));
}
for(int j=i;j<=9;j++){
//格式化字符串,代码更简洁
String iChanese = numberToChinese2(i);
String jchanese = numberToChinese2(j);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-8s"+(j==9?"%n%n":""),iChanese+jchanese+"得"+jiChanese);
}else {
format = String.format("%-8s"+(j==9?"%n%n":""), iChanese+jchanese+jiChanese);
}
System.out.print(format);
}
}
打印效果:
倒着的另一种效果
for(int i=1;i<=9;i++){
for(int j = i;j<=9;j++){
String iChanese = numberToChinese2(i);
String jchanese = numberToChinese2(j);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-8s"+(j==9?"%n%n":""),iChanese+jchanese+"得"+jiChanese);
}else {
format = String.format("%-8s"+(j==9?"%n%n":""), iChanese+jchanese+jiChanese);
}
System.out.print(format);
}
}
完整代码
下面是打印乘法口诀表的完整代码
public class MultTable {
public static void main(String[] args) {
//typeMultiplicationTable();
//typeMultiplicationTable2();
//typeMultiplicationTable3();
//typeMultiplicationTable4();
//typeMultiplicationTable5();
typeMultiplicationTable6();
}
/**
* 打印数字的乘法口诀表
*/
static void typeMultiplicationTable() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
if (j == 2 && (i == 3 || i == 4)) {
System.out.print(j + "×" + i + "=" + (i * j) + " ");
continue;
}
System.out.print(j + "×" + i + "=" + (i * j) + " ");
}
System.out.println();
}
}
/**
* 优化打印数字的乘法口诀表
*/
static void typeMultiplicationTable2() {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
//格式化字符串,代码更简洁
String format = String.format(j+"×"+i+"=%2d"+(j==i?"%n":" "), i*j);
System.out.print(format);
}
}
}
/**
* 汉字乘法口诀表
*/
static void typeMultiplicationTable3() {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
//格式化字符串,代码更简洁
String jchanese = numberToChinese2(j);
String iChanese = numberToChinese2(i);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-6s"+(j==i?"%n%n":""),jchanese+iChanese+"得"+jiChanese);
}else {
format = String.format("%-6s"+(j==i?"%n%n":""), jchanese+iChanese+jiChanese);
}
System.out.print(format);
}
}
}
/**
* 倒着的汉字乘法口诀表
*/
static void typeMultiplicationTable4() {
for(int i=1;i<=9;i++){
for(int n=1;n<=i-1;n++){
System.out.print(String.format("%-10s"," "));
}
for(int j=i;j<=9;j++){
//格式化字符串,代码更简洁
String iChanese = numberToChinese2(i);
String jchanese = numberToChinese2(j);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-8s"+(j==9?"%n%n":""),iChanese+jchanese+"得"+jiChanese);
}else {
format = String.format("%-8s"+(j==9?"%n%n":""), iChanese+jchanese+jiChanese);
}
System.out.print(format);
}
}
}
/**
* 倒着的汉字乘法口诀表2
*/
static void typeMultiplicationTable5() {
for(int i=1;i<=9;i++){
for(int j =1;j<=9;j++){
String iChanese = numberToChinese2(i);
String jchanese = numberToChinese2(j);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-8s"+(j==9?"%n%n":""),iChanese+jchanese+"得"+jiChanese);
}else {
format = String.format("%-8s"+(j==9?"%n%n":""), iChanese+jchanese+jiChanese);
}
if(i<=j){
System.out.print(format);
}
}
}
}
/**
* 倒着的汉字乘法口诀表2
*/
static void typeMultiplicationTable6() {
for(int i=1;i<=9;i++){
for(int j = i;j<=9;j++){
String iChanese = numberToChinese2(i);
String jchanese = numberToChinese2(j);
int ji = i*j;
String jiChanese = numberToChinese2(i*j);
String format = null;
if(ji <= 9){
format = String.format("%-8s"+(j==9?"%n%n":""),iChanese+jchanese+"得"+jiChanese);
}else {
format = String.format("%-8s"+(j==9?"%n%n":""), iChanese+jchanese+jiChanese);
}
System.out.print(format);
}
}
}
/**
* 数字转汉字
* @param number
* @return
*/
public static String numberToChinese(int number) {
String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
StringBuilder sb = new StringBuilder();
while (number > 0) {
int digit = number % 10;
sb.append(chineseNumbers[digit]);
number /= 10;
}
return sb.reverse().toString();
}
/**
* 数字(小于100)转汉字
* @param number
* @return
*/
public static String numberToChinese2(int number) {
String[] chineseNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九","十"};
StringBuilder sb = new StringBuilder();
while (number > 0) {
int digit = number % 10;
//去掉零
if(digit != 0){
sb.append(chineseNumbers[digit]);
}
number /= 10;
//拼接十
if(number > 0) {
sb.append(chineseNumbers[10]);
}
//十位是1且个位不是0是去掉1
if(digit !=0 && number==1){
break;
}
}
return sb.reverse().toString();
}
}