1.编写程序,完成用户信息录入。要求录入姓名,性别,年龄,家庭住址这四项信息。
1 package com.wuzy; 2 public class Person { 3 private String name; 4 private char sex; 5 private int age; 6 String home; 7 public static void main(String[] args) { 8 Person p1=new Person(); 9 Person p2=new Person(); 10 p1.name="小明"; 11 p1.sex='m'; 12 p1.age=20; 13 p1.home="翻斗花园201室"; 14 p2.name="花花"; 15 p2.sex='f'; 16 p2.age=18; 17 p2.home="龙兴小区501室"; 18 System.out.println("我是"+p1.name+",性别"+p1.sex+",年龄"+p1.age+",家住"+p1.home); 19 System.out.println("我是"+p2.name+",性别"+p2.sex+",年龄"+p2.age+",家住"+p2.home); 20 } 21 }
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 }