1.九九乘法表
public class study1 {
public static void main(String[] args) {
for(int i = 1;i <=9;i++){
for(int j =1;j <= i;j++){
System.out.print(j + "*" + i + "=" + (j*i) + "\t");
}
System.out.println( );
}
}
}
注意:加上制表位 \t 会使结果更加整洁哦!
2. 打印实心、空心菱形
实心:
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想打印实心菱形数,方法为,输入一个数a,得到的是2 * a - 1个层数的实心菱形:");
int totalfloor = sc.nextInt();
for (int i = 1;i <= totalfloor;i++){
for(int j = 1;j <= totalfloor - i;j++){
System.out.print(" ");
}
for(int k = 1;k <= 2*i-1;k++){
System.out.print("*");
}
System.out.println(" ");
}
for(int a = totalfloor - 1;a >= 1;a--){
for(int b = 1;b <= totalfloor - a;b++){
System.out.print(" ");
}
for(int c = 1;c <= 2 * a - 1;c++){
System.out.print("*");
}
System.out.println(" ");
}
空心:
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想打印空心菱形数,方法为,输入一个数a,得到的是2 * a - 1个层数的空心菱形:");
int totalfloor = sc.nextInt();
for (int i = 1;i <= totalfloor;i++){
for(int j = 1;j <= totalfloor - i;j++){
System.out.print(" ");
}
for(int k = 1;k <= 2 * i - 1;k++){
if(k == 1 || k == 2 * i - 1 ){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println(" ");
}
for(int a = totalfloor - 1;a >= 1;a--){
for(int b = 1;b <= totalfloor - a;b++){
System.out.print(" ");
}
for(int c = 1;c <= 2 * a - 1;c++){
if(c == 1 || c == 2 * a - 1){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
3.水仙花数
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个三位数,即可判断此数是不是水仙花树:");
int number = sc.nextInt();
int a = number / 100;//得到百位上的数
int b = number % 100; // 得到除了百位上的其他位数
int c = number % 10; //个位
int d = b / 10; //十位
if (number == a * a * a + c * c * c + d * d * d) {
System.out.println("您输入的数为水仙花树");
break;
} else {
System.out.println("您输入的不是,请在此输入!");
}
注:用while循环,通过输入数字,判断是否为水仙花数,直至输入的数字为水仙花树!
4.输出1~100不能被5整除的数,每输入5个换行
public static void main(String[] args) {
int count = 0;
for(int i = 1;i <= 100;i++){
if(i % 5 == 0){
continue;
}
else{
System.out.print(i + " ");
count++;
if(count % 5 ==0){
System.out.println(" ");
}
}
}