2018-05-11 周测试题

1、实现在控制台输出九九乘法表。

public class Test {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("" + i + " * " + j + " = " + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}

2、定义方法sum,要求实现两个数之和的运算,要求在main方法中调用。

public class Test2 {

	public static int sum(int a, int b) {
		return a + b;
	}

	public static void main(String[] args) {
		int i = 10;
		int j = 20;

		System.out.println("" + i + " + " + j + " = " + Test2.sum(i, j) + "");
	}
}

3、请写一个方法打印数组的内容,实现遍历数组,要求在main方法中调用。

提示:在main方法中定义一个数组,然后将数组作为参数传给方法,在方法中打印结果"[a,b,c,....]"

public class Test3 {
	
	public static void main(String[] args) {
		String[] arr1 = {"a","b","c"};
		String[] arr2 = {};
		String[] arr3 = null;
		System.out.println(printArray(arr1));
		System.out.println(printArray(arr2));
		System.out.println(printArray(arr3));
		
	}
	
	public static String printArray(String[] arr) {
		// [a,b,c]
		String result = "";
		
		if (null == arr) {
			result = "数组为null";
		} else {
			if (arr.length == 0) {
				result = "数组长度为 0";
			} else {
				result = "[";
				
				for (int i = 0; i < arr.length; i++) {
					if (i == arr.length - 1) {
						result += arr[i];
					} else {
						result += arr[i] + ",";
					}
				}
				
				result += "]";
			}
		}
		
		return result;
	}
}

4、请将消费者在商城购物这个场景抽象出类,并编写一个客户端类,实现“小明在欧尚买了一件T恤”这样一个购物行为。

public class Customer {
	private String name;
	
	public Customer(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}
public class Market {
	private String name;
	
	public Market(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

public class Product {
	private String name;

	public Product(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}
public interface ShoppingServiceInter {
	void shopping(Customer cus, Market market, Product product);
}
public class ShoppingService implements ShoppingServiceInter {

	public void shopping(Customer cus, Market market, Product product) {
		System.out.println("" + cus.getName() + "在" + market.getName() + "买了件" + product.getName() + "");
	}
}
public class Client {
	public static void main(String[] args) {
		Customer customer = new Customer("小明");
		Market market = new Market("欧尚");
		Product product = new Product("T恤");
		
		ShoppingService service = new ShoppingService();
		service.shopping(customer, market, product);
	}
}

猜你喜欢

转载自blog.csdn.net/zhou_jiepeng/article/details/80288572
今日推荐