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.编写求两个数之和的函数,要求使用函数重载,能求整数、长整型、浮点、双精度等数的和。
1 package helloworld; 2 import java.util.Scanner; 3 public class study{ 4 public static int sum(int a,int b) { 5 return a+b; 6 } 7 public static long sum(long a,long b) 8 { 9 return a+b; 10 } 11 public static float sum(float a,float b) 12 { 13 return a+b; 14 } 15 public static double sum(double a,double b) 16 { 17 return a+b; 18 } 19 public static void main(String[] args){ 20 Scanner in=new Scanner(System.in); 21 System.out.println("请输入两个int型数:"); 22 int a1,a2; 23 a1=in.nextInt(); 24 a2=in.nextInt(); 25 System.out.println("两数之和:"+sum(a1,a2)); 26 System.out.println("请输入两个long型数:"); 27 long b1,b2; 28 b1=in.nextLong(); 29 b2=in.nextLong(); 30 System.out.println("两数之和:"+sum(b1,b2)); 31 System.out.println("请输入两个float型数:"); 32 float c1,c2; 33 c1=in.nextFloat(); 34 c2=in.nextFloat(); 35 System.out.println("两数之和:"+sum(c1,c2)); 36 System.out.println("请输入两个double型数:"); 37 double d1,d2; 38 d1=in.nextDouble(); 39 d2=in.nextDouble(); 40 System.out.println("两数之和:"+sum(d1,d2)); 41 } 42 }