package unite3; import java.io.Console; import java.math.BigInteger; import java.util.Arrays; import java.util.Date; import java.util.Scanner; public class Example1 { public static void main(String[] args){ //3.1建档java程序 /*类名以大写字母开头的名词 * main方法必须为静态,public */ //3.2注释 //三种注释方法 (当前行) /* * 多行的注释方法 * * */ /** * 可以自动生成文档 * */ //3.3数据类型 /* * 共有8中数据类型 long int short byte (整型) float double(浮点型) char(为unicode编码表)boolean * 8 4 2 1 4 8 */ //System.out.println(5/0);//正整数除以零会抛异常 书中说为NaN //3.4变量 /*命名很重要,看公司编码规范 * 需要声明并初始化 */ //3.5运算符 /* * 一些运算符的使用(优先级) 数学函数 * */ //强制类型转换 强制切去多余位 (也可以调用round方法进行四舍五入) double n1 = 9.997; int n2 = (int) n1; int n3 = (int) Math.round(n1); System.out.println(n2 +" "+n3); System.out.println(" "); //String String a = "hahahahaee"; boolean b = a.endsWith("hahaee"); //判断是否以haha结尾 System.out.println("判断是否以hahaee结尾:"+" "+b); System.out.println("判断是否以hahaee开始:"+" "+a.startsWith("ha")); System.out.println("判断字符串是否相同:"+" "+b); System.out.println("判断字符串是否相同(忽略大小写):"+" "+a.equalsIgnoreCase("Hahahahaee")); System.out.println("返回第一个匹配索引的位置,不存在返回-1:"+" "+a.indexOf("e")); System.out.println("返回第一个匹配索引的位置,不存在返回-1:"+" "+a.lastIndexOf("e")); System.out.println("返回字符串长度:"+" "+a.length()); System.out.println(" "); //3.7输入输出 /* * */ // Scanner in = new Scanner(System.in); // String name = in.nextLine(); //输入string类型 // System.out.println("name: "+name); // int age = in.nextInt();//int 类型 其他也一样 // System.out.println("age: "+age); //会报空指针异常,这可能是直接读取数据的吧,有待了解。。。 // Console cons = System.console();//之前输入为显示输入,账号密码试用于从控制台读取密码 // String username = cons.readLine("User name: "); // System.out.println("username: "+username); // char[] password = cons.readPassword("password : "); // System.out.println("password: "+password); //解决方法 取自http://blog.csdn.net/fantasy_wxe/article/details/38391941 // Console cons = System.console(); // if(cons != null) // { // System.out.print("User Name:"); // String username = cons.readLine(); // // System.out.print("Password: "); // char[] passwd = cons.readPassword(); // } // else // { // System.out.println("Console object is null"); // } //jvm不是在命令行中被调用没有与系统的交互,所以为空 详见http://blog.sina.com.cn/s/blog_a4f75d8001018wvf.html //3.7.2格式化的输出 /* * 只能在printf中使用格式化输出 */ double x = 2333.333333; System.out.println("x=" + x); System.out.printf("x=%8.2f",x);//包含一个为4字节的空格符 System.out.println(" "); System.out.printf("%tc", new Date()); System.out.println(" "); //3.8控制流程 /* * if else * while do while(至少执行一次) * for循环和增强for循环 * swich语句 * break */ //3.9大数值 /* * BigInteger 任意精度整数运算 */ BigInteger n39 = BigInteger.valueOf(100);//将普通述职转化为大数值 BigInteger n391 = n39.add(n39).subtract(n39).multiply(n39).divide(n39).mod(n39); //加减乘除余 n391.compareTo(n39);//两个大数值相比。相同返回0,大于为正,小于为负 /* * BigDecimal 任意精度浮点数运算 * 用法同上 */ //3.10数组 /* * */ int[] n310 = new int[100];//创建一个长度为100的数组 所有元素初始化为0 boolean数组初始化为ifalse 对象数组初始化为null 例如String字符串 int[] n3101 = {1,2,3}; for(int element : n3101) System.out.print(element);//用增强for循环遍历比较方便 int[] copyn3101 = Arrays.copyOf(n3101, n3101.length);//数组之间进行拷贝 前一个参数为数组名称,后一个为长度(可改变) for(int element : copyn3101) System.out.print(element); //数组排序 Arrays.sort(n3101);//对数组进行快速排序 String n3102 = Arrays.toString(n310);//将数组转换成字符串进行输出 System.out.println(n3102); Arrays.fill(n3101, 2333);//将数组中所有的值都变为2333 //3.10.6多维数组 double[] [] n3106 = new double[4][5];//创建一个多维数组 int[] [] n31061 = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; } }