我是靠谱客的博主 寂寞盼望,这篇文章主要介绍java语言程序设计基础篇——一维数组(1),现在分享给大家,希望可以做个参考。

1.(计算数字的出现次数)编写程序,读取1到100之间的整数,然后计算每个数出现的次数。假定输入是以0结束的。

方法一:不是很常规,将整数直接读取到数组中。

因为样例输出时,是按照数字从小到大的顺序输出的,所以先将数组进行排序;

将第i个位置的数字,与i位置之后的数字依次进行比较,如果相等,则该数字的出现次数在1的基础上加1,紧接着将该位置的数字变成0,下次不会再访问;

循环输出count值

复制代码
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
import java.util.Arrays; import java.util.Scanner; public class Exercise6_3 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the integers between 1 and 100: "); int[] f = new int[101]; for(int i=0;i<f.length;i++){ f[i] = input.nextInt(); if(f[i] == 0) break; } Arrays.sort(f); int count; for(int i=0;i<f.length;i++){ count=1; if(f[i]==0||f[i]>100||f[i]<0) continue; for(int j=i+1;j<f.length;j++){ if(f[j]==f[i]){ count++; f[j]=0; } } if(count > 1) System.out.println(f[i]+" occurs "+count+" times"); else System.out.println(f[i]+" occurs "+count+" time"); } } }


方法二:设置计数数组counts[],从第0~第99位置分别对应数字1~100出现的次数。最后只选择输出counts[i]不为0的元素值

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner; public class Exercise6_3 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter the integers between 1 and 100: "); int number = input.nextInt(); int[] counts = new int[101]; while(number!= 0){ if(number<=100 && number>=0) counts[number-1]++; number = input.nextInt(); } for (int i=0;i<100;i++){ if(counts[i]>0) System.out.println((i+1)+" occurs "+counts[i]+((counts[i] == 1)?" time":" times")); } } }

2.(分析成绩)编写一个程序,读入个数不确定的考试分数,并且判断有多少个分数是大于或等于平均分,多少个分数是低于平均分的。输入一个负数表示输入的结束。假设成绩的最高分为10分。

因为有输入一个负数结束输入的限制,所以依旧采用while循环依次输入考试分数,然后将考试分数在0-10之间包括0和10的数字依次存储进数组中,在存储过程中,记录元素的个数和元素的和,最终求出平均数;

将数组内的元素依次与平均数进行比较,大于等于平均数的,使count1加1,小于的使count2加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
28
29
30
31
32
import java.util.Scanner; public class Exercise6_4 { public static void main(String[] args){ Scanner input = new Scanner(System.in); double[] f = new double[10000001]; double sum=0; int count=0; System.out.print("Enter the goals between 0 and 10: "); double number = input.nextDouble(); while(number>=0){ if(number<=10){ f[count]=number; count++; sum+=number; } number = input.nextDouble(); } double ave = sum/count; System.out.println("The average of goals is "+ave); int count1=0; int count2=0; for(int i=0;i<count;i++){ if(f[i]>= ave) count1++; else count2++; } System.out.println("The number of goals higher than(or equal to) average is "+count1); System.out.println("The numbre of goals lower than average is "+count2); } }

方法2:因为题目中未明确读入分数的个数,所以申请较大的数组会浪费空间资源,申请较小的数组会发生越界,所以较好的办法应该是使用ArrayList建立动态数组,根据需要分配空间。

复制代码
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
import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class E6_4 { public static void main(String[] args) { // TODO 自动生成的方法存根 ArrayList list = new ArrayList(); Scanner input = new Scanner(System.in); int score = input.nextInt(); while (score >= 0) { list.add(score); score = input.nextInt(); } Integer[] array = (Integer[]) list.toArray(new Integer[list.size()]);//能正确运行 double scores = 0; for(int element:array){ scores += element; //1 for(元素类型t 元素变量x : 遍历对象obj){ //2 引用了x的java语句; } scores = scores / list.size(); int i = 0 , j = 0; for (int element:array) { if (element < scores) { i++; } else j++; } System.out.println("平均分: " + scores); System.out.println("低于平均分的: " + i); System.out.print("高于平均分的: " + j); } }

3.(打印不同的数)编写一个程序,读入10个数并且显示互不相同的数(即一个数出现多次,但仅显示一次)。提示,读入一个数,如果它是一个新数,则将它存储在数组中。如果该数已经在数组中,则忽略它。输入之后,数组包含的都是不同的数。

该题的关键在于通过循环判断输入的数字是否存在于数组中(关键在于count值的使用)

在判断过程中,如果出现数字和某一位置的元素相等,则立马结束循环,此时的j值肯定小于count值;

如果z直到循环结束都没有找到与之相等的元素,此时的j值肯定为count,则可以将该数字存进数组中,count值进行加1;

其实这种方法有些难想,比较常规的就是设置一个标志,如果出现数字与元素相等,则该标志变为false,显然,最后只需要观察标志位的值是true还是false就可以知道该数字需不需要存进去。

复制代码
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
import java.util.Scanner; public class Exercise6_5 { public static void main(String[] args){ Scanner input = new Scanner(System.in); int[] f = new int[10]; System.out.print("Enter ten numbers: "); int count=0; for(int i=1;i<10;i++){ int number = input.nextInt(); int j; for(j=0;j<count;j++){ if(number==f[j]){ break; } } if(j==count){ f[count]=number; count++; } } System.out.print("The distinct numbers are: "); for(int i=0;i<count;i++){ System.out.print(f[i]+" "); } } }


4.(修改程序清单4-14)程序清单4-14通过检验2,3,4,5,6,......,n/2是否是数n的因子来判断n是否是素数。如果找到一个因子,n就不是素数。判断n是否是素数的另一个更有效的方法是:检验小于等于根号下n的素数是否都能整除n。如果不能,则n就是素数,然后再检查它们是否是n的可能的因子。

下面是程序清单4-14的代码实现:

复制代码
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
public class PrimeNumber { public static void main(String[] args){ final int NUMBER_OF_PRIMES = 50; final int NUMBER_OF_PRIMES_PER_LINE = 10; int count = 0; int number = 2; System.out.println("The first 50 prime numbers are n"); while(count < NUMBER_OF_PRIMES){ boolean isPrime = true; for(int divisor = 2;divisor <= number/2;divisor++){ if(number % divisor == 0){ isPrime = false; break; } } if(isPrime){ count++; if(count % NUMBER_OF_PRIMES_PER_LINE == 0){ System.out.println(number); }else System.out.print(number + " "); } number++; } } }

改写的程序:按照红色部分的要求进行改写就可以,这个时候需要建一个存储素数的数组,用已经判断出是素数的数组元素去判断新的数是否是素数。

复制代码
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
public class Exercise6_6 { public static void main(String[] args){ final int NUM_OF_PRIMES = 50; int count=0; int number=2; int[] primeNum = new int[NUM_OF_PRIMES]; System.out.println("The first 50 prime numbers are n"); while(count<NUM_OF_PRIMES){ boolean isPrime = true; for(int i=0;i<count&&primeNum[i]<=Math.sqrt(number);i++){ if(number%primeNum[i]==0){ isPrime=false; break; } } if(isPrime){ primeNum[count]=number; count++; } number++; } for(int i=0;i<NUM_OF_PRIMES;i++){ if(i%10==9) System.out.println(primeNum[i]); else System.out.print(primeNum[i]+" "); } } }

最后

以上就是寂寞盼望最近收集整理的关于java语言程序设计基础篇——一维数组(1)的全部内容,更多相关java语言程序设计基础篇——一维数组(1)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部