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 + “=” + ij + " ");
}
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方法中调用。
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;
}
}
请将消费者在商城购物这个场景抽象出类,并编写一个客户端类,实现“小明在欧尚买了一件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);
}
}