我只做到了21题,有点乏了,未完待续.........(以后更新)
(7.2~7.5节)
*7.1(指定等级)
import java.util.Scanner;
public class S1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numbers=input.nextInt();
System.out.print("Enter "+numbers+" scores: ");
int[] scores=new int[numbers];
for(int i=0; i=best-10)
System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[0]);
else if(scores[i]>=best-20)
System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[1]);
else if(scores[i]>=best-30)
System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[2]);
else if(scores[i]>=best-40)
System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[3]);
else
System.out.println("Student "+i+" score is "+scores[i]+" and grade is "+ch[4]);
}
input.close();
}
}
7.2(倒置输入的数)
import java.util.Scanner;
public class S2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入10个整数: ");
int[] numbers=new int[10];
for(int i=0; i=0; i--) {
System.out.print(" "+numbers[i]);
}
input.close();
}
}
**7.3(计算数字出现的次数)
参考大神博客会有不同解法:
(https://blog.csdn.net/weixin_46215617/article/details/105097958?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
(https://blog.csdn.net/LEE_FIGHTING_JINGYU/article/details/80009407?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
在这里我用count[1]存储数字1的次数,用count[2]存储数字2的次数,......,以此类推。
import java.util.Scanner;
public class S3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter tne integers between 1 and 100: ");
int number=input.nextInt();
int[] counts=new int[102];
int i=1;
while(number!=0) {
if(number>=1 && number<=100) {
counts[number]++;
}
number=input.nextInt();
}
for(int j=1; j<=100; j++) {
if(counts[j]>0) {
System.out.println(j+" occurs "+counts[j]+(counts[j]>1 ? " times." :" time."));
}
}
input.close();
}
}
7.4(分析成绩)
某大神参考代码:
https://blog.csdn.net/lee_fighting_jingyu/article/details/80009407
我改变了一下,数组长度我让用户自己输入,以确定存储分数的数组长度。
import java.util.Scanner;
public class S4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入你将要输入几个考试分数: ");
int n=input.nextInt();
System.out.print("请输入考试分数(以负数结束): ");
int[] scores = new int[n];
int i=0;
int count=0;
int sum=0;
int number=input.nextInt();
while(number>=0) {
if(number>=0 && number<=100) {
count++;
scores[i]=number;
sum += scores[i];
i++;
}
else {
System.out.println("Invalid input!");
}
number=input.nextInt();
}
double average= sum / count;
input.close();
int low=0;
int height=0;
for(int j=0; j=average) {
height++;
}
else {
low++;
}
}
System.out.println("平均数是: "+average);
System.out.println("大于平均数的分数有: "+height);
System.out.println("小于平均数的分数有: "+low);
}
}
**7.5(打印不同的数)
我不会,我参考了一下大神的答案:
https://blog.csdn.net/lee_fighting_jingyu/article/details/80021289
https://blog.csdn.net/lee_fighting_jingyu/article/details/80009407
*7.6(修改程序清单 5-15)
略,读不懂题目,有大神会的请给我留言,谢谢┭┮﹏┭┮(快哭了)
*7.7(统计一位数的个数)
public class S7 {
public static void main(String[] args) {
int[] numbers=new int[100];
int[] counts=new int[10];
for(int i=0; i<100; i++) {
numbers[i]=(int) (Math.random()*10);
counts[numbers[i]]++;
}
for(int i=0; i<10; i++) {
System.out.println(i+" 出现的次数为: "+counts[numbers[i]]+" 次.");
}
}
}
7.8(求数组的平均值)
import java.util.Scanner;
public class S8 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入10个doublex型的值:");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
System.out.println("平均数为: "+average(n));
}
public static int average(int[] array) {
int count=0;
int sum=0;
for(int i=0; i
7.9(找出最小元素)
import java.util.Scanner;
public class S9 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter ten numbers:");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
System.out.println("The minimum number is:"+min(n));
}
public static double min(double[] array) {
double min=array[0];
for(int i=1; i
7.10(找出最小元素的下标)
import java.util.Scanner;
public class S10 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter ten numbers:");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
System.out.println("The minimum's index is:"+indexOfSmallestElement(n));
}
public static int indexOfSmallestElement(double[] array) {
int indexOfSmall=0;
double min=array[0];
if(array.length>1) {
for(int i=1; i
*7.11(统计学方面:计算标准差)
import java.util.Scanner;
public class S11 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter ten numbers:");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
input.close();
System.out.printf("The mean is: %4.2f \n",mean(n));
System.out.printf("The standard deviation is: %7.5f",deviation(n));
}
//计算平均值
public static double mean(double[] x) {
double mean=0;
double sum=0;
for(int i=0; i
*7.12(倒置数组)
import java.util.Scanner;
public class S12 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入10个整数: ");
int[] numbers=new int[10];
for(int i=0; i=0 && j
7.9节
*7.13(随机数选择器)
首先题目要多读几遍:(编写一个方法,返回1-54之间的随机数,不包括传递到参数中的numbers)
意思是说:产生的这个随机数呀,与numbers数组里的元素不同.
public class S13 {
public static void main(String[] args) {
//数组1:getRandom(34, 3, 54, 1, 28, 23, 39, 4 );
//匿名数组 2:getRandom(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
System.out.println("产生与数组 1 里元素各不相同的随机数是:"+getRandom(34, 3, 54, 1, 28, 23, 39, 4 ));
System.out.println("产生与数组 2 里元素各不相同的随机数是:"+getRandom(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
}
public static int getRandom(int... numbers) {
int random=(int) (1+Math.random()*54);
for(int i=0; i
7.14(计算gcd)
求最大公约数的方法:(可以参考第五章的编程练习题5.14)
1.首先找到5个数中的最小值 d 。
2.依次检验 d, d-1, d-2,..., 2, 1.是否是这5个数的公约数。
3.当然,第一个满足条件的 d 就是最大公约数。
import java.util.Scanner;
public class S114 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入5个数: ");
int[] n=new int[5];
for(int i=0; i<5; i++) {
n[i]=input.nextInt();
}
System.out.println("该数组的最大公约数为: "+gcd(n));
}
public static int gcd(int... numbers) {
int d=numbers[0];//d为 5个数中的最小值
for(int i=1; i<5; i++) {
if(numbers[i]=1 && gcd==-1) {
if(numbers[0]%d==0 && numbers[1]%d==0
&& numbers[2]%d==0 && numbers[3]%d==0
&& numbers[4]%d==0) {
gcd=d;
}
d--;
}
return gcd;
}
}
7.15(消除重复)
我还是不太懂,还是看大神连接吧:(别人的参考答案)
https://blog.csdn.net/qq_36653524/article/details/93508892
https://blog.csdn.net/weixin_38750084/article/details/84196256
7.16(执行时间)
public class S16 {
public static void main(String[] args) {
int key=(int) (Math.random()*100000);
int[] list=new int[100000];
for(int i=0; i= low) {
int mid=(low+height)/2;
if(key
**7.17(对学生排序)
个人感觉应该用面向对象的方法来做比较方便,等我学会了以后再回头来写这题吧,嘻嘻,我先留着
以下是我未完成的代码:(我不知道成绩和名字怎么连接,目前,待我学了对象和类再来做)
import java.util.Scanner;
public class S17 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入学生的个数: ");
int n=input.nextInt();
String[] name=new String[n];
int[] scores=new int[n];
for(int i=0; i=0; i--) {
System.out.println(scores[i]+": "+name[i]);
}
}
}
**7.18(冒泡排序)
import java.util.Scanner;
public class S18 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入10个数: ");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
bubbleSort(n);
input.close();
}
public static void bubbleSort(double[] n) {
for(int i=0; i=n[j+1]) {
double temp=n[j];
n[j]=n[j+1];
n[j+1]=temp;
}
}
}
System.out.println("冒泡排序:");
for(int k=0; k
**7.19(是否排好序了?)
import java.util.Scanner;
public class S19 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter list: ");
int number=input.nextInt();
int[] n=new int[number];
for(int i=0; i list[i]) {
return false;
}
}
return true;
}
}
*7.20(修改选择排序)
import java.util.Scanner;
public class S20 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入10个数: ");
double[] n=new double[10];
for(int i=0; i<10; i++) {
n[i]=input.nextDouble();
}
selectionSort(n);
}
public static void selectionSort(double[] list) {
for(int i=0;i
***7.21(游戏:豆机)
不知道,略