1. Purpose of the experiment
1. Master the definition and usage of arrays;
2. Master the reference passing of arrays;
3. Master methods and method overloading .
2. Experimental hours
2 credit hours
3. Experiment type
Confirmatory experiment
4. Experimental requirements
1. Hardware
Each student is provided with a computer
2. Software
Windows XP operating system, JDK , eclipse , MySQL
3. Network
none
4. Tools
none
5. Experimental Theory and Preliminary Knowledge
1. The definition and usage of arrays;
2. The reference transfer of the array;
3. Method and method overloading.
6. Experimental content and results
1. Programming seeks 1 ! +2 ! +3 ! The value of + ...+20! is displayed, and it is required to use the method to complete.
public class fangfadiaoyong_jiechenghe {
public static double jicheng ( int a){
if (a == 1) {
return 1;
} else {
return a * jicheng(a - 1);
}
}
public static void main(String[] args) {
double num[] = new double[20];
double sum = 0;
for (int i = 0; i < 20; i++) {
num[i] = jicheng(i+1);
sum += num[i];
}
System.out.println(sum);
}
}
2. To output a table of multiplication formulas, two layers of loops are required.
public class chengfakoujue {
public static void main(String[] args) {
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++)
{
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}
}
3. Write a method that converts an integer into binary output.
public class erjinzhizhuanhuan {
public static void main(String[] args) {
bian(378);
}
public static void bian(int a){
if(a/2!=0){
bian(a/2);
System.out.print(a%2);
}
}
}
4. There are 5 people sitting together, how old is the 5th person? Answer: 2 years older than the 4th person , when asked how old the 4th person is, he is 2 years older than the 3rd person , when asked how old the 3rd person is, 2 years older than the 2nd person , and asked how old the 2nd person is When the first person is 2 years older than the first person , when the first person is asked how old he is, the first person says he is 8 years old, so how old is the fifth person? Use the code to calculate the age of the fifth person and print it out.
public class diwugerenage {
public static void main(String[] args) {
System.out.println(age(5));
}
public static int age(int m){
if(m==1){
return 8;//迭代和递归
}
return 2+age(m-1);
}
}
5. There are 30 numbers between 0 and 9 , count how many times the 10 numbers from 0 to 9 appear respectively.
import java.util.Random;
public class tonglishuzi {
public static void main(String[] args) {
Random r = new Random();//创建对象
int a[] = new int[30];
for (int i = 0; i < 10; i++) {
a[i] = r.nextInt(10);//获取一个0-9之间的随机数,存进数组a中
}
for (int i = 1; i < a.length; i++) {//对原数组a进行冒泡排序
for (int j = 0; j < a.length; j++) {
if (a[i] < a[j]) {
int x = a[i];
a[i] = a[j];
a[j] = x;
}
}
}
for (int i = 0; i < a.length; i++) {//统计每个数字出现的次数
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[i] == a[j]) {
count++;
}
}
if (i < 29 && a[i] != a[i + 1] && count != 0) {
System.out.println(a[i] + "出现次数为:" + count + "次");
}
if (i == 29) {
System.out.println(a[i] + "出现次数为:" + count + "次");
}
}
}
}
6. Add a number to the sorted array, insert the added number into the appropriate position of the array, and keep the original sorting method of the array.
public class charupaixushuzi {
public static void main(String[] args) {
int q[]={1,2,4,9};//往排好序的数组中插入数字
insert(3,q);
}
public static void insert(int a,int b[]){
int q1[]=new int[b.length+1];
int j=0;
int m=0;
for(int i=0;i<b.length;i++){
if(a<b[i]){
q1[j++]=a;
while(j<q1.length)
{
q1[j++]=b[i++];
}
m++;
}
else{
q1[j++]=b[i];
}
}
if(m==0){
q1[j]=a;
}
for(int i=0;i<q1.length; i++){
System.out.print(q1[i]+"\t");
}
}
}
A lot of recursive and iterative ideas are used in the experiment, um~, this kind of thinking is actually very wonderful, and it needs to be thought about more. Let me recommend an article by a big blogger. If you are interested, you can click to learn it.