成绩排序,元素删除,冒泡排序,快速排序,判断素数,判断字符,求利润,定义类

1.考试成绩已经保存在数组scores中,依次为 89,23,64,91,119,52,73,-23

要求根据通过自定义方法来找出其中前三名,将数组成绩作为参数传入
要求判断成绩的有效性(0-100),如果成绩无效,则忽略此成绩.

public class HelloWorld{
    public static void main(String[] args){
        int[] scores={89,-23,64,91,119,52,73};      
        System.out.println("考试前三名为:");
        HelloWorld test=new HelloWorld();
        test.showTop3(scores);
    }
    public void showTop3(int[] scores) {
        Arrays.sort(scores);
        int num=0;
        for(int i=scores.length-1;i>=0;i--){
            if(scores[i]<0||scores[i]>100){
                continue;
            }
            num++;
            if(num>3){
                break;
            }
            System.out.println(scores[i]);
        }

    }
}

这里写图片描述

  1. 用数组来实现, 定义并初始化一个(1–100)保存100个数的数组,从第一个元素开始,依次数(1,2,3 循环往复),每次数到到3的元素淘汰掉.当到达数组
    末尾的时候再从头开始,直到最后剩余一个元素,写出算法,并输出最后保留的元素所在的最初的位置.
package HelloWorld;

  public class Delete3 {
     public static void Delete3(int[] num) {  
            int count = 0;//计数器  
            int i = 0;//下标 
            int j = 0;//记录被淘汰的数的位置  
            int numPerson = 0;
            while(count != 100) {  
                if(num[i] != 0) {     //通过该数是否为0 判断该数是否已经被淘汰  
                    if(numPerson == 2) {   //判断循环的数字是否到3  
                        num[i] = 0;     //达到3时  该数置为0 计数器加一  记录位置  循环计数器 重置为0  
                        count++;  
                        j = i;  
                        numPerson = 0;  
                            }  
                    else {  
                        numPerson++;  
                    }  
                }  
                i++;  
                if (i == 100) {  
                    i = 0;  
                }  
            }  
            System.out.println("最终淘汰的元素的下标为  " + j);  
        }  

        public static void main(String[] args) {  
            int[] num = new int[100];  
            for(int i = 0;i < num.length;i++)   
                num[i] = i + 1;   
            Delete3(num);
        }  
    }

这里写图片描述

  1. 用数组来实现对于一个整形数组, 分别通过冒泡排序和 快速排序,实现对于任意一个数组进行由小到大的排列。
public class ArraySort {
    public static void main(String[] args) {
        int [] score=new int[]{9,12,5,0,35,11};
            for(int i=0;i<score.length-1;i++){
                for(int j=i+1;j<score.length;j++){

                    if(score[i]>score[j]){
                        int temp=score[i];
                        score[i]=score[j];
                        score[j]=temp;//根据大小排序
            }
        }
    }

        for(int i=0;i<score.length;i++){
            System.out.print(score[i]); //按大小排序打印
            System.out.print("  ");
        }
    }
}

这里写图片描述

public class FastSort{

    public static void main(String []args){

       int[] a = {12,20,5,16,15,1,30,45,23,9};
       int start = 0;
       int end = a.length-1;
       sort(a,start,end);
       for(int i = 0; i<a.length; i++){
            System.out.print(a[i]+"   ");
        }

    }

    public static void sort(int[] a,int low,int high){
        int start = low;
        int end = high;
        int key = a[low];


        while(end>start){
            //从后往前比较
            while(end>start&&a[end]>=key)  //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
                end--;
            if(a[end]<=key){
                int temp = a[end];
                a[end] = a[start];
                a[start] = temp;
            }
            //从前往后比较
            while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
               start++;
            if(a[start]>=key){
                int temp = a[start];
                a[start] = a[end];
                a[end] = temp;
            }
        //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
        }
        //递归
        if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
        if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
    }

}

这里写图片描述

4.判断101-200之间有多少个素数,并输出所有素数。

public class Test {

        public static final void main(String[] args) {
            // 记录素数个数
            // 素数概念:除了1和它本身以外不再有其他的除数整除
            int num = 0;
            for (int i = 101; i < 201; i++) {
                for (int k = 2; k < i; k++) {
                    int remainder = i % k;
                    if (k == i - 1) {
                        ++num;
                        System.out.println("素数:" + i);
                    } else if (remainder == 0) {// 余数等于0说明有其他的除数整数,说明不是素数数
                        break;
                    }
                }
            }
            System.out.println(num + " 个");
        }
    }

这里写图片描述

5.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

public class BufferedReader {
    public static void main(String[] args) {  
        int letterSum = 0;  
        int blankSum = 0;  
        int numberSum = 0;  
        int otherSum = 0;  

        Scanner scan = new Scanner(System.in);  

        System.out.println("请输入字符串:");  

        String str = scan.nextLine();  //定义一个字符串并从键盘录入

        char[] ch = str.toCharArray();  

        for(int i = 0 ; i < ch.length; i++){  
            if(Character.isLetter(ch[i])){  
                letterSum++;  
            }else if(Character.isSpaceChar(ch[i])){  
                blankSum++;  
            }else if(Character.isDigit(ch[i])){  
                numberSum++;  
            }else{  
                otherSum++;  
            }  //判断字符串
        }  

        System.out.println("输入的字符串中共有字母:"+letterSum);  
        System.out.println("输入的字符串中共有空格:"+blankSum);  
        System.out.println("输入的字符串中共有数字:"+numberSum);  
        System.out.println("输入的字符串中共有其他字符:"+otherSum);  
    }  
}

这里写图片描述

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%提成,从键盘输入当月利润,求应发放奖金总数?

import java.util.Scanner;

public class BonusCalcolation {
public static void main(String[] args) {
double bonus=0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入利润");
double profit = sc.nextDouble();
if (profit > 0 && profit <= 10) {//依次判断利润的范围
         bonus=profit*0.1;
}
if (profit > 10 && profit <= 20) {
 bonus=10*0.1+(profit-10)*0.075;
}
if (profit > 20 && profit <= 40) {
 bonus=(profit-20)*0.05;
}
if (profit > 40 && profit <= 60) {
 bonus=(profit-40)*0.03;
}
if (profit > 60 && profit <=100) {
 bonus=(profit-60)*0.015;
}
if (profit>100) {
 bonus=(profit-100)*0.01;
}
System.out.println("您的奖金为"+bonus+"万元");
}

}

这里写图片描述

7.分别定义用户类,订单类,产品类,其中订单类至少要具有下订单的行为(入参为一个产品名称),
产品类中至少有一个成员变量为产品名称,至少有一个方法用来获取产品的名称。
用户类中持有一个产品类型的成员变量。
用户类中持有一个订单类型的成员变量。

在我们的用户类中定义main函数,当执行的时候,构造一个用户类的对象,
并且通过手段可以拿到产品成员变量中的产品名称,并且调用成员变量订单的对象,进行下单。
最后输出下单成功。

 import java.util.Scanner;
public class User {
String product;
String order;
public static void main(String[] args) {
    User u=new User();
    Product p=new Product();
    u.product=p.getName();
    Order o=new Order();
    o.placeAnOrder(u.product);
}
}
class Order  extends User{//订单类继承用户类
    void placeAnOrder(String name){//下单
        System.out.println("你的商品"+name+"下单成功");
    }

}
class Product  extends User{//产品类继承用户类
    String name="学习";
    String getName(){//输入产品名称
        Scanner n=new Scanner(System.in);
        System.out.println("请输入商品名称");
        name=n.next();
        return name;

    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值