1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
5 5....
6 ....
7 ....
8 ....
9 ...
我已经写了一个不过那上下对不齐。。。
各位帮我看看是哪出错了。。。。
public class MultiTest{
public static void main(String[] args){
int i = 1,j = 1;
System.out.print(" MultiTest Table "+"\n");
System.out.print("---------------------"+"\n");
System.out.print(" ");
while(i <= 9)
{
System.out.print(i + " ");
//i++;
for(i = 1;i < 9;i++)
{
System.out.println(i);
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
}
i++;
}
//System.out.println();
/*for(i = 1;i <= 9;i++)
{
System.out.println(i);
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
}*/
}
}
7 个解决方案
#1
不太清楚楼主是要啥样子的乘法口诀最好是有个图,这种题目就两个循环就可以了
public class Multitest {
public static void main(String[] args){
System.out.print(" MultiTest Table "+"\n");
System.out.print("---------------------"+"\n");
int i=1,j=1;
while(i<10){
for(j=1;j<10;j++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.print("\n");
i++;
}
}
}
public class Multitest {
public static void main(String[] args){
System.out.print(" MultiTest Table "+"\n");
System.out.print("---------------------"+"\n");
int i=1,j=1;
while(i<10){
for(j=1;j<10;j++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.print("\n");
i++;
}
}
}
#2
九九乘法表还是很好控制格式的,因为输出的数只有一位整数和两位整数两种情况,作个简单的判断就行了:
public class MultiTest {
public static void main(String[] args) {
System.out.println(" MultiTest Table ");
System.out.println("---------------------");
showTable1();
}
static void showTable1() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
for (int j = 1; j < 10; j++)
System.out.print(i * j + (i * j > 9 ? " " : " "));
System.out.println();
}
}
}
#3
PS:对于格式化输出效果,用格式化输出函数System.out.printf(System.out.format)要比System.out.print容易得多:
public class MultiTest {
public static void main(String[] args) {
System.out.println(" MultiTest Table ");
System.out.println("---------------------");
// showTable1();
showTable2();
}
static void showTable1() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
for (int j = 1; j < 10; j++)
System.out.print(i * j + (i * j > 9 ? " " : " "));
System.out.println();
}
}
static void showTable2() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.printf("%-3d", i);
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.printf("%-2d", i);
for (int j = 1; j < 10; j++)
System.out.printf("%-3d", i * j);
System.out.println();
}
}
}
#4
public class TestMulti
{
public static void main(String agrs[])
{
for(int i=1;i<=9;i++)//控制i的变化,i自加一次
{
for(int j=1;j<=i;j++)//控制j变化,j变化i次
{
System.out.printf("%d*%d=%2d\t",i,j,i*j);
}
System.out.println();
}
}
}
{
public static void main(String agrs[])
{
for(int i=1;i<=9;i++)//控制i的变化,i自加一次
{
for(int j=1;j<=i;j++)//控制j变化,j变化i次
{
System.out.printf("%d*%d=%2d\t",i,j,i*j);
}
System.out.println();
}
}
}
#5
这个代码实现的惩罚口诀
#6
1、System.out.println(i);->改为System.out.println();
2、
==================================
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
==================================
改为
==================================
for(j = 1;j <= 9;j++){
String s = i * j + " ";
if (s.length() < 3) {
s = s + " ";
}
System.out.print(s);
}
==================================
2、
==================================
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
==================================
改为
==================================
for(j = 1;j <= 9;j++){
String s = i * j + " ";
if (s.length() < 3) {
s = s + " ";
}
System.out.print(s);
}
==================================
#7
public static void test(){
int i=1;
while(i<=9){
for(int j=1;j<=9;j++){
int value = i*j;
String strdis = value+"|";
if(strdis.length()<3){
strdis=" "+strdis;
}
System.out.print(strdis);
}
System.out.println();
System.out.print("————————————————————————————————");
System.out.println();
i++;
}
}
#1
不太清楚楼主是要啥样子的乘法口诀最好是有个图,这种题目就两个循环就可以了
public class Multitest {
public static void main(String[] args){
System.out.print(" MultiTest Table "+"\n");
System.out.print("---------------------"+"\n");
int i=1,j=1;
while(i<10){
for(j=1;j<10;j++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.print("\n");
i++;
}
}
}
public class Multitest {
public static void main(String[] args){
System.out.print(" MultiTest Table "+"\n");
System.out.print("---------------------"+"\n");
int i=1,j=1;
while(i<10){
for(j=1;j<10;j++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.print("\n");
i++;
}
}
}
#2
九九乘法表还是很好控制格式的,因为输出的数只有一位整数和两位整数两种情况,作个简单的判断就行了:
public class MultiTest {
public static void main(String[] args) {
System.out.println(" MultiTest Table ");
System.out.println("---------------------");
showTable1();
}
static void showTable1() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
for (int j = 1; j < 10; j++)
System.out.print(i * j + (i * j > 9 ? " " : " "));
System.out.println();
}
}
}
#3
PS:对于格式化输出效果,用格式化输出函数System.out.printf(System.out.format)要比System.out.print容易得多:
public class MultiTest {
public static void main(String[] args) {
System.out.println(" MultiTest Table ");
System.out.println("---------------------");
// showTable1();
showTable2();
}
static void showTable1() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
for (int j = 1; j < 10; j++)
System.out.print(i * j + (i * j > 9 ? " " : " "));
System.out.println();
}
}
static void showTable2() {
System.out.print(" ");
for (int i = 1; i < 10; i++)
System.out.printf("%-3d", i);
System.out.println();
for (int i = 1; i < 10; i++) {
System.out.printf("%-2d", i);
for (int j = 1; j < 10; j++)
System.out.printf("%-3d", i * j);
System.out.println();
}
}
}
#4
public class TestMulti
{
public static void main(String agrs[])
{
for(int i=1;i<=9;i++)//控制i的变化,i自加一次
{
for(int j=1;j<=i;j++)//控制j变化,j变化i次
{
System.out.printf("%d*%d=%2d\t",i,j,i*j);
}
System.out.println();
}
}
}
{
public static void main(String agrs[])
{
for(int i=1;i<=9;i++)//控制i的变化,i自加一次
{
for(int j=1;j<=i;j++)//控制j变化,j变化i次
{
System.out.printf("%d*%d=%2d\t",i,j,i*j);
}
System.out.println();
}
}
}
#5
这个代码实现的惩罚口诀
#6
1、System.out.println(i);->改为System.out.println();
2、
==================================
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
==================================
改为
==================================
for(j = 1;j <= 9;j++){
String s = i * j + " ";
if (s.length() < 3) {
s = s + " ";
}
System.out.print(s);
}
==================================
2、
==================================
for(j = 1;j <= 9;j++)
{
System.out.print(i * j + " ");
}
==================================
改为
==================================
for(j = 1;j <= 9;j++){
String s = i * j + " ";
if (s.length() < 3) {
s = s + " ";
}
System.out.print(s);
}
==================================
#7
public static void test(){
int i=1;
while(i<=9){
for(int j=1;j<=9;j++){
int value = i*j;
String strdis = value+"|";
if(strdis.length()<3){
strdis=" "+strdis;
}
System.out.print(strdis);
}
System.out.println();
System.out.print("————————————————————————————————");
System.out.println();
i++;
}
}