作业:
1. 考试成绩已经保存在数组scores中,依次为 89,23,64,91,119,52,73,-23. 要求根据通过自定义方法来找出其中前三名,将数组成绩作为参数传入, 要求判断成绩的有效性(0-100),如果成绩无效,则忽略此成绩.
1. public class Scores {
2.
3. public static void main(String[] args) {
4. int [] scores={89,23,64,91,119,52,73,-23};
5. for(int i=0;i <scores.length-1;i++){
6. for(int j =0;j<scores.length-1-i;j++){
7. if(scores[j] <scores[j+1]){
8. int temp =scores[j];
9. scores[j]= scores[j+1];
10. scores[j+1]=temp;
11. }
12.
13. }
14. }
15. int count=0;
16. for(int i=0;i<scores.length;i++){
17. if(scores[i] >0 && scores[i] <100&& count<3){
18. count++;
19. System.out.print("前三名的成绩依次为:" + scores[i]);
20. }
21. else{
22. i++;
23. }
24.
25. }
26. }
27. }
2. 用数组来实现,定义并初始化一个(1--100)保存100个数的数组,从第一个元素开始,依次数(1,2,3 循环往复),每次数到到3的元素淘汰掉.当到达数组末尾的时候再从头开始,直到最后剩余一个元素,写出算法,并输出最后保留的元素所在的最初的位置.
1. public class XuHuan1 {
2. public static void main(String[] args){
3. int []array=new int[100]; //定义初始数组
4. int count=0;
5. int i=0;
6. int circul=0;
7. int outelement=0;
8. System.out.println("每次被淘汰的元素所在的位置是:");
9. while(count!=100){
10. if(array[i]!=-1){
11. if(circul==2){
12. count++;
13. array[i]=-1;
14. circul=0;
15. outelement=i;
16. System.out.print(outelement);
17. System.out.print(" ");
18. }else{
19. circul++;
20. }
21. }
22. i++;
23. if(i==100){
24. i=0;
25. }
26. }
27. System.out.println("");
28. System.out.println("最后保留的元素所在的最初的位置是:"+outelement);
29. }
30. }
3. 用数组来实现对于一个整形数组, 分别通过冒泡排序和 快速排序,实现对于任意一个数组进行由小到大的排列。
4. public class sort2 {
5. public static void main(String[] args) {
6. int [] array={5,1,7,2,8};
7. int length = array.length;
8.
9. QuickSortqs = new QuickSort();
10. qs.sort(array,0,length-1);
11.
12. System.out.println("快排的结果为:");
13. for(int i =0;i<array.length;i++){
14. System.out.print(array[i] + " ");
15. }
16. System.out.println(" " );
17.
18. sort2sc = new sort2();
19. sc.bubbleSort(array);//对数组array进行冒泡排序.
20. sc.printArray(array);//对排序过的数组进行打印输出.
21. }
22.
23. public void sort(int[] array,int left,int right){
24. if(left < right){
25. int p =partition(array,left,right);
26. sort(array,left,p-1);
27. sort(array,p+1,right);
28. }
29. }
30. public int partition(int []array,int left,int right){
31. int store = left;
32. for(int i=left;i<right;i++){
33. if(array[i] < array[right]){
34. swap(array,i,store);
35. store++;
36. }
37. }
38. swap(array,store,right);
39. return store;
40. }
41. public void swap(int [] array,int i,int j){
42. int temp;
43. temp= array[i];
44. array[i]=array[j];
45. array[j] =temp;
46. }
47. public static void bubbleSort(int []array){//此方法对数组按从小到大进行排序
48. for(int i=0;i <array.length-1;i++){
49. for(int j =0;j<array.length-1-i;j++){
50. if(array[j] >array[j+1]){
51. int temp =array[j];
52. array[j]= array[j+1];
53. array[j+1]=temp;
54. }
55. }
56. }
57. }
58.
59. public static void printArray(int []array){//此方法打印出冒泡排序的结果
60. System.out.println("冒泡排序的结果为:");
61. for(int i=0;i<array.length;i++){
62. if(i != array.length-1){
63. System.out.print(array[i]+",");
64. }
65. else{
66. System.out.println(array[i]);
67. }
68. }
69. }
70. }
4.判断101-200之间有多少个素数,并输出所有素数。
public class SuShu {
public static void main(String[] args) {
int count =0;
for(int i=101;i<200;i++){
for(int j=2;j<i;j++){
int m=i%j;
if(j==i-1){
++count;
System.out.print(i + " ");
}
else if(m==0){
break;
}
}
}
System.out.println( );
System.out.println("101到200之间素数有" + count + "个");
}
}
5.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
importjava.util.Scanner;
public class TongJi {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入一行字符串:");
String result=sc.nextLine();
int zimucount =0;
int shuzicount =0;
int blakcount =0;
int othercount =0;
for(int i=0;i<result.length();i++){
char an=result.charAt(i);
if(an >='0' && an<='9'){
shuzicount++;
}
else if((an >='a' && an<='z')||(an >='A' && an<='Z')){
zimucount++;
}
else if(an==' '){
blakcount++;
}
else{
othercount++;
}
}
System.out.println("英文字母的个数为zimucount:" + zimucount);
System.out.println("数字的个数为shuzicount:" + shuzicount);
System.out.println("空格的个数为blakcount:" + blakcount);
System.out.println("其他字符的个数为othercount" + othercount);
// TODO自动生成的方法存根
}
}
6.题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?
importjava.util.Scanner;
public class MoneyAward {
public static void main(String[] args) {
double sumall =0.0;
double money =0.0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入本月利润(万元)");
double profit=sc.nextDouble();
if(profit>100){
money=(profit-100)*0.01+40*0.015+20*0.03+20*0.05+10*0.075+10*0.1;
sumall += money;
}
if(profit >60 && profit<=100){
money=(profit-60)*0.015+20*0.03+20*0.05+10*0.075+10*0.1;
sumall += money;
}
if(profit >40 && profit<=60){
money=(profit-40)*0.03+20*0.05+10*0.075+10*0.1;
sumall+= money;
}
if(profit >20 && profit<=40){
money=(profit-20)*0.05+10*0.075+10*0.1;
sumall += money;
}
if(profit >10 && profit<=20){
money =(profit-10)*0.075+10*0.1;
sumall += money;
}
if(profit <=10){
money =profit*0.1;
sumall += money;
}
System.out.println("应发奖金总数为sumall(万元):" + sumall);
}
}
7.分别定义用户类,订单类,产品类,其中订单类至少要具有下订单的行为(入参为一个产品名称),
产品类中至少有一个成员变量为产品名称,至少有一个方法用来获取产品的名称。
用户类中持有一个产品类型的成员变量。
用户类中持有一个订单类型的成员变量。
在我们的用户类中定义main函数,当执行的时候,构造一个用户类的对象,
并且通过手段可以拿到产品成员变量中的产品名称,并且调用成员变量订单的对象,进行下单。
public class Users {
order an = new order();//创建order类的对象
production in = new production();//创建production类的对象
public static void main(String[] args) {
Users sc = new Users();//创建Users类的对象
String name2=sc.in.getname("水杯");//用户类中持有一个产品类型的成员变量。
String name3=sc.an.ordering("手机");// 用户类中持有一个订单类型的成员变量。
System.out.println("您下单的产品是:" + name3);
System.out.println("产品名称为:" + name2);
System.out.println("恭喜您,下单成功");
}
class order {
String ordering(String name){
return name;
}
}
class production{
String name1;//定义产品类的成员变量name1
String getname(String name){
String name1=name;
return name1;
}
}
}