zoukankan      html  css  js  c++  java
  • 20145120 《Java程序设计》第3周学习总结

    20145120 《Java程序设计》第3周学习总结

    教材学习内容总结

    基本类型与类类型的概念
    在java里使用数组和字符串
    封装的概念
    在java定义函数
    重载的概念
    static的概念

    因为程序很多所以我截取部分

    猜数字

    public class Guess {
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            int number = (int) (Math.random() * 10);
            int guess;
    
            do {
                System.out.print("猜数字(0 ~ 9):");
                guess = console.nextInt();
            } while(guess != number);
            System.out.println("猜中了XD");
        }
    }
    

    使用BigDecimal

    import java.math.BigDecimal;
    
    public class DecumalDemo {
        public static void main(String[] args) {
            BigDecimal operand1 = new BigDecimal("1.0");
            BigDecimal operand2 = new BigDecimal("0.8");
            BigDecimal result = operand1.subtract(operand2);
            System.out.println(result);
    
            BigDecimal op1 = new BigDecimal("0.1");
            BigDecimal op2 = new BigDecimal("0.1");
            BigDecimal op3 = new BigDecimal("0.1");
            BigDecimal result2 = new BigDecimal("0.3");
            if(op1.add(op2).add(op3).equals(result2)) {
                System.out.println("等于 0.3");
            }
            else {
                System.out.println("不等于 0.3");
            }
        }
    }
    

    基本类型打包

    public class IntegerDemo {
        public static void main(String[] args) {
            int data1 = 10;
            int data2 = 20;
    
            Integer wrapper1 = new Integer(data1);
            Integer wrapper2 = new Integer(data2);
    
            System.out.println(data1 / 3);
            System.out.println(wrapper1.doubleValue() / 3);
            System.out.println(wrapper1.compareTo(wrapper2));
        }
    }
    

    数组复制

    class Clothes {
        String color;
        char size;
        Clothes(String color, char size) {
            this.color = color;
            this.size = size;
        }
    }
    
    public class Copy {
        public static void main(String[] args) {
            Clothes[] c1 = {new Clothes("red", 'L'), new Clothes("blue", 'M')};
            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);    //浅层复制的结果
    
            Clothes[] c3 = {new Clothes("red", 'L'), new Clothes("blue", 'M')};
            Clothes[] c4 = new Clothes[c3.length];
            for(int i = 0; i < c3.length; i++) {
                Clothes c = new Clothes(c3[i].color, c3[i].size);
                c4[i] = c;
            }
            c3[0].color = "yellow";
            System.out.println(c4[0].color);    //深层复制的结果
        }
    }
    

    1到100

    public class OneTo100 {
        public static void main(String[] args) {
            StringBuilder oneTo100 = new StringBuilder();
            for (int i = 1; i < 100; i++) {
                oneTo100.append(i).append('+');
            }
            System.out.println(oneTo100.append(100).toString());
        }
    }
    

    储蓄

    import java.util.Scanner;
    
    class CashCard {
        private String number;
        private int balance;
        private int bonus;
    
        CashCard(String number, int balance, int bonus) {
            this.number = number;
            this.balance = balance;
            this.bonus = bonus;
        }
    
        void store(int money) {
            if(money > 0) {
                this.balance += money;
                if(money >= 1000) {
                    this.bonus++;
                }
            }
            else {
                System.out.println("储值是负的?你是来捣乱的吗?");
            }
        }
    
        void charge(int money) {
            if(money > 0) {
                if(money <= this.balance) {
                    this.balance -= money;
                }
                else {
                    System.out.println("钱不够啦!");
                }
            }
            else {
                System.out.println("扣负数?这不是让我储值吗?");
            }
        }
    
        int exchange(int bonus) {
            if(bonus > 0) {
                this.bonus -= bonus;
            }
            return this.bonus;
        }
    
        int getBalance() {
            return balance;
        }
    
        int getBonus() {
            return bonus;
        }
    
        String getNumber() {
            return number;
        }
    }
    
    public class CardApp {
        public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
    
            CashCard card1 = new CashCard("A001", 500, 0);
            card1.store(console.nextInt());
    
            CashCard card2 = new CashCard("A002", 300, 0);
            card2.store(console.nextInt());
    
            CashCard card3 = new CashCard("A003", 1000, 1);
            card3.store(console.nextInt());
    
            System.out.printf("明细 (%s, %d, %d)%n",
                    card1.getNumber(), card1.getBalance(), card1.getBonus());
            System.out.printf("明细 (%s, %d, %d)%n",
                    card2.getNumber(), card2.getBalance(), card2.getBonus());
            System.out.printf("明细 (%s, %d, %d)%n",
                    card3.getNumber(), card3.getBalance(), card3.getBonus());
        }
    }
    
    

    教材学习中的问题和解决过程

    不太理解为什么要进行基本类型打包,基本类型打包有什么作用?

    这周我将NetBaens换成了IDEA,
    对1到100那个程序的运行结果截图的时候因为结果太长,很难截图,感觉应该是有结果自动换行功能的,找了一下,发现了应该是自动换行按钮的东西

    还有显示代码行数的功能

    等等

    代码调试中的问题和解决过程

    都是在抄书,所以没问题。

    其他(感悟、思考等,可选)

    感觉本周的学习难度明显上升了,有很多新概念出现,例如对象、参考等概念感觉理解起来就很麻烦,很难把这些概念固定在印象里。

    学习进度条

    代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
    目标 1000行 16篇 300小时
    第一周 20/1000 1/16 8/300
    第二周 71/1000 2/16 17/300
    第三周 320/1000 3/16 30/300

    参考资料

  • 相关阅读:
    Effective Java 19 Use interfaces only to define types
    Effective Java 18 Prefer interfaces to abstract classes
    Effective Java 17 Design and document for inheritance or else prohibit it
    Effective Java 16 Favor composition over inheritance
    Effective Java 15 Minimize mutability
    Effective Java 14 In public classes, use accessor methods, not public fields
    Effective Java 13 Minimize the accessibility of classes and members
    Effective Java 12 Consider implementing Comparable
    sencha touch SortableList 的使用
    sencha touch dataview 中添加 button 等复杂布局并添加监听事件
  • 原文地址:https://www.cnblogs.com/20145120hxx/p/5298151.html
Copyright © 2011-2022 走看看