for 练习

时间:2019-02-11
本文章向大家介绍for 练习,主要包括for 练习使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

水仙花数

import java.util.Scanner;

public class forTest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count = 0;
		for(int i=100;i<=999;i++) {
			int a = i/100%10;
			int b = i/10%10;
			int c = i%10;
			if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3)==i) {
				count++;
				System.out.println(i);
			}
		}
		System.out.println(count);
	}
}

倒三角

public class forTest {
	public static void main(String[] args) {
		int i=5;
		for (int x=0;x<5;x++) //外循环控制有几行
		{
			for(int y=1;y<i;y++)//内循环控制每列有几个
			{
				System.out.print("* ");
			}
			System.out.println();
			i--;
		}
		
		int j=1;
		for (int x=0;x<5;x++) //外循环控制有几行
		{
			for(int y=j;y<5;y++)//内循环控制每列有几个
			{
				System.out.print("* ");
			}
			System.out.println();
			j++;
		}
		
	}
}


* * * * 
* * * 
* * 
* 

* * * * 
* * * 
* * 
* 

12345
1234
123
12
1

public class forTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=5;
		for (int x=0;x<=5;x++) //外循环控制有几行
		{
			for(int y=1;y<=i;y++)//内循环控制每列有几个
			{
				System.out.print(y);
			}
			System.out.println();
			i--;
		}

	}
}

54321
5432
543
54
5

public class forTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=1;
		for (int x=1;x<=5;x++) //外循环控制有几行
		{
			for(int y=5;y>=i;y--)//内循环控制每列有几个
			{
				System.out.print(y);
			}
			System.out.println();
			i++;
		}
	}
}

54321
4321
321
21
1

public class forTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(int i=5;i>0;i--) {
			for(int j=i;j>0;j--) {
				System.out.print(j);	
			}
			System.out.println();
		}
	}
}

九九乘法表

public class forTest6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int x=1; x<=9 ;x++ )
		{
			for (int y=1; y<=x ;y++ )
			{
				System.out.print(y+"*"+x+"="+y*x+"\t");
			}
			System.out.println();
		}
	}
}