JAVA实训题

第1关:随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package step1;
public class MaxMin{
public static void main(String[] args){
double[] arr = new double[100];
int s=0; //定义 统计大于50的整数个数 变量
double max=0,min=100; //定义 最大值 最小值 变量
for(int i=0;i<100;i++){
double d=100*Math.random();//获取随机数
arr[i] = d;
/********* Begin *********/

if(arr[i]>max) {
max = Math.floor(arr[i]);
}
else if(arr[i]<min) {
min = Math.floor(arr[i]);
}
if(arr[i]>50) {
s ++;
}


/********* end *********/
}
judgeRadom.cpm(arr,max,min,s); //这个方法会判断你的计算是否正确
}
}

编写程序,利用数组计算Fibonacci数列的前20个元素的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package step2;
public class Fibonacci{
public static void main(String args[]){
/*********begin********/
int arr[] = new int[20];
arr[0] = 1;
arr[1] = 1;
int sum = 2;
for(int i=2; i<arr.length;i++){
arr[i] = arr[i-1] + arr[i-2];
sum = sum + arr[i];

}
System.out.printf("前20个元素的和为" + sum);




/********end********/
}
}

使用递归计算1+2+…+n之和。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package step3;
import java.util.Scanner;
public class Sum{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int result = recursionSum(num);
System.out.println(result);
}

public static int recursionSum(int mum){
/*********begin*********/
if(mum==1) {
return 1;
}else {
return mum+recursionSum(mum-1);
}


/*********end*********/
}
}

类的定义 与 对象的创建
设计一个表示学生的类,该类具有表示姓名的属性name和表示年龄的属性age,同时还具有表示说话行为的方法speak(),用于输出学生的姓名和年龄。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package task01;
import java.util.Scanner;
/*
* 说明:根据TODO提示信息,在横线处编写对应功能代码,
* 编写代码时,注意要删除对应的横线。
*/
// TODO 在此定义学生类名
class Student{
// TODO 在此定义2个成员变量
String name;
int age;
void speak() {
System.out.println("我的姓名:"+name+",年龄:"+age);
}
}
public class Task01
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
// TODO 在此定义一个学生对象
Student stu = new Student();
// TODO 在此给学生对象姓名赋值
stu.name = scan.next();
// TODO 在此给学生对象年龄赋值
stu.age = scan.nextInt();
// TODO 在此调用学生对象 speak() 方法
stu.speak();
}
}

本实训将创建三个学生对象,它们的引用变量分别是s1、s2和s3,首先分别使用s1和s2引用为name和age赋值,然后调用speak()方法,最后将s2变量赋值给s3,s3也调用speak()方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package task02;

import java.util.Scanner;

/*
* 说明:根据提示,补充完整 begin 和 end 之间的代码。
* (其他代码可以不用改)
*/

/**************** begin *****************/
// 在此定义学生类名
class Student
/**************** end *****************/
{
/**************** begin *****************/
// 在此定义2个成员变量:姓名name和年龄age
String name;
int age;
/**************** end *****************/

void speak() {
System.out.println("我的名字是" + name + ",今年" + age + "岁");
}
}

public class Task02{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

/**************** begin *****************/
/*
* 创建Student类的第一个对象,其引用变量为s1,
* 使用s1分别为name和age赋值,值为运行时接收从键盘输入
* 然后调用speak()方法。
*/
Student s1 = new Student();
s1.name = scan.next();
s1.age = scan.nextInt();
s1.speak();
/**************** end *****************/


/**************** begin *****************/
/*
* 创建Student类的第二个对象,其引用变量为s2,
* 使用s2分别为name和age赋值,值为运行时接收从键盘输入,
* 然后调用speak()方法。
*/
Student s2 = new Student();
s2.name = scan.next();
s2.age = scan.nextInt();
s2.speak();
/**************** end *****************/


/**************** begin *****************/
/*
* 创建Student类的第三个对象,其引用变量为s3,
* 将s2的值赋给s3,
* 然后使用s3调用speak()方法。
*/
Student s3 = new Student();
s3 = s2;
s3.speak();


/**************** end *****************/
}
}

对类进行封装,防止外界对类中的成员变量随意访问。
为了掌握类的封装,本练习将使用private关键字对学生类的成员变量name和age进行私有化,同时分别提供一个setName(String n)和setAge(int a)方法用于外界的访问,其中setAge(int a)中需要对age进行判断。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package task03;

import java.util.Scanner;

/*
* 说明:根据提示,补充完整 begin 和 end 之间的代码。
* (其他代码可以不用改)
*/

/**************** begin *****************/
// 在此定义学生类名
class Student
/**************** end *****************/
{
/**************** begin *****************/
// 将名字name和年龄age属性定义为私有的
private String name;
private int age;
/**************** end *****************/

/**************** begin *****************/
/*
* 定义4个公有的方法:getName()、setName(String n)、
* getAge()和setAge(int a),
* 用于对外访问name和age。
*
* 在setAge(int a)方法中对传入的参数进行检查,
* 如果输入值为负数,则打印出“设置的年龄不合法”,
* 如果不为负数,才将其设置为age属性的值。
*/
public String getName(){
return name;

}
public int getAge(){
return age;


}
public void setAge(int age){


if(age<0){
System.out.println("设置的年龄不合法");

}
else{
this.age = age;

}

}
public void setName(String name){
this.name = name;

}
/**************** end *****************/

void speak() {
System.out.println("我的名字是" + name + ",今年" + age + "岁");
}
}

public class Task03{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name1 = scan.next();
int age1 = scan.nextInt();

/**************** begin *****************/
/*
* 创建Student类的实例对象,
* 通过调用对象的setName(String n)和setAge(int a)方法
* 来设置实例对象的name属性和age属性值,
* 方法的参数值为运行时接收从键盘输入,
* 并调用speak()方法。
*/
Student stu = new Student();
stu.setName(name1);
stu.setAge(age1);
stu.speak();

/**************** end *****************/
}
}

第二章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package chapter2.step7;
import java.util.Scanner; //1.导入Scanner
public class HelloWorld{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //2.声明扫描仪
System.out.println("请录入嫦娥个人信息:");
System.out.println("请输入姓名:");
String name = input.next();
System.out.println("请输入年龄:");
int age = input.nextInt();
System.out.println("请输入性别:");
String sex = input.next();
System.out.println("请输入体重:");
float weight = input.nextFloat();
System.out.println("请输入地址:");
String address = input.next();
System.out.println("请输入是否已婚:");
String isMarried = input.next();
System.out.println("信息如下:");
System.out.println("\t姓名:" + name);
System.out.println("\t年龄:" + age + "岁");
System.out.println("\t性别:" + sex);
System.out.println("\t体重:" + weight + "kg");
System.out.println("\t地址:" + address);
System.out.println("\t婚否:" + isMarried);
}
}