[Java作业]Week02

此篇博客记录第二周的作业

Week02

1.作业一

package org.westos;
/**
 * 1.考试成绩已经保存在数组scores中,依次为 89,23,64,91,119,52,73,-23
  要求根据通过自定义方法来找出其中前三名,将数组成绩作为参数传入
  要求判断成绩的有效性(0-100),如果成绩无效,则忽略此成绩.
  输出结果:
  第1名的成绩是:91
  第2名的成绩是:89
  第3名的成绩是:73
 * */
public class TestDemo_1 {
	
	private static  void sort(int[] scores) {
		//通过快速排序选中前三名并将其放在数组前三位
		for (int i = 0;i < scores.length;i++) {
			for (int j = i + 1;j < scores.length;j++)  {
				//确保数据有效性
				if (scores[j] < 0 || scores[j] > 100) {
					continue;
				}
				if (scores[j] > scores[i]) {
					int temp = scores[j];
					scores[j] = scores[i];
					scores[i] = temp;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		int[] scores = {89,23,64,91,119,52,73,-23};
		sort(scores);
		for(int i = 0;i < 3;i ++) {
			System.out.println("第" + (i + 1) + "名的成绩是:" + scores[i]);
		}
	}
}

2.作业二

package org.westos;
/**用数组来实现, 定义并初始化一个(1--100)保存100个数的数组,从第一个元素开始,依次数(1,2,3 循环往复),
 * 每次数到到3的元素淘汰掉.当到达数组末尾的时候再从头开始,
 * 直到最后剩余一个元素,写出算法,并输出最后保留的元素所在的最初的位置.
 * 
 * 输出结果:
 *  最终淘汰的元素的下标为   90
 * */
public class TestDemo_2 {
	
	//初始化数组
	private static void setArr(int[] num) {
		for(int i = 0;i < num.length;i++) 
			num[i] = i + 1;
	}
	
	private static void cirWithThree(int[] num) {
		int count = 0;//计数器
		int loc = 0;//记录被淘汰的数的位置
		int i = 0;//下标
		int numPerson = 0;//每次循环的人数  1,2,3
		while(count != 100) {
			if(num[i] != 0) {     //通过该数是否为0 判断该数是否已经被淘汰
				if(numPerson == 2) {   //判断循环的数字是否到3
					num[i] = 0;     //达到3时  该数置为0 计数器加一  记录位置  循环计数器 重置为0
					count++;
					loc = i;
					numPerson = 0;
					System.out.println("第" + (i + 1) + "位置的元素被淘汰");
				}
				else {
					numPerson++;
				}
			}
			i++;
			if (i == 100) {
				i = 0;
			}
		}
		System.out.println("最终淘汰的元素的下标为  " + loc);
	}
	
	public static void main(String[] args) {
		int[] num = new int[100];
		setArr(num);
		cirWithThree(num);
	}
}


3.作业三

package org.westos;
/**用数组来实现对于一个整形数组, 分别通过冒泡排序和 快速排序,实现对于任意一个数组进行由小到大的排列。
 * */
/**
 * 输出结果:
 * 排序之前的数组序列:5 8 4 2 6 8 4 4 788 2 20 7 
 * 快速排序:
 * 2 2 4 4 4 5 6 7 8 8 20 788 
 * 冒泡排序
 * 2 2 4 4 4 5 6 7 8 8 20 788 
 */
public class TestDemo_3 {
	//对数组进行初始化  考虑到无法进行随机数   考虑放弃
	private static void setArr(int[] num) {
		for(int i = 0;i < num.length;i++) {
			num[i] = (i + 5) % (i + 2);
		}
	}
	//选择排序
	public static void sortTest1(int[] num) {
		for(int i = 0;i < num.length;i++) {
			for(int j = i + 1;j < num.length;j++) {
				if(num[j] < num[i]) {
					int temp = num[j];
					num[j] = num[i];
					num[i] = temp;
				}
			}
		}
		System.out.println("选择排序:");
		printArr(num);
	}
	//快速排序
	public static void quickSort(int[] num,int low, int high) {
		int left = low;
		int right = high;
		int key = num[low];
		int temp;
		
		while (left < right) {
			//从有边往左边找,把大的数放到右边
			while (left < right && num[right] >= key) {
				right--;
			}
			if (left < right) {
				temp = num[right];
				num[right] = num[left];
				num[left] = temp;
				left++;
			}
			//从左边往右边找
			while (left < right && num[left] <= key) {
				left++;
			}
			if(left < right) {
				temp = num[right];
				num[right] = num[left];
				num[left] = temp;
				right--;
			}
		}
		if(left > low) {
			quickSort(num,low,left - 1);
		}
		if(right < high) {
			quickSort(num,right + 1,num.length - 1);
		}
	}
	//冒泡排序
	public static void sortTest2(int[] num) {
		for(int j = 0;j < num.length; j++) {	
			for(int i = 0;i < num.length - 1;i++) {
					if(num[i + 1] < num[i]) {
						int temp = num[i + 1];
						num[i + 1] = num[i];
						num[i] = temp;
					}
				}
		}
		System.out.println("冒泡排序");
		printArr(num);
	}
	//输出数组
	public static void printArr(int[] num) {
		for(int i = 0;i < num.length;i++) {
			System.out.print(num[i] + " ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		int[] num = new int[] {5,8,4,22,16,8,4,4,788,32,20,7};
//		setArr(num);
		System.out.print("排序之前的数组序列:");
		printArr(num);
//		sortTest1(num);     注意不要多个排序一起测试,数组是引用类型,会干扰后面的结果
		quickSort(num,0,num.length - 1);
		System.out.println("快速排序");
		printArr(num);
//		sortTest2(num);
	}
}


4.作业四

package org.westos;
/**判断101-200之间有多少个素数,并输出所有素数。
 * 输出结果
 * 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 
 * */

public class TestDemo_4 {
	//列出101-200之间的素数
	public static void printPrime() {
		for (int i = 101; i <= 200;i++) {
			boolean flag = false;
			for (int j = 2;j <= i/2;j++) {
				if (i % j == 0) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				System.out.print(i + " ");
			}
		}
	}
	
	public static void main(String[] args) {
		printPrime();
	}
}

5.作业五

package org.westos;

import java.util.Scanner;

/**输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
 * lalala 123 456 **
英文字母个数:6
数字个数:6
空格个数:3
其他字符个数:2
 * 
 * */
public class TestDemo_5 {
	
	private static void printNum(String s) {
		int numChar = 0;
		int numSpace = 0;
		int num = 0;
		int numOther = 0;
		//把字符串的每个字符转化为char类型
		char[] ch = s.toCharArray(); //

		for(int i = 0;i < ch.length;i++) {
			if((ch[i] >= 'a' && ch[i] < 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) {
				numChar++;
			}else if(ch[i] >= '0' && ch[i] <= '9') {
				num++;
			}else if(ch[i] == ' ') {
				numSpace++;
			}else {
				numOther++;
			}
		}
		System.out.println("英文字母个数:" + numChar);
		System.out.println("数字个数:" + num);
		System.out.println("空格个数:" + numSpace);
		System.out.println("其他字符个数:" + numOther);
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String string = sc.nextLine();
		printNum(string);
	}
}

6.作业六

package org.westos;

import java.util.Scanner;

/*
 * 企业发放的奖金根据利润提成。
 * 利润低于或等于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%提成,
 * 从键盘输入当月利润,求应发放奖金总数?

 */
public class TestDemo_6 {
	//根据利润奖金算出奖金
	public static double getBonus(double profit) {
		double bonus = 0;
		if (profit <= 10) {
			bonus = profit * 0.1;
		}
		if (profit > 10 && profit <= 20) {
			bonus = getBonus(10) + (profit - 10) * 0.075;
		}
		if (profit > 20 && profit <= 40) {
			bonus = getBonus(20)  + (profit - 20) * 0.05;
		}
		if (profit > 40 && profit <= 60) {
			bonus = getBonus(40) + (profit - 40) * 0.03;
		}
		if (profit > 60 && profit <= 100) {
			bonus = getBonus(60) + (profit - 60) * 0.015;
		}
		if (profit > 100) {
			bonus = getBonus(100) + (profit - 100) * 0.01; 
		}
		return bonus;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double profit = sc.nextDouble();
		double bonus = getBonus(profit);
		System.out.println("奖金总额为:" + bonus);
	}
}

7.作业七

package org.westos;

import java.util.Scanner;

//TestDemo_7

/**分别定义用户类,订单类,产品类,其中订单类至少要具有下订单的行为(入参为一个产品名称),
  产品类中至少有一个成员变量为产品名称,至少有一个方法用来获取产品的名称。
  用户类中持有一个产品类型的成员变量。
  用户类中持有一个订单类型的成员变量。
  在我们的用户类中定义main函数,当执行的时候,构造一个用户类的对象,
  并且通过手段可以拿到产品成员变量中的产品名称,并且调用成员变量订单的对象,进行下单。
  最后输出下单成功。
 * @author asus-pc
 *
 *输出结果:
	余额为:100.0
	Please input the name:三鹿奶粉
	Please input the price:20.00
	下单成功!您已成功购买三鹿奶粉!
	总消费:20.0
	余额为:80.0

 */
public class User {
	Product purchaseObj = new Product();
	Order order = new Order();
	double balance = 100.00;
	//查询余额
	public void getBalance() {
		System.out.println("余额为:" + balance);
	}
	
	public static void main(String[] args) {
		User user = new User();
		user.getBalance();
		user.purchaseObj.setName();
		user.purchaseObj.setPrice();
		user.order.setOrder(user.purchaseObj);
		user.balance -= user.purchaseObj.price;
		user.getBalance();
	}
}

class Product {
	String productName;
	double price;
	//初始化产品名称
	public void setName() {
		System.out.print("Please input the name:");
		Scanner sc = new Scanner(System.in);
		productName = sc.nextLine();
	}
	//初始化产品价格
	public void setPrice() {
		System.out.print("Please input the price:");
		Scanner sc = new Scanner(System.in);
		price = sc.nextDouble();
	}
	
	public void getName() {
		System.out.println(productName);
	}
}

class Order {
	//下单行为
	public void setOrder(Product p) {
		System.out.println("下单成功!" + "您已成功购买" + p.productName + "!");
		System.out.println("总消费:" + p.price);
	}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值