From 6bb791239c72650bca07c0817096d6fc2a21cd60 Mon Sep 17 00:00:00 2001 From: psckwxi8l <929144989@qq.com> Date: Wed, 13 Dec 2023 17:05:05 +0800 Subject: [PATCH] Update README.md --- README.md | 370 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 370 insertions(+) diff --git a/README.md b/README.md index f4ea443..bfaee57 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # java +高级特性IO流 package step4; import java.io.FileInputStream; @@ -39,3 +40,372 @@ public class Task { } } +分支结构 +package step4; + +import java.util.Scanner; + +public class HelloSwitch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("请输入月份:"); + + int input = sc.nextInt(); //获取输入的月份 + + //通过输入的月份来判断当前季节并输出 + /*****start*****/ + + +switch (input) { +case 1: +System.out.println(input+"月是冬天"); +break; +case 2: +System.out.println(input+"月是冬天"); +break; +case 3: +System.out.println(input+"月是春天"); +break; +case 4: +System.out.println(input+"月是春天"); +break; +case 5: +System.out.println(input+"月是春天"); +break; +case 6: +System.out.println(input+"月是夏天"); +break; +case 7: +System.out.println(input+"月是夏天"); +break; +case 8: +System.out.println(input+"月是夏天"); +break; +case 9: +System.out.println(input+"月是秋天"); +break; +case 10: +System.out.println(input+"月是秋天"); +break; +case 11: +System.out.println(input+"月是秋天"); +break; +case 12: +System.out.println(input+"月是冬天"); +break; + + +} + + /*****end*****/ + + } +} + + + + + + +package step4; + +import java.util.Scanner; + +public class HelloSwitch { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("请输入月份:"); + + int input = sc.nextInt(); //获取输入的月份 + + //通过输入的月份来判断当前季节并输出 + /*****start*****/ + + +switch (input) { +case 1: +System.out.println(input+"月是冬天"); +break; +case 2: +System.out.println(input+"月是冬天"); +break; +case 3: +System.out.println(input+"月是春天"); +break; +case 4: +System.out.println(input+"月是春天"); +break; +case 5: +System.out.println(input+"月是春天"); +break; +case 6: +System.out.println(input+"月是夏天"); +break; +case 7: +System.out.println(input+"月是夏天"); +break; +case 8: +System.out.println(input+"月是夏天"); +break; +case 9: +System.out.println(input+"月是秋天"); +break; +case 10: +System.out.println(input+"月是秋天"); +break; +case 11: +System.out.println(input+"月是秋天"); +break; +case 12: +System.out.println(input+"月是冬天"); +break; + + +} + + /*****end*****/ + + } +} + +数组 +package step2; + +import java.util.Scanner; + +public class HelloWorld { + public static void main(String[] args) { + + + /********** Begin **********/ + //在这里定义一个长度为4的字符串数组,用来存放学生姓名 +String[] stuNames = new String[]{"张三","张无忌","张三丰","张岁山"} ; + + //在这里给stuNames数组赋值 分别为 张三,张无忌,张三丰,张岁山 + + + + //在这里输出stuNames数组中的数据 + System.out.println("数组中的第一个数据为:" + stuNames[0]); + System.out.println("数组中的第二个数据为:" + stuNames[1]); + System.out.println("数组中的第三个数据为:" + stuNames[2]); + System.out.println("数组中的第四个数据为:" + stuNames[3]); + + + int[] scores; + Scanner sc = new Scanner(System.in); + //在这里使用Scanner获取系统输入的整数,并用获取到的数据来设置scores数组的长度 + int length = sc.nextInt(); ; + scores = new int[length]; ; + /********** End **********/ + + System.out.println("数组scores的长度为:" + scores.length); + } +} + +对象封装 +package case1; + +public class TestPersonDemo { + public static void main(String[] args) { + /********* begin *********/ + // 声明并实例化一Person对象p + Person p = new Person(); + // 给p中的属性赋值 + p.setName("张三"); + p.setAge(18); + // 调用Person类中的talk()方法 + p.talk(); + /********* end *********/ + + } +} + +// 在这里定义Person类 +class Person { + /********* begin *********/ + + private String name; + private int age; + public String getNmae(){ + return name; + } + public void setName(String name){ + this.name = name; + } + public int getAge(){ + return age; + } + public void setAge(int age){ + this.age = age; + } + public void talk(){ + System.out.println("我是:"+name+",今年:"+age+"岁"); + } + /********* end *********/ +} + + + + + + + + + + + + +package case2; + +public class extendsTest { + public static void main(String args[]) { + // 实例化一个Cat对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息 + /********* begin *********/ + Cat cat = new Cat(); + cat.name = "大花猫"; + cat.age = "6岁"; + cat.voice(); + cat.eat(); + System.out.println(cat.name+cat.age); + /********* end *********/ + + // 实例化一个Dog对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息 + /********* begin *********/ + Dog dog = new Dog(); + dog.name = "大黑狗"; + dog.age = "8岁"; + dog.voice(); + dog.eat(); + System.out.println(dog.name+dog.age); + + /********* end *********/ + + } +} + +class Animal { + /********* begin *********/ + String name; + String age; + /********* end *********/ + +} + +class Cat extends Animal { + // 定义Cat类的voice()和eat()方法 + /********* begin *********/ + + public void voice(){ + System.out.println(name+"喵喵叫"); + } + public void eat(){ + System.out.println(name+"吃鱼"); + } + /********* end *********/ +} + +class Dog extends Animal { + // 定义Dog类的voice()和eat()方法 + /********* begin *********/ + public void voice(){ + System.out.println(name+"汪汪叫"); + } + public void eat(){ + System.out.println(name+"吃骨头"); + } + /********* end *********/ +} + + + + + + + + + + + + + + + + + +package case7; + +public class interfaceTest { + public static void main(String[] args) { + // 实例化一Student的对象s,并调用talk()方法,打印信息 + /********* begin *********/ + Student s = new Student(); + s.talk(); + /********* end *********/ + + } +} + +// 声明一个Person接口,并在里面声明三个常量:name、age和occupation,并分别赋值,声明一抽象方法talk() +interface Person { + /********* begin *********/ + String name = "张三"; + int age = 18; + String occupation = "学生"; + public abstract void talk(); + + /********* end *********/ +} + +// Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息 +class Student implements Person { + + + /********* begin *********/ + public void talk() { + System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!"); + } + /********* end *********/ +} + + + +线程 +package step1; + +//请在此添加实现代码 +/********** Begin **********/ +public class ThreadClassOne extends Thread{ + public int i=0; + public ThreadClassOne(){ + super(); + } + public void run(){ + for(i=0;i<10;i++){ + if(i%2==1) + System.out.print(i+" "); + } + } +} +/********** End **********/ + + + + +package step1; + +//请在此添加实现代码 +/********** Begin **********/ +public class ThreadClassTwo implements Runnable{ + public int i=0; + private Thread mythread; + public void run(){ + for(i=0;i<11;i++){ + if(i%2==0) + System.out.print(i+" "); + } + } +} +/********** End **********/