-Scanner实例,猜数字。html
package src.week1; import java.util.Scanner; public class g { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);//创建scanner实例 int num=(int)(Math.random()*10); int guess; do { System.out.print("猜数字(0-9):"); guess=scanner.nextInt(); }while (guess!=num); System.out.println("year!!"); } }
代码运行结果以下图所示
Scanner的NextInt()方法会看看标准输入中有无输入下一个字符串,有的话将至剖析为int型。
-使用java.math.BigDecimaljava
package src.week1; import java.math.BigDecimal; public class h { public static void main(String[] args) { BigDecimal op1=new BigDecimal("0.1"); BigDecimal op2=new BigDecimal("0.1"); BigDecimal op3=new BigDecimal("0.1"); BigDecimal result=new BigDecimal("0.8"); if(op1.add(op2).add(op3).equals(result)) { System.out.printf("==0.3"); } else System.out.println("!=0.3"); } }
java遵照浮点数运算规范,对于0.1会无限循环下去,没法精确,故可使用java.math.BigDecimal方法,在建立时会剖析传入字符串,以默认精度进行接下来的运算。git
package src.week2; public class g { public static void main(String[] args) { int[][] cords={ {1,2,3}, {4,5,6} };//数组初始化 int[][] cords1=new int[][] { {1,2,3}, {4,5,6} };//数组也能够这样初始化 for(int x=0;x<cords.length;x++)//数组有几行 { for(int y=0;y<cords[x].length;y++)//每行有几个元素 { System.out.printf("%2d",cords[x][y]); }System.out.println(); } for (int[] hang:cords)//数组有几行 { for(int lie:hang)//每行有几个元素 System.out.printf("%2d",lie); } } }
结果以下图所示:
上述代码中,hang参考到的对象就是一维数组对象,外层for循环就是循环取得cords参考对象的每一个索引,将参考到的对象指定给int[]类型的hang名称。cords.length的值就是问cords参考对象索引0de参考对象的长度是什么,答案是3.编程
Integer[][] cords=new Integer[3][2];
package src.week1; import java.util.Arrays; public class d { public static void main(String[] args) { int scores1[]={1,2,3,4,5,6,7,8,9}; int scores2[]=Arrays.copyOf(scores1,scores1.length); for(int a:scores2) { System.out.printf("%3d",a); } System.out.println(); scores2[0]=99; for (int x:scores1) { System.out.printf("%3d",x); } } }
执行结果以下图所示:数组
package src.week1; class clothes{ String color; char size; clothes(String color,char size) { this.color=color; this.size=size; } } public class d { public static void main(String[] args) { clothes[] c1={new clothes("red",'l'),new clothes("red",'l')}; clothes[]c2=new clothes[c1.length]; for(int i=0;i<c1.length;i++) { c2[i]=c1[i]; } c1[0].color="yellow"; System.out.println(c2[0].color); } }
for(int i=0;i<c1.length;i++) { clothes c=new clothes(c1[i].color,c1[i].size);//自行复制元素 c2[i]=c; }
结果一样也是“yellow”,但这种复制被叫作浅层复制,仅仅是将c1中的每一个参考对象也给c2每一个索引来参考。至关于c1,c2索引的对象都是同一个。而深层复制行为也就是说,将c1每一个索引参考的对象被复制后分别指定给c2的每一个索引,即索引的参考对象相同但不是同一个。dom
package src.week2; public class h { public static void main(String[] args) { String a1="hello"; String a2="hello"; String a3=new String("hello"); String a4=new String("hello"); System.out.println(a1==a2); System.out.println(a3==a2); System.out.println(a3==a4); System.out.println(a1.equals(a2)); System.out.println(a2.equals(a3)); System.out.println(a3.equals(a4)); } }
代码执行结果以下:在java中,以“”包括的字符串,只要内容彻底相同,不管其出现几回,JVM都只会创建一个String实例,并在字符串池中进行维护。“==”用于指向对象彻底相同两个实例才会出现true。而在上述代码中,a3和a4分别参考至新建的String实例。故获得的结果是false。但使用equals(),就能够获得不一样的答案,由于其强调的是比较对象内容是否相同。函数
package src.week2; public class h { public static void main(String[] args) { String a="adfaads"; System.out.println(a); System.out.println(a.length());//取得字符串长度 System.out.println(a.charAt(2));//取得字符串中第2个字符 System.out.println(a.toUpperCase());//将字符串中的全部字符大写 char[] cs={'a','a','d','f'}; String b=new String(cs); char[]cs2=a.toCharArray();//将字符串以数组形式返回 System.out.println("你的名字:" +a);// } }
执行结果以下学习
方法 | 说明 |
---|---|
Byte.parseByte(a); | 将a剖析成byte整数 |
Short.parseShort(a); | 将a剖析成Short整数 |
Long.parseLong(a); | 将a剖析long整数 |
Integer.parseInt(a); | 将a剖析成Integer整数 |
Float.parseFloat(a); | 将a剖析成float整数 |
Double.parseDouble(a); | 将a剖析成double整数 |
package src.week2; class cashcard { String number; int balance; int bonus; cashcard(String number,int balance,int bonus) { this.number=number; this.balance=balance; this.bonus=bonus; } }public class h { public static void main(String[] args) { cashcard[] a = { new cashcard("444", 455, 1), new cashcard("434", 455, 1), new cashcard("424", 455, 1), }; for (cashcard cash : a) { System.out.printf("(%s,%d,%d)%n", cash.number, cash.balance, cash.bonus); } } }
用java的构造语法,实现了对向初始化的封装,this
package src.week2; import java.util.Scanner; class cashcard { String number; int balance; int bounce; cashcard(String number,int balance,int bounce) { this.number=number; this.balance=balance; this.bounce=bounce; }; void store (int money) { if(money>0) { this.number+=money; if(money>=1000) { this.bounce++; } }else { System.out.println("储值不能为负"); } } void charge(int money) {if(money>0) { if(money <=this.balance) { this.balance-=money; } else System.out.printf("钱不够了"); } else System.out.printf("取值不能为负"); } int exchange(int bounce) { if(bounce>0){ this.bounce-=bounce; } return bounce; } } public class h { public static void main(String[] args) { Scanner a=new Scanner(System.in); int money,bounce; money=a.nextInt(); bounce=a.nextInt(); cashcard a1=new cashcard("a111",100,5); a1.store(money); cashcard a2=new cashcard("a111",100,5); a2.charge(money); cashcard a3=new cashcard("a111",100,5); a3.exchange(bounce); } }
若是不用返回值,方法名称前能够用void。创建cashcard的方法以下.net
cashcard [] a= { new cashcard("a44",12,2), .... }
cashcard a1=new cashcard("a111",123,1); cashcard a2=new cashcard("a111",123,1); cashcard a3=new cashcard("a111",123,1);
两者的前提是:
class cashcard { String number; int balance; int bounce; cashcard(String number,int balance,int bounce) { this.number=number; this.balance=balance; this.bounce=bounce; };
class cashcard2 { private String number; private int balance; private int bounce;//使用private定义私有成员 }
int getBalance() { return balance; } int getBounce() { return bounce; } String getNumber() { return number;//提供取值方法成员; }
此时咱们封装了类私有数据,让用户没法直接存取,必须经过我所提供的方法,即要想调用private定义过的值,必须使用同一个包内的方法,而且用户也不会知道对象的内部细节。取值方法,形如getBalance(),等取值方法。
package src.week2; public class p {//这是一个公开类; public static void main(String[] args) {//main()方法 } public p(String number, int balance, int bounce)//这是公用构造函数; { } public static void main(int money)//公开方法; { } public int charge()//公开方法; { return 4; } }
声明类加上public后表示它是个公开类,其余包能够直接使用,在构造函数上声明public,表示其余包的类能够直接调用这个构造函数。在方法中声明public,表示其余包的方法中能够直接调用这个方法。一样也能够在数据成员上声明public。
main()方法是java应用程序的入口方法,没有这个方法,java程序没法编译。这个方法必须是public static void类型的。方法必须接受一个字符串数组的参数等等。
-构造函数是与类名称同名,无需声明返回类型的方法。建立对象时,数据成员会初始化,若是没有指定初始值,会使用默认值初始化,参考P133表5.1。若是定义类时没有撰写任何构造函数,会自动加入默认构造函数,因此在没有撰写任何构造函数时,也能够这样以无自变量方式构造函数:
Some a=new Some();
注意:自行撰写的无参数无内容的构造函数不能成为默认构造函数。
能够定义多个构造函数,只要参数类型或者个数不一样。可用意志的名称来调用相似功能的方法,方法重裁可根据传递自变量的类型不一样,也可根据参数列个数的不一样来设计方法重裁。注意:返回值类型不一样不可做为方法重裁的依据。
package src.week2; public class p {//这是一个公开类; public static void main(String[] args) {//main()方法 } public int some(int i) { return 0; } public int some(int i) { return 0.0; }
以上编译出现两个错误:1.重复定义 2.返回值类型出错。
在对象创建后为“这个对象”的参考名称。在构造函数参数与对象数据同名时,能够用this加以区别。
package src.week2; public class p{ public static void main(String[] args) { } private int a=10; private String text="n.a."; public p(int a) { if(a>0) { this.a=a; } } public p(int a,String text) { this(a); if(text!=null) { this.text=text; } } }
this()表明了调用另外一个构造函数,至于调用哪一个构造函数,则视调用this()时给的自变量类型与个数而定,能够避免程序中出现重复的语句。同时可使用可使用this()来进行调用函数,至于调用的是哪一个函数则视调用this()时给的自变量类型与个数而定。
package src.week2; public class p { final int x;//编译出错 public static void main(String[] args) { } p() { }//若是调用这个函数,x就没有被赋值。编译出错 p(int c,int b) { this(10);//至关于引用了p(int 10)即下面这个函数对final对象成员x设值 } p(int x){ this.x=x;//对final对象成员x设值 } }
class ball{ double a; static final double PI=3.1459; }
就能够这样取得圆周率:
System.out.println(ball.PI);
注意:遵照命名习惯;static方法或者区块中不能出现this关键字。由于static成员属于类,而不是个别对象。
package src.week2; class ball { double a; static void a() { double i = this.a; } void c() { } static void v() { c();//隐含使用了this.c() } }
就会出现编译错误。
-注意:int...声明的变量实际上展开为数组;在使用不定长度自变量时,必须是参数列中的最后一个,使用两个以上不定长度自变量也是不合法的,使用对象的不定长度自变量不能和方法相同。
package src.week2; public class p{ public static void main(String[] args) { some a=new some(); some.other o=a.new other(); o.doother(); } } class some{ static int x=10; int y=7; class other{ void doother() { System.out.println(x); System.out.println(y);//出现错误,static内部类不可存取外部类非static成员。 } } }
内部类自己能够存取外部类的成员。虽然将外部类看成名称空间,但也算是个独立的类,它能够存取外部类static成员,但不可存取外部类非static成员。
package src.week2; import java.math.BigDecimal; public class k { public static void main(String[] args) { int x=100; int u=100; System.out.println(x==u); BigDecimal a=new BigDecimal(1); BigDecimal b=new BigDecimal(1); System.out.println(a==b); } }
为何结果是true,false。
Integer x=100; Integer u=new Integer(100);
获得的结果是false。书上只说了以“”包括的字符串,只要内容彻底相同,不管其出现几回,JVM都只会创建一个String实例,并在字符串池中进行维护。“==”用于指向对象彻底相同两个实例才会出现true。而int类型也是如此,只有 Integer u=new Integer(100);新建一个实例才会指向不一样。
a3.exchange(bounce);System.out.printf("%n%d", a3.exchange(bounce));
但发现得出的是奇怪的数字并非正确结果。因而我开始调试代码,发如今函数末位的返回值是正确的,我又在函数末位加上
System.out.printf("%n%d", this.bounce);
发现结果是正确的。最后发现错误所在,上述代码输入有误,不该该是"%n%d", a3.exchange(bounce)而应该是"%n%d", a3.bounce。最后能够获得正确答案。
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 120/120 | 1/1 | 16/16 | 开始了JAVA学习的第一步! |
第二周 | 346/466 | 1/2 | 23/36 | 了解并学习了Java基础语法 |
第三周 | 364/830 | 1/3 | 21/57 | 进一步了解java设计语句 |
计划学习时间:18小时
实际学习时间:21小时
改进状况:能够不参照书本打出想要的代码了,虽然有时仍是会编译出错,但相较于上一周已经有了很大的进步。