Files
wahyd4.github.com/source/_posts/java-9-9.md
T
Hermes Bot 232ecd7c22 Rebuild blog source from GitHub Pages artifact
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25)
- Hexo project with NexT theme, reading-experience custom styles
- publish-post.sh: write post -> hexo build -> PR (master untouched)
2026-08-04 09:44:07 +10:00

56 lines
1.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Java双重循环的典型应用(九九乘法表)"
date: 2010-11-10 00:00:00
tags: [java]
---
刚才正好在看JAVA的双重循环这一块,然后突然就想到了很经典的九九乘法表。因为我们之前C语言里面也写过这样的程序。所以我就试着写这个程序。确实在试的过程中还是发现很多问题和值得注意的地方。下面慢慢说来。
用JAVA写的九九乘法表。
```java
{
public static void main(String[] args)
{
int a,b,s;
for (a=1;a<=9 ;a++ )
{
for(b=1;b<=a;b++)
{
s=a*b;
System.out.print(a+"*"+b+"="+s);
System.out.print("  ");
}
System.out.println();
}
}
}
```
用C语言写的九九乘法表
```cpp
#include "stdio.h"    //九九乘法表。
void main()
{
int i,j,s;
for (i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{  s=i*j;
printf ("%d*%d=%-4d",i,j,s);
}
printf ("n") ;
}
}
```
这个是没用使用空格
这个是使用了空格,但是没有左对齐。
**其实通过比较我们就可以很容易看到,这个 程序JAVA和C的代码都是差不多的。只是细节上有一些变化。有个不同就是我不不知道JAVA里面有没有类似** **printf (“%d\*%d=%-4d”,i,j,s);这样的语句。即实现左对齐。**于是我在JAVA里面想到了这样一个办法。再输出一行,让它打印空格就好。
**System.out.print(a+”\*”+b+”=”+s);
System.out.print(“  ”);**
**当然我也试过这样一种方法。即System.out.print(a+”\*”+b+”=”+s “     ”)但是通过编译发现这样是行不通的。**
**还有一个就是我晓得了那个String ()agrs .它其实就是一个数组其实相当于是程序比不可少的东西,不管怎样你都要写出来,但是你还是可以选择不在程序里面使用它。**