第一天:
/*
关键字:被Java语言赋予特定含义的单词
特点:
A:组成关键字的字母全部小写
B:常见的代码编辑器,针对关键字有特殊的颜色标记
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
第二天:
package com.itheima;
import java.util.Scanner;
/*
* 键盘录入三个数据,获取这三个数据中的最大值
*/
public class ScannerTest3 {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in);
// 接收数据
System.out.println("请输入第一个数据:");
int a = sc.nextInt();
System.out.println("请输入第二个数据:");
int b = sc.nextInt();
System.out.println("请输入第三个数据:");
int c = sc.nextInt();
// 如何获取三个数据的最大值
int temp = (a > b ? a : b);
int max = (temp > c ? temp : c);
System.out.println("max:" + max);
}
}
第三天:package com.itheima_07;
/*
* 按要求分析结果,并验证
*
* break:输出2次
* continue:输出7次
*/
public class BreakAndContinueDemo {
public static void main(String[] args) {
for (int x = 1; x <= 10; x++) {
if (x % 3 == 0) {
// 分别写break,continue,说说输出几次
//break;
continue;
}
System.out.println("我爱林青霞");
}
}
}
第四天:
package com.itheima_02;
/*
* 两个常见小问题:
* A:java.lang.ArrayIndexOutOfBoundsException
* 数组越界异常
* 产生的原因:就是你访问了不存在的索引元素。
* B:java.lang.NullPointerException
* 空指针异常
* 产生的原因:数组已经不指向堆内存的数据了,你还使用数组名去访问元素。
* 为什么我们要记住这样的小问题呢?
* 编程不仅仅是把代码写出来,还得在出现问题的时候能够快速的解决问题。
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = { 1, 2, 3 };
//System.out.println(arr[3]);
//引用类型:类,接口,数组
//常量:空常量 null,是可以赋值给引用类型的
//arr = null;
System.out.println(arr[1]);
}
}
第五天:
public class ArgsDemo {
public static void main(String[] args) {
// 定义变量
int a = 10;
int b = 20;
System.out.println("a:" + a + ",b:" + b);// a:10,b:20
change(a, b);
System.out.println("a:" + a + ",b:" + b);// a:10,b:20
}
public static void change(int a, int b) { // a=10,b=20
System.out.println("a:" + a + ",b:" + b);// a:10,b:20
a = b; // a=20;
b = a + b;// b=40;
System.out.println("a:" + a + ",b:" + b);// a:20,b:40
}
}
第六天:
package com.itheima;
/*
* 需求:打印5位数中的所有回文数。
* 什么是回文数呢?举例:12321是回文数,个位与万位相同,十位与千位相同。
*
* 分析:
* A:5位数告诉了我们数据的范围,用for循环实现
* B:获取每一个5位数,然后得到它的个位,十位,千位,万位
* 假设x是一个5位数:
* 个位:x%10
* 十位:x/10%10
* 千位:x/10/10/10%10
* 万位:x/10/10/10/10%10
* C:把满足条件的数据输出即可
*/
public class Test3 {
public static void main(String[] args) {
//5位数告诉了我们数据的范围,用for循环实现
for(int x=10000; x<100000; x++) {
//获取每一个5位数,然后得到它的个位,十位,千位,万位
int ge = x%10;
int shi = x/10%10;
int qian = x/10/10/10%10;
int wan = x/10/10/10/10%10;
//把满足条件的数据输出即可
if((ge==wan) && (shi==qian)) {
System.out.println(x);
}
}
}
}
第七天:
package com.itheima_08;
/*
* 构造方法:
* 给对象的数据进行初始化
*
* 格式:
* 方法名和类名相同
* 没有返回值类型,连void都不能写
* 没有具体的返回值
*
*/
public class Student {
public Student() {
System.out.println("这是构造方法");
}
}
package com.itheima_08;
public class StudentDemo {
public static void main(String[] args) {
//如何调用构造方法呢?
//通过new关键字调用
//格式:类名 对象名 = new 构造方法(...);
Student s = new Student();
}
}
第八天:
3.3.1.1案例代码十五:
package com.itheima_03;
/*
* StringBuilder和String的相互转换
*
* StringBuilder -- String
* public String toString():通过toString()就可以实现把StringBuilder转成String
*
* String -- StringBuilder
* StringBuilder(String str):通过构造方法就可以实现把String转成StringBuilder
*/
public class StringBuilderTest {
public static void main(String[] args) {
//StringBuilder -- String
/*
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
String s = sb.toString();
System.out.println(s);
*/
//String -- StringBuilder
String s = "helloworld";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb);
}
}
第九天:
package com.itheima;
/*
* 创建一个学生数组,存储三个学生对象并遍历
*
* 分析:
* A:定义学生类
* B:创建学生数组
* C:创建学生对象
* D:把学生对象作为元素赋值给学生数组
* E:遍历学生数组
*/
public class StudentDemo {
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];
//创建学生对象
Student s1 = new Student("曹操",40);
Student s2 = new Student("刘备",35);
Student s3 = new Student("孙权",30);
//把学生对象作为元素赋值给学生数组
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍历学生数组
for(int x=0; x<students.length; x++) {
Student s = students[x];
//System.out.println(s);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
第十天:
package com.itheima_05;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 需求:
* 把项目路径下的FileWriterDemo.java中的内容复制到项目路径下的Copy.java中
*
* 数据源:
* FileWriterDemo.java -- 读数据 -- FileReader -- 高效的读数据 -- BufferedReader
* 目的地:
* Copy.java -- 写数据 -- FileWriter -- 高效的写数据 -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
//创建输入缓冲流对象
BufferedReader br = new BufferedReader(new FileReader("FileWriterDemo.java"));
//创建输出缓冲流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
//读写数据
String line;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
br.close();
}
}
此上是基础班学习中的一些习题 !!! |
|