A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

汪航

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© 汪航 初级黑马   /  2018-4-5 14:32  /  731 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

ASDFGHJKL
第六天:
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();
        }
}


                                              此上是基础班学习中的一些习题 !!!

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马