1
.考试成绩已经保存在数组scores中,依次为 89,23,64,91,119,52,73,-23 要求根据通过自定义方法来找出其中前三名,将数组成绩作为参数传入 要求判断成绩的有效性(0-100),如果成绩无效,则忽略此成绩.
```
`public class ScoresSort{
public static void large(int[] arr){ //此函数选取数组中最大的
for(int i=1;i<arr.length;i++){
if(arr[i]>0&&arr[i]>arr[0]&&arr[i]<=100){ //判断是否大于零小于100并且判断a[i]是否大于a[0]
arr[i]=arr[i]^arr[0];
arr[0]=arr[i]^arr[0]; //若a[i]大于a[0]使用异或交换两个的值,将最大值赋值给a[0]
arr[i]=arr[i]^arr[0];
}
}}
public static void deleteLarge(int[] arr){ //设置循环先选取最大的,然后删除它,在选取剩下中最大的
for(int i=0;i<arr.length-1;i++){
arr[i]=arr[i+1];
}
}
public static void main(String[] args){
int[] arr=new int[]{89,23,64,91,119,52,73,-23};
for(int i=0;i<3;i++){
large(arr);
System.out.println(arr[0]);
deleteLarge(arr);}
}
}`
运行结果
2
. 用数组来实现, 定义并初始化一个(1–100)保存100个数的数组,从第一个元素开始,依次数(1,2,3 循环往复),每次数到到3的元素淘汰掉.当到达数组
末尾的时候再从头开始,直到最后剩余一个元素,写出算法,并输出最后保留的元素所在的最初的位置
package first;
public class Delete3 {
public static void main(String[] args) {
int distence = 0;
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
while (true) {//使用死循环重复运行代码,用break跳出
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
j++; //使用变量j记录数组中不为0的个数
}
}
if (j > 2) {
int z = distence;//将上一次末尾剩余的数字加在新的数组的前面
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
z++; //使用变量z记录不为零的个数,
}
if (z == 3) {
arr[i] = 0;//当z等于3时将该值改为0
z = 0; //重置不为零的个数
}
}
distence = (j + distence) % 3;// 用distence表示每一次改变完后末尾余的数据的个数
} else {
break;
}
}
if (distence == 2 || distence == 0) {
for (int i = arr.length - 1; i > 0; i--) {
if (arr[i] != 0) {
System.out.println(i);// distence为2或者0时应该输出后面的
break;
}
}
} else {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) { // distence为1时应该输出前面的
System.out.println(i);
break;
}
}
}
}
}
“
运行结果
3
.题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
package first;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
public class StatisticNumber {
public static void statistic(int[] arr) {
int number = 0;
int letter = 0;
int space = 0;//初始化变量
int else1 = 0;
for (int j = 0; j < arr.length; j++) {
if ((arr[j] >= 65 && arr[j] <= 90) || (arr[j] >= 97 && arr[j] <= 122)) {
letter++;
continue;//判断大小写字母的个数大写65~90,小写97~122
}
if (arr[j] >= 48 && arr[j] <= 57) {
number++; //判断数字的个数48~57
continue;
}
if (arr[j] == 32) {
space++;//空格ascii码为32,判断空格的个数
continue;
}
else1++;//其他的个数
}
System.out.println("字母的个数" + letter);
System.out.println("数字的个数" + number);
System.out.println("空格的个数" + space);
System.out.println("其他的个数" + (else1-1));//会默认把回车当做字符处理
}
public static void main(String[] args) {
char i = 0;
int number = 0;
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");//使用换行符作为分隔符
String a = sc.next();//接受字符串
int[] arr = new int[a.length()];//string.length()可以读取字符串的长度
for (int j = 0; j < a.length(); j++) {
i = a.charAt(j);//读取字符串中的字符
number = i;//将读出来的字符转换成ASCII码
arr[j] = number; //将ASCII码存入数组
}
statistic(arr);//调用方法
}
}
运行结果
4
.题目:企业发放的奖金根据利润提成。利润(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%提成,从键盘输入当月利润,求应发放奖金总数?
package first;
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+"万元");
}
}
运行结果
5
.分别定义用户类,订单类,产品类,其中订单类至少要具有下订单的行为(入参为一个产品名称),
产品类中至少有一个成员变量为产品名称,至少有一个方法用来获取产品的名称。
用户类中持有一个产品类型的成员变量。
用户类中持有一个订单类型的成员变量。
在我们的用户类中定义main函数,当执行的时候,构造一个用户类的对象,
并且通过手段可以拿到产品成员变量中的产品名称,并且调用成员变量订单的对象,进行下单。
最后输出下单成功。
package first;
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;
}
}
运行结果
6
.判断101-200之间有多少个素数,并输出所有素数。
package first;
public class PrimeSearch {
public static void main(String[] args) {
int[] arr = new int[100];
int number = 0;
for (int i = 0; i < arr.length; i++) {//初始化数组
arr[i ] = i+101 ;
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {//用模2来判断是否为素数
System.out.println(arr[i]);
number++;//用number来记录素数的个数
}
}
System.out.println("素数的个数为"+number);
}
}
运行结果
7
.用数组来实现对于一个整形数组, 分别通过冒泡排序和 快速排序,实现对于任意一个数组进行由小到大的排列。
冒泡排序
package first;
public class ArraySort {
public static void bubbleSort(int[] arr) {// 冒泡排序
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
if (arr[i] > arr[j]) {
arr[i] = arr[i] ^ arr[j];// 使用异或来交换数值
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}}}
public static void main(String[] args) {
int[] arr = new int[] { 2, 3, 4, 5, 5, 9, 3 };
bubbleSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
运行结果
快速排序
package first;
public class ArraySort2{//快速排序
public static int partition(int[] array,int lo,int hi){
//固定的切分方式
int key=array[lo];
while(lo<hi){
while(array[hi]>=key&&hi>lo){//从后半部分向前扫描
hi--;
}
array[lo]=array[hi];
while(array[lo]<=key&&hi>lo){//从前半部分向后扫描
lo++;
}
array[hi]=array[lo];
}
array[hi]=key;
return hi;
}
public static void sort(int[] array,int lo ,int hi){
if(lo>=hi){
return ;
}
int index=partition(array,lo,hi);
sort(array,lo,index-1);
sort(array,index+1,hi);
}
public static void main(String[] args) {
int[] arr=new int[]{1,5,6,9,7,2,3};
sort(arr,0 ,arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}}}
运行结果