Day 1 实验课 Java的环境配置与Java数组

面向对象的程序设计语言
 • 特点:
 - 简单性
 简化c++ 内存模型等
 - 面向对象
 - 安全性

c++与Java的引用不是一回事
javac(编译器);Java(解释器)
 
 
 
 
 
 
数组越界?
 

Content 3 Java程序的数组

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

 1
 1 package hello;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Hellojava {
 6     public static void main(String[] args) {
 7         
 8         Scanner in =  new Scanner(System.in);
 9 
10         int[] a = new int [100];
11         int x;
12         x = in.nextInt();
13         while(x!=0)
14         {
15             a[x-1]++;
16             x=in.nextInt();
17         }
18         for(int i=0;i<a.length;i++)
19         {
20             if(a[i]!=0)
21                 System.out.print(i+1+" "+a[i]);
22         }
23         
24     }
25 }
View Code

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

2

 1 package hello;
 2 
 3 public class test1 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int[] a= {1,2,3,4,5};
 8         int[] b=a;
 9         b[0]++;
10         for(int i=0;i<a.length;i++)
11         {
12             System.out.println(a[i]);
13         }
14     }
15 
16 }

输出

2
2
3
4
5

3

 1 package hello;
 2 
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int[] a = {1,2,3,4,5,6,7,8,9,0};
 8         int[] b = new int [10];
 9         for(int i=0;i<a.length;i++)
10         {
11             b[i]=a[i];
12         }
13         a[0]=6;
14         for(int i=0;i<b.length;i++)
15         {
16             System.out.println(b[i]);
17         }
18     }
19 
20 }

输出

1
2
3
4
5
6
7
8
9
0

Content 4 可选题:多项式加法


 1 package hello;
 2 
 3 import java.util.Scanner;
 4 
 5 public class test3 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner in = new Scanner(System.in);
10         int[] a = new int [100];
11         int x,y,q,p;
12         x = in.nextInt();
13         y = in.nextInt();
14         q=x;
15         a[q]=y;
16         while(x>0)
17         {
18             a[x]=y;
19             x = in.nextInt();
20             y = in.nextInt();
21         }
22         a[0]=y;
23         x = in.nextInt();
24         y = in.nextInt();
25         if(x>q)
26         {
27             q=x;
28             a[q]=y;
29         }
30         while(x>0)
31         {
32             a[x]+=y;
33             x = in.nextInt();
34             y = in.nextInt();
35         }
36         a[0]+=y;
37         System.out.print(a[q]+"x"+q);
38         for(int i=q-1;i>0;i--)
39         {
40             if(a[i]!=0)
41             {
42                 if(a[i]>0)
43                     System.out.print("+");
44                 System.out.print(a[i]+"x");
45                 if(i!=1)
46                     System.out.print(i);
47             }
48         }
49         if(a[0]>0)
50         {
51             System.out.print("+"+a[0]);
52         }else if(a[0]<0){
53             System.out.print(a[0]);
54         }
55     }
56 
57 }
 
原文地址:https://www.cnblogs.com/lllllllm/p/13219760.html