package com.study;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//目标:完成飞机票的价格计算
//1、让用户输入机票原价,月份,仓位类型
Scanner sc = new Scanner(System.in);
System.out.println("请您输入机票原价:");
double money = sc.nextDouble();
System.out.println("请您输入机票的月份(1-12):");
int month = sc.nextInt();
System.out.println("请您选择仓位类型:");
String type = sc.next();
//4、调用方法,统计结果
System.out.println("机票优惠后的价格是:" + cacl(money,month,type));
}
/**
* 2、定义方法接收信息,统计优惠后的价格返回
*/
public static double cacl(double money,int month,String type){
//3、判断用户选择的信息情况
if(month>=5 && month<=10){
//旺季
switch (type) {
case "头等舱":
money *= 0.9;//money=money * 0.9;
break;
case "经济舱":
money *= 0.85;//money=money * 0.85;
break;
default:
System.out.println("您输入的仓位类型有误~~");
money = -1; //表示无法计算价格
}
}else if (month == 11 || month ==12 || month >= 1 && month <= 4){
//淡季
switch (type) {
case "头等舱":
money *= 0.7;//money=money * 0.7;
break;
case "经济舱":
money *= 0.65;//money=money * 0.65;
break;
default:
System.out.println("您输入的仓位类型有误哦~");
money = -1; //表示无法计算价格
}
}else{
System.out.println("您输入的月份有误~~");
money = -1; //表示无法计算价格
}
return money;//最终机票价格
}
}
package com.study;
public class test2 {
public static void main(String[] args) {
//1、定义一个循环,找到101-200直接的全部数据
for (int i = 101; i <= 200; i++) {
//信号位:标记
boolean flag = true;//一开始认为当前数据是素数
//2、判断当前遍历的这个数据是否是素数
for (int j = 2; j < i/2; j++) {
if(i % j == 0){
flag = false;
break;
}
}
//3、根据判定的结果选择是否输出这个数据,是素数则输出
if(flag){
System.out.println(i + "\t");
}
}
}
}
package com.study;
import java.util.Random;
public class Test3 {
public static void main(String[] args) {
String code = createCode(5);
System.out.println("随机验证码:" + code);
}
//1、定义一个方法返回一个随机验证码
public static String createCode(int n){
//3、定义一个字符串变量记录生成的随机字符
String code = "";
Random r = new Random();
//2、定义一个for循环,循环n次,依次生成随机字符
for (int i = 0; i < n; i++) {
//3、生成一个随机字符:英文大写 小写 数字(0 1 2)
int type = r.nextInt(3);
switch (type){
case 0:
//大写字符(A 65 —— Z 65+25) 编号范围(0-25)+65
char ch = (char)(r.nextInt(26)+25);
code += ch;
break;
case 1:
//小写字符(a 97 —— z 97+25) 编号范围(0-25)+97
char ch1 = (char)(r.nextInt(26)+97);
code += ch1;
break;
case 2:
//数字字符
code += r.nextInt(10);//0-9
break;
}
}
return code;
}
}
package com.study;
public class Test4 {
public static void main(String[] args) {
int[] arr1 = {19,21,24,27};
int[] arr2 = new int[arr1.length];
copy(arr1,arr2);
printArray(arr1);
printArray(arr2);
}
public static void printArray(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1? arr[i]:arr[i]+",");
}
System.out.println("]");
}
public static void copy(int[] arr1,int[] arr2){
//正式完成元素的复制
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
}
package com.study;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
//1、定义一个动态初始化的数组,用于录入评委分数
int[] scores = new int[6];
//2、录入6个评委分数
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("请您输入第"+(i+1)+"个评委的打分:");
int score = sc.nextInt();
//3、把这个分数存入到数据的对应位置处
scores[i] = score;
}
//4、遍历数组中的每个分数,找到最大值和最小值
int max = scores[0];
int min = scores[0];
int sum = 0;
for (int i = 0; i < scores.length; i++) {
//最大值
if (scores[i] > max){
max=scores[i];
}
//最小值
if (scores[i] < min){
min=scores[i];
}
//总分
sum +=scores[i];
}
//5、统计平均分即可
double result = (sum - max - min) * 1.0/(scores.length -2);
System.out.println("选手最终得分:"+result);
}
}
package com.study;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
//1、数据加密:定义一个数组存入需要加密的数据
System.out.println("请您输入需要加密的数字个数:");
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int[] arr = new int[length];
//2、录入需要加密的数字
for (int i = 0; i < arr.length; i++) {
System.out.println("请您输入加密的第"+(i+1)+"个数字:");
int number = sc.nextInt();
arr[i] = number;
}
//3、打印数组内容看一下
printArray(arr);
//4、核心逻辑(对数组加密)
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
//5、核心逻辑(对数组中加密反转)
for (int i = 0, j=arr.length -1 ;i < j; i++,j--) {
//直接交换两者位置即可
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
printArray(arr);
}
private static void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i]:arr[i]+",");
}
System.out.println("]");
}
}
package com.study;
import java.util.Random;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
//1、随机6个红球号码(1-33,不能重复),随机一个篮球号码(1-16),可以采用数组装起来作为中奖号码
int[] luckNumbers = createLuckNumber();
//2、用户选中的号码
int[] userNumbers = userInputNumbers();
//3、判断中奖情况
judge(luckNumbers,userNumbers);
}
public static void judge(int[] lunckNumbers,int[] userNumbers){
//判断是否中奖了
//1、定义两个变量分别存储红球命中的个数,以及篮球命中的个数
int red = 0;
int blue = 0;
//2、判断红球命中了几个,开始统计
for (int i = 0; i < userNumbers.length - 1; i++) {
for (int j = 0; j < lunckNumbers.length - 1; j++) {
//每次找到了相等了,意味着当前号码命中了
if (userNumbers[i] == lunckNumbers[j]){
red ++;
break;
}
}
}
//蓝球号码是否命中了
blue = lunckNumbers[6] == userNumbers[6] ? 1 : 0;
System.out.println("中奖号码是:" );
printArray(lunckNumbers);
System.out.println("您投注的号码是:" );
printArray(userNumbers);
System.out.println("您命中了几个红球:"+red);
System.out.println(("您是否命中蓝球:"+(blue == 1 ? "是":"否")));
//判断中奖情况
if(blue == 1 && red < 3){
System.out.println("恭喜您,中了5元小奖!");
}else if((blue == 1 && red == 3) || (blue == 0 && red == 4)){
System.out.println("恭喜您,中了10元小奖!");
}else if((blue == 1 && red == 4) || (blue == 0 && red == 5)){
System.out.println("恭喜您,中了200元大奖!");
}else if(blue == 1 && red == 5){
System.out.println("恭喜您,中了3000元大奖!");
}else if(blue == 0 && red == 6){
System.out.println("恭喜您,中了500万超级大奖!");
}else if(blue == 1 && red == 6){
System.out.println("恭喜您,中了1000万巨奖!可以开始享受人生,诗和远方!");
}else{
System.out.println("感谢您为福利事业作出的突出贡献!");
}
}
public static void printArray(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();//换行
}
public static int[] userInputNumbers(){
//a、定义一个数组存储7个号码
int[] numbers = new int[7];
//b、让用户录入6个红球
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numbers.length - 1; i++) {
System.out.println("请您输入第"+(i+1)+"个红球号码(1-33,要求不重复):");
int data = sc.nextInt();
//c、把当前录入的数据存入到数组中去
numbers[i] = data;
}
//d、单独录入一个蓝球号码
System.out.println("请您输入蓝球号码(1-16):");
numbers[6] = sc.nextInt();
return numbers;
}
public static int[] createLuckNumber() {
//a、定义一个动态初始化的数组,存储7个数字
int[] numbers = new int[7];
//b、遍历数组,为每个位置生成对应的号码。(注意:遍历前6个位置,生成6个不重复的红球号码,范围是1-33)
Random r = new Random();
for (int i = 0; i < numbers.length; i++) {
while (true) {
int data = r.nextInt(33) + 1;//1-33 —— > (0-32)+1
//c.注意:必须判断当前随机的这个号码之前是否出现过,出现过要重新随机一个,直到不重复为止,才可以存入数组中去
//定义一个flag变量,默认data没有重复
boolean flag = true;
for (int j = 0; j < i; j++) {
if (numbers[j] == data) {
//data当前这个数据之前出现过,不能用
flag = false;
break;
}
}
if (flag) {
//data这个数据之前没有出现过,可以使用了
numbers[i] = data;
break;
}
}
}
//d、为第7个位置生成一个1-16的号码作为篮球号码
numbers[numbers.length - 1] = r.nextInt(16) +1;
return numbers;
}
}