[Java程序设计基础] Day 1 实验课 Java的环境配置与Java数组

Content 1 Java开发工具与简单程序设计

实验目的
1、安装JDK
2、掌握Java程序组成、编译、运行
3、安装eclipse
4、使用eclipse开发Java程序(HelloWorld)

package class_one;

import java.util.Scanner;

public class HelloWorld {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("HelloWorld");
	}
}

Content 2 Java程序设计基础

实验目的:
1、掌握数据的基本类型
2、注意程序中数据类型的转换
3、数量使用运算符
实验内容:
1、将1000000*1000000分别按照int型、long型、double型、float型输出,观察不同类型的输出结果,分析其原因。

package class_one;

public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i = 1000000;
		long j = 1000000;
		double a = 1000000;
		float b = 1000000;
		System.out.println("int: "+i*i);
		System.out.println("long: "+j*j);
		System.out.println("double: "+a*a);
		System.out.println("float: "+b*b);
	}

}

注意:不能直接赋值1e12,因为会按int算完之后赋值

输出结果

int: -727379968
long: 1000000000000
double: 1.0E12
float: 1.0E12

Content 3 Java程序的数组

实验目的:
1、掌握并灵活运用一维数组,理解数组是容器
2、掌握数组的赋值与复制
实验内容:
1、任意输入1到100间的整数,以0结束,然后计算每个数出现的次数
2、定义两个数组a和b,并初始化,执行a=b后输出a和b
3、定义两个数组a,并初始化,复制数组a
1、

package class_one;

import java.util.Scanner;

public class test2_1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x;
		int[] count = new int[101];
		Scanner in = new Scanner(System.in);
		x = in.nextInt();
		while (x!=0) {
			count[x]++;
			x = in.nextInt();
		}
		for (int i=0; i<count.length; i++)
			System.out.println(i+": "+count[i]);
	}

}

补充:鸭鸭发现,如果在print的时候直接print(i+a[i])的话会将二者相加后输出,所以要实现并排输出的话要使用print(i+""+a[i])

2、

package class_one;

public class test2_2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = {1,2,3,4,5};
		int[] b = {6,7,8,9,10};
		a = b;
		for (int i=0;i<a.length;i++)
			System.out.print(a[i]+" ");
		System.out.println("");
		for (int i=0;i<b.length;i++)
			System.out.print(b[i]+" ");
		System.out.println("");
	}

}

输出:

6 7 8 9 10 
6 7 8 9 10 

3、

package class_one;

public class test2_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = {1,2,3,4,5};
		int[] b = {6,7,8,9,10};
		for (int i=0; i<a.length; i++)
			b[i]=a[i];
		for (int i=0;i<a.length;i++)
			System.out.print(a[i]+" ");
		System.out.println("");
		for (int i=0;i<b.length;i++)
			System.out.print(b[i]+" ");
		System.out.println("");
	}
}

输出:

1 2 3 4 5 
1 2 3 4 5 

Content 4 可选题:多项式加法


package class_one;

import java.util.Scanner;

public class test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int x,y;
		int mx;
		int[] a = new int[101];
		x = in.nextInt();
		y = in.nextInt();
		mx = x;
		while (x!=0) {
			a[x]+=y;
			x = in.nextInt();
			y = in.nextInt();
		}
		a[0]+=y;
		x = in.nextInt();
		y = in.nextInt();
		if (x>mx) mx = x;
		while (x!=0) {
			a[x]+=y;
			x = in.nextInt();
			y = in.nextInt();
		}
		a[0]+=y;
		int flag=0;
		for (int i=mx; i>0; i--) {
			if (a[i]!=0) {
				if (flag==1)
					System.out.print("+");
				System.out.print(a[i]+"x");
				if (i>1)
					System.out.print(i);
				flag=1;
			}
		}
		if (a[0]>0) {
			if (flag==1)
				System.out.print("+");
			System.out.println(a[0]);
		}
	}

}
posted @ 2020-07-01 15:21  Mrha  阅读(194)  评论(0编辑  收藏  举报