1,实现在控制台输出九九乘法表。
package test1;
public class test1{
public static void main(String[] args) {
multiplication();
}
public static void multiplication(){
for(int i = 0; i<= 9; i++){
for(int j =1;j<= i; j++){
System.out.print(i+ " * " + j + " = " + i * j + " ");
}
System.out.println();
}
}
}
2,定义方法sum,要求实现两个数之和的运算,要求在main方法中调用。
package com.test;
public class test2 {
//定义相加的方法
public static int sum(int i, int j) {
return i + j;
}
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);
}
}
5,借助比较器,使用ltem具备按照价格从高到低排序;初始化10个ltem,并且Collections sort进行排序,查看排序结果。
public class Test5 {
public static void main(String[] args) {
//创建一个items的集合
List<Item> items = new ArrayList<>();
//创建Random的对象
Random r = new Random();
//用for循环创建10个物品,并分别添加到items集合中去
for (int i = 0; i < 10; i++) {
Item item = new Item("item" + i, r.nextInt(100));
items.add(item);
}
//创建比较器
Comparator<Item> aa = new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
if (o1.getPrice() < o2.getPrice()) {
return 1;
} else {
return -1;
}
}
};
//调用排序方法
Collections.sort(items, aa);
//打印出集合元素
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i));
}
}
}