package javaxgp.teacher.test; import java.math.BigInteger; import java.util.Scanner; import java.util.TreeSet; /** * java课第一次作业 */ public class Demo1 { public static void main(String[] args) { /** *进制转换 */ // method1(); /** * 寻找素数 */ // method2(); /** * 计算PI */ // method3(); /** * 排序问题 */ method4(); /** * 5,傻瓜操做,略 */ } private static void method4() { TreeSet<Integer> treeSet = new TreeSet(); Scanner scanner = new Scanner(System.in); for(int i = 0;i < 10;i++) { System.out.println("请输入第" + (i + 1) + "个整数"); treeSet.add(scanner.nextInt()); } for (Integer out : treeSet) { System.out.println("排序结果:"); System.out.print(out + " "); } } private static void method3() { double pi=0.0d; for(int i=1;i<=1000000;i++) { pi+=Math.pow(-1,(i+1))*4/(2*i-1); } System.out.println("近似pi = " + pi); System.out.println("math库的pi = " + Math.PI); System.out.println("误差值(pi - Math.PI) " + (pi -Math.PI)); } private static void method2() { BigInteger init = BigInteger.ONE; while ((init = init.nextProbablePrime()).compareTo(new BigInteger("1000")) == -1) { System.out.println(init); } } private static void method1() { try { long input = 0; String num2 = null; String num8 = null; String num10 = null; String num16 = null; Scanner scanner = new Scanner(System.in); while(true) { System.out.println("请输入要转换的数字(输录字母q退出使用):"); String str = scanner.nextLine(); if("q".equals(str)) { break; } System.out.println("该数字的进制为(2,10,8,16):"); int num = scanner.nextInt(); if(!str.isEmpty()) { switch (num) { case 2:input = Long.parseLong(str, num);break; case 8:input = Long.parseLong(str, num);break; case 10:input = Long.parseLong(str, num);break; case 16:input = Long.parseLong(str, num);break; } num2 = Long.toBinaryString(input); num8 = Long.toOctalString(input); num10 = Long.toString(input); num16 = Long.toHexString(input); System.out.println("该数字的2进制为:" + num2); System.out.println("该数字的8进制为:" + num8); System.out.println("该数字的10进制为:" + num10); System.out.println("该数字的16进制为:" + num16); } } }catch (Exception e) { System.out.println("你的输录有误,程序退出!"); }finally { System.out.println("欢迎你的再次使用!"); } } }