Java每日编程day2
Java每日编程day2
第一题
package com.pcx.day2; /* * 九九乘法表并逆序 */ public class JiuJiu { public static void main(String[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+"*"+i+"="+i*j+"\t"); } System.out.println(); }
System.out.println("=========================================="); for (int i = 9; i >0; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+"*"+i+"="+i*j+"\t"); } System.out.println(); } } } |
第二题
package com.pcx.day2; /** * 在控制台打印菱形 * @author Administrator * */ public class LingXing { public static void main(String[] args) { //前三行 for (int i = 0; i <=3; i++) { for (int j = 0; j <= 2-i; j++) { System.out.print(" "); } for (int j = 0; j <= 2*i; j++) { System.out.print("*"); } System.out.println(); } //后三行 for (int i = 0; i <=3; i++) { for (int j = 0; j <=i; j++) { System.out.print(" "); } for (int j = 0; j < 5-2*i; j++) { System.out.print("*"); } System.out.println(); } } } |
package com.pcx.day2;
import java.util.Arrays;
/* * 冒泡排序 */ public class PaiXu { public static void main(String[] args) { int []array={2,1,3,4,6,67,9,3,54}; for (int i = 0; i < array.length; i++) { for (int j = i; j < array.length; j++) { if (array[i]>array[j]) { int temp=array[i]; array[i]=array[j]; array[j]=temp; } } } System.out.println(Arrays.toString(array)); } }
|
package com.pcx.day2; /* * 100内的素数 * 素数的因子只有1和他本身 * 对正整数n,如果用2到之间的所有整数去除,均无法整除,则n为质数 */ public class SuShu { public static boolean isSu(int x) { for (int i = 2; i <=Math.sqrt(x); i++) { if (x%i==0) { return false; } } return true; }
public static void main(String[] args) { for (int i = 2; i < 100; i++) { if (isSu(i)) { System.out.println(i+"是素数"); } } } } |