首先补充昨晚的最后一问,五次机会猜数
代码如下:
package nlday01;
import java.util.Scanner;
public class Guess {
public static void main(String[] args) {
int flag = 5;
int a = (int) (Math.random() * 100);
System.out.println(a);// 测试五次机会专用
System.out.println("Please input your number:");
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
while (number != a) {
if(flag == 1){
System.out.println("you have no choice");
break;}
if (number > a) {
flag--;
System.out.println("too big");
number = scan.nextInt();
} else {
flag--;
System.out.println("too small");
number = scan.nextInt();
}
}
if (number == a) // 这边我掉进一个逻辑错误,就是五次机会内是对的和超过五次的错的
{
System.out.println("you are right!");
}
}
}
结果如下
不过如果把程序中的
if (number == a)
{
System.out.println(“you are right!”);
}
这三行放在while (number != a) 前面,运行结果中,当输出正确时,不会显示“you are right”
## 线程
package nlday02;
public class Thread1 extends Thread{
public void run(){
//定义你要执行的任务
for(int i = 0;i < 100;i++ ){
System.out.println("上单移动");
}
}
}
package nlday02;
public class Thread2 implements Runnable{
@Override
public void run() {
// 定义要执行的任务
for(int i = 0;i < 100;i++){
System.out.println("中路法师");
}
}
}
package nlday02;
public class TextThread {
public static void main(String[] args) {
new Thread1().start();
new Thread(new Thread2()).start();
}
}
学过计算机基础就不难理解
抽象类
package nlday02;
public abstract class Animal {//抽象类不能实例化
public abstract void sing();
}
package nlday02;
public class C extends Animal {
@Override
public void sing() {
// public void sing();
}
}
package nlday02;
public class Dog extends Animal {
//@Override抽象方法必须重写
public void sing() {
// TODO Auto-generated method stub
}
}
这里我不能理解,有点复杂。。。
继承
package nlday02;
public class Iwassleeping {
/**
* String API
* @author Administrator
*
*/
//public static void main(String[] args) {
int a = 345;
String ab = String.valueOf(a);
String str1 = " I,love,love,Java ";
String str2 = "Java";
String str3 = "Love";
//1.charAt(int index)返回指定索引出的char值
System.out.println(str1.charAt(0));
//2.concat(String str)将指定字符串与原字符串拼接
System.out.println(str1.concat(str2));
//3.contains(String str)是否包含指定字符串
System.out.println(str1.contains(str3));
//4.endsWith(String str)判断是否以指定字符串结尾
System.out.println(str1.endsWith("va"));
//5.startsWith(String str)判断是否以指定字符串开始
System.out.println(str1.startsWith("I"));
//6.indexOf()返回指定字符在此字符串中第一次出现处的索引。
System.out.println(str1.indexOf('a'));
//7.lastIndexOf()返回指定子字符串在此字符串中最右边出现处的索引。
System.out.println(str1.lastIndexOf('a'));
//8.replace(char oldChar, char newChar)
System.out.println(str1.replaceFirst("love", "hate"));
//9.split()按照指定符号分割字符串
String [] strs = str1.split(",");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
//10.substring(int index)截取字符串
System.out.println(str1.substring(3));
//11.substring(int beginIndex,int endIndex)
//注意:包含开头,不包含结尾
System.out.println(str1.substring(4, 6));
//12.toCharArray()
char[] chars = str1.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
//13.toLowerCase()将字符串中的大写字母转为小写
System.out.println(str1.toLowerCase());
//14.toUpperCase()将字符串中的小写字母转为大写
System.out.println(str1.toUpperCase());
//15.trim()
System.out.println(str1.trim());
}
}
这是8月13号下午,没睡午觉导致上这节课的时候我直接趴在了桌上。
这个不需要贴结果,老师直接给了下图的字典,查找功能即可。
方法重写
public class Student extends Person{
int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(){
}
public Student(int score,String name,int age,char sex){
super(name,age,sex);//这个放前面
this.score = score;
}
/* public int study(){
int count = (int)(Math.random()*10);
//方法重写
}
*/
package nlday02;
public class Person extends Object{
//定义类的属性
private String name;
private int age;
private char sex;
//如果一个类没有写构造方法,那么Java会自动给这个类加一个默认的无参构造方法
//如果给这个类写了构造方法(无论是有参还是无参),那么Java就不再给这个类提供默认的无参构造了
public Person(){
System.out.println("调用了Person类的无参构造方法");
}
public Person(String name,int age,char sex){
super();
this.name = name;
this.age = age;
this.sex = sex;
}
//定义Person 的get/set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
//重写person的toString方法
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", getName()=" + getName() + ", getAge()="
+ getAge() + ", getSex()=" + getSex() + ", study()=" + study() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
//定义类的方法
public void eat(){
System.out.println("feed my belly");
}
public void sleep(String where){
System.out.println("i am at" + where + "sleep");
}
public int study(){
int count = (int)(Math.random()*10);
System.out.println("i love study" + count + "kinds subjects");
return count;
}
public static void main(String[] args){
Person person = new Person();
person.name = "HuiZhen.Yu";
person.age = 20;
person.sex = '女';
person.eat();
person.sleep("yifulou");
person.study();
}
}
super();最好有。
package nlday02;
public class TestPerson {
public static void main(String[] args) {
Person person = new Person();
Person person2 = new Person("zhuyue",20,'女');//有参构造
//person.setName("zhuyue");
//person.setAge(20);
//person.setSex('女');
System.out.println(person2);
person.eat();
person.sleep("逸夫楼");
person.study();
Student stu = new Student();
stu.setName("小鱼鱼");
stu.setAge(-10);
stu.setSex('男');
System.out.println(stu);
// Person p1 = new Student();//继承是多态的前提
}
}
小插曲一段:今天电赛成绩出来,没有得奖的心情没有很糟,大学以来,参加了一些比赛,包括前面的蓝桥杯。我发现,只要平平淡淡不折不扣的做完所有东西,其实没必要做的很精致,因为它本身就精致了。