学习内容:
1、编写程序,完成用户信息录入。要求录入姓名,性别,年龄,家庭住址这四项信息
import java.util.Scanner; public class Person { String name; String sex; int age; String home; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入用户人数:"); int n = in.nextInt(); Person per[] = new Person[n]; for (int i = 0; i < per.length; i++) { per[i] = new Person(); System.out.println("请输入第" + (i + 1) + "个用户信息"); System.out.print("姓名:"); per[i].name = in.next(); System.out.print("性别:"); per[i].sex = in.next(); System.out.print("年龄:"); per[i].age = in.nextInt(); System.out.print("家庭住址:"); per[i].home = in.next(); } System.out.println(); System.out.println("输出用户信息:"); for (int i = 0; i < per.length; i++) { System.out.printf("%-10s", per[i].name); System.out.print("\t" + per[i].sex + "\t" + per[i].age + "\t"); System.out.printf("%-10s\n", per[i].home); } } }
2、编写求两个数之和的函数,要求使用函数重载,能求整数、长整型、浮点、双精度等数的和
package helloworld; import java.util.Scanner; public class study{ public static int sum(int a,int b) { return a+b; } public static long sum(long a,long b) { return a+b; } public static float sum(float a,float b) { return a+b; } public static double sum(double a,double b) { return a+b; } public static void main(String[] args){ Scanner in=new Scanner(System.in); System.out.println("请输入两个int型数:"); int a1,a2; a1=in.nextInt(); a2=in.nextInt(); System.out.println("两数之和:"+sum(a1,a2)); System.out.println("请输入两个long型数:"); long b1,b2; b1=in.nextLong(); b2=in.nextLong(); System.out.println("两数之和:"+sum(b1,b2)); System.out.println("请输入两个float型数:"); float c1,c2; c1=in.nextFloat(); c2=in.nextFloat(); System.out.println("两数之和:"+sum(c1,c2)); System.out.println("请输入两个double型数:"); double d1,d2; d1=in.nextDouble(); d2=in.nextDouble(); System.out.println("两数之和:"+sum(d1,d2)); } }