2018.12.13学习面向对象以来写的第一段完整代码

这是我学习面向对象以来写的第一段完整代码,比较繁琐,但总算以面向对象为思想用代码实现了基本功能。
题:P135(二)
我的代码:

package myObject;

import java.util.Scanner;
public class BuyBook {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入书名:");
		//创建对象
		BuyBook a = new BuyBook();
		
		//成员变量
		String b[] = {"《Java从入门到精通》","《Java Web从入门到精通》"};
		//输出书名
		for(int i=0;i<b.length;i++) {
			System.out.println("书名为:"+b[i]);
		}
		double c[] = {59.8,69.8};
		//输出价格
		for(int j=0;j<c.length;j++) {
				System.out.println("价格分别为:"+c[j]);
			}
		a.account(c);
		//输出打完折后的价格
		System.out.println("打折后的价格");
		for(int h=0;h<c.length;h++) {
			System.out.println(c[h]);
		}
		sc.close();
	}
	public void account(double i[]) {
		 for(int j=0;j<i.length;j++) {
			 i[j]=i[j]*0.5;
		 }
	}
}

标准答案:

package myObject;

public class BuyBooks {
	public static void main(String[] args) {
		String[] books = {"《Java从入门到精通(第4版)》", "《Java Web从入门到精通(第2版)》"};
		String author = "明日科技";
		double[] prices = {59.8, 69.8};
		double totalPrice = 0;
		System.out.println("----------------------------图书信息----------------------------");
		System.out.println("书名\t\t\t\t\t作者\t\t售价");
		System.out.println("---------------------------------------------------------------");
		for (int i = 0; i < prices.length; i++) {
			System.out.println(books[i] + "\t\t" + author + "\t\t" + prices[i]);
			totalPrice += prices[i];
		}
		System.out.println("---------------------------------------------------------------");
		System.out.println("合计\t\t\t\t\t\t\t" + totalPrice);
		BuyBooks buyBooks = new BuyBooks();
		System.out.println("折后价\t\t\t\t\t\t\t" + buyBooks.discount(totalPrice));
	}
	
	public double discount(double totalPrice) {
		totalPrice = totalPrice * 0.5;
		return totalPrice;
	}
}

总结:①学面向对象以来第一个真正使用面向思想的程序
②注意\t的使用方法
③学习答案中使用一个for循环同时遍历两个数组的方法


猜你喜欢

转载自blog.csdn.net/wxw2008wxw/article/details/84996650