1 编写一个Java应用程序,从键盘读取用户输入两个字符串,
并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。
答案版本1:
import java.util.Scanner;
public class Sum {
public static String sum(String s1, String s2) {
return s1+s2;
}
public static int sum(int i1, int i2) {
return i1+i2;
}
public static double sum(double d1, double d2) {
return d1+d2;
}
public static void main(String[] args) {
String s1 = null;
String s2 = null;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符串:");
s1 = sc.next();
System.out.println("请输入第二个字符串:");
s2 = sc.next();
System.out.println("两个字符串的拼接为:");
System.out.println(s1+s2);
System.out.println();
int i1 = 0;
int i2 = 0;
System.out.println("请输入第一个整数:");
i1 = sc.nextInt();
System.out.println("请输入第二个整数:");
i2 = sc.nextInt();
System.out.println("两个整数的和为:");
System.out.println(i1+i2);
System.out.println();
double d1 = 0;
double d2 = 0;
System.out.println("请输入第一个浮点数:");
d1 = sc.nextDouble();
System.out.println("请输入第二个浮点数:");
d2 = sc.nextDouble();
System.out.println("两个浮点数的和为:");
System.out.println(d1+d2);
System.out.println();
}
}
结果:
2 编写一个Java程序将长度分别为10的数组内容以相反的顺序显示出来。
// 10:
public class HW2 {
public static void main(String[] args) {
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
System.out.println("正序显示:");
for(int i = 0; i<a.length; ++i) {
System.out.print(i+" ");
}
System.out.println();
System.out.println("逆序显示:");
for(int i = a.length-1; i>=0; --i) {
System.out.print(i+" ");
}
}
}
拓展:
Java实现将长度分别为10和11的数组反序(比如讲[1,2,3,4]变为[4,3,2,1])
// 10:
public class HW2_2 {
public static void swap(int[] a, int b, int c) {
int t = a[b];
a[b] = a[c];
a[c] = t;
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7,8,9,10};
for(int i=0; i<a.length/2; ++i) {
swap(a, i, a.length-1-i);
}
for(int i:a) {
System.out.println(i);
}
}
}
// 11:
public class HW2_2 {
public static void swap(int[] a, int b, int c) {
int t = a[b];
a[b] = a[c];
a[c] = t;
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7,8,9,10,11};
for(int i=0; i<a.length/2; ++i) {
swap(a, i, a.length-1-i);
}
for(int i:a) {
System.out.println(i);
}
}
}
可见 反转数组 奇数个和偶数个的代码一样
3 定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
(4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息:
计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
先来个傻瓜解答:
public class Student {
private String sNO;
private String sName;
private String sSex;
private int sAge;
private double sJava;
public Student(String sNO, String sName, String sSex, int sAge, double sJava) {
super();
this.sNO = sNO;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public String getsNO() {
return sNO;
}
public void setsNO(String sNO) {
this.sNO = sNO;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsSex() {
return sSex;
}
public void setsSex(String sSex) {
this.sSex = sSex;
}
public int getsAge() {
return sAge;
}
public void setsAge(int sAge) {
this.sAge = sAge;
}
public double getsJava() {
return sJava;
}
public void setsJava(double sJava) {
this.sJava = sJava;
}
public static double avg(Student s1, Student s2, Student s3, Student s4, Student s5) {
double avg = (s1.getsJava() + s2.getsJava() + s3.getsJava() + s4.getsJava() + s5.getsJava()) / 5;
return avg;
}
public static double max(Student s1, Student s2, Student s3, Student s4, Student s5) {
double max = s1.getsJava();
if(s2.getsJava() > max) max = s2.getsJava();
if(s3.getsJava() > max) max = s3.getsJava();
if(s4.getsJava() > max) max = s4.getsJava();
if(s5.getsJava() > max) max = s5.getsJava();
return max;
}
public static double min(Student s1, Student s2, Student s3, Student s4, Student s5) {
double min = s1.getsJava();
if(s2.getsJava() < min) min = s2.getsJava();
if(s3.getsJava() < min) min = s3.getsJava();
if(s4.getsJava() < min) min = s4.getsJava();
if(s5.getsJava() < min) min = s5.getsJava();
return min;
}
public static void main(String[] args) {
Student s1 = new Student("01", "1号学生", "男", 21, 93);
Student s2 = new Student("02", "2号学生", "男", 22, 94);
Student s3 = new Student("03", "3号学生", "女", 23, 95);
Student s4 = new Student("04", "4号学生", "女", 24, 96);
Student s5 = new Student("05", "5号学生", "男", 25, 97);
System.out.println("5个学生的成绩平均值为: " + avg(s1,s2,s3,s4,s5));
System.out.println("5个学生的成绩最大值为: " + max(s1,s2,s3,s4,s5));
System.out.println("5个学生的成绩最小值为: " + min(s1,s2,s3,s4,s5));
}
}
输出效果:
4 计算一个整数的阶乘:
import java.util.Scanner;
public class Factorial {
public static long fact(long n) {
long res;
if(n < 0) res = -1;
else if(n==0 || n == 1) res = 1;
else res = fact(n-1)*n;
return res;
}
public static void main(String[] args) {
System.out.println("您想求谁的阶乘? \n请输入:");
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
System.out.println(n + " 的阶乘是: " + fact(n));
}
}