第一题
1.使用二重循环打印如图-1所示的图形。
*
***
*****
*******
*****
***
*
import java.util.*;
public class chenfew {
public static void main(String[] args) {
System.out.print("请输入 行数:");
Scanner input = new Scanner(System.in);
int hang = input.nextInt();
for (int i = hang ; i >= 1; i--) {
for (int k = 1; k <= hang - i; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= hang-1; i++) {
for (int p = 1; p <= hang - i-1; p++) {
System.out.print("*");
}
System.out.println();
}
}
}
第二题
.定义两个数组,首先把两个数组合并成一个新数组,然后把新数组中的所有元素逆序排列,需要实现的效果如图-2所示。
public class suzu {
public static void main(String[] args) {
int[] a1 = new int[] { 10, 20, 30 };
int[] a2 = new int[] { 40, 50, 60 };
int[] a12 = new int[a1.length + a2.length];
for (int i = 0; i < a12.length; i++) {
if (i < a1.length) {
a12[i] = a1[i];
} else {
a12[i] = a2[i - a1.length];
}
}
System.out.print("第一个数组:");
for (int i = 0; i < a1.length; i++) {
System.out.print(+a1[i] + " ");
}
System.out.println();
System.out.print("第二个数组:");
for (int i = 0; i < a1.length; i++) {
System.out.print(+a2[i] + " ");
}
System.out.println();
System.out.print("两个数组合并后:");
for (int i = 0; i < a12.length; i++) {
System.out.print(+a12[i] + " ");
}
System.out.println();
System.out.print("逆序");
for (int i = a12.length - 1; i >= 0; i--) {
System.out.print(+a12[i] + " ");
}
}
}
第三题
使用面向对象思想,输出员工信息并计算员工的工资。实现过程为:定义一个部门(Department)类,该类有部门编号(Id)、部门名称(Name)属性; 再定义一个员工(Employee)类,该类的属性有员工编号(Id)、姓名(Name)、底薪(Salary),所在部门(Department,该属性的值为Department类的对象)。
然后为员工类定义一个计算工资(countSalary)的方法,该方法的参数为本月销售额(sales),返回值为该员工的月工资。月工资的计算方法为:底薪+销售提成,销售提成的比例如下:小于10w为1%,10w至20w为2%,大于等于20w为3%。
在main()方法中,创建Department类和Employee类的对象、输入该员工的本月销售额后,计算并输出该员工的工资。
public class er {
String id;
String name;
}
员工类
import java.util.*;
public class ti3 {
String id;
String name;
int dixin;
double qian;
public double show() {
Scanner input = new Scanner(System.in);
System.out.println("请输入销售额:");
int shu = input.nextInt();
if (shu < 100000) {
qian = dixin + shu * 0.01;
} else if (shu >= 100000 && shu < 200000) {
qian = dixin + shu * 0.02;
} else {
qian = dixin + shu * 0.03;
}
return qian;
}
}
调试类
public class tiao {
public static void main(String[] a) {
er de = new er();
de.id = "0001";
de.name = "销售部";
System.out.println("部门信 息 " + "部门标号 " + de.id + " 部门名称 "
+ de.name);
ti3 fe = new ti3();
fe.id = "0003";
fe.name = "郭德纲";
fe.dixin = 2000;
System.out.println("部门编号: " + fe.id + " 员工姓名: " + fe.name
+ " 底薪: " + fe.dixin + " 所在部门: " + de.name);
fe.show();
double fan=fe.qian;
System.out.println(fe.name+"的工资是"+fan);
}
}