- //java阶乘求和1!+2!+・・・・+n!
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- int t=0;//阶乘和
- for (int j = 1; j < n+1; j++) {
- int x = 1;//阶乘
- for (int i = 1; i < j+1 ; i++) {
- x = x * i;//求阶乘
- }
- t=t+x;//求阶乘和
- }
- System.out.println(t);
- }
- //java九九乘法表
- public static void main(String[] args) {
- for(int i=0;i<9;i++){
- for(int j=0;j<i+1;j++){
- int x =(i+1)*(j+1);
- System.out.print((j+1)+"*"+(i+1)+"="+x);
- System.out.print(" ");
- }
- System.out.println();
- }
- }
- //用*打印一个等腰三角形
- public static void main(String[] args) {
- for (int i = 0; i < 9; i++) {
- if (i < 4) {
- for (int j = 0; j < i+1; j++) System.out.print("*");
- }else{
- for(int j=9;j>i;j--)System.out.print("*");
- }
- System.out.println();
- }
- //使用字母打印正三角形
- public static void main(String[] args) {
- String[] a = {"A", "B", "C", "D", "E", "F", "G"};
- for (int i = 0; i < 7; i++) {
- for (int j = 1; j < 14; j++) {
- if (j < 7 - i || j > 7 + i) System.out.print(" ");
- else System.out.print(a[i] + " ");
- }
- System.out.println();
- }}
- //查找数组中的最大值与最小值,数组int[] a ={10,98,78,65,90,15,67};
- public static void main(String[] args) {
- int[] a = {10, 98, 78, 100, 90, 15, 67};
- int max=a[0];
- for (int j = 1; j < a.length - 1; j++) {
- if(max<a[j])max=a[j];
- }
- System.out.println(max);
- }
- }
- //冒泡排序算法
- public static void main(String[] args) {
- int[] a = {10, 98, 78, 100, 90, 15, 67};
- int temp;
- for (int j = 0; j < a.length - 1; j++) {
- for (int i = 0; i < a.length - 1; i++) {
- if (a[i] > a[i + 1]) {
- temp = a[i];
- a[i] = a[i + 1];
- a[i + 1] = temp;
- }
- }
- }
- for (int i = 0;i < a.length;i++) {
- System.out.println(a[i]);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1501201511548.html
来源: http://www.codesnippet.cn/detail/1501201511548.html