目录
案例1.买飞机票
案例2.找素数
案例3.开发验证码
import java.util.Scanner;
/**
* 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。
* 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
*/
public class base {
public static void main(String[] args) {
boolean flag=true;
Scanner scanner = new Scanner(System.in);
while (flag){
System.out.println("请输入机票原价");
int money = scanner.nextInt();
System.out.println("请输入月份");
int month = scanner.nextInt();
System.out.println("请输入头等舱(1)或经济舱(2)");
int j = scanner.nextInt();
double Money=compare(money, month, j);
flag=false;
if (Money==-1){
flag=true;
continue;
}
System.out.println("价格:"+Money);
}
}
private static double compare(int money,int month,int k) {
if (month>=5&&month<=10){//旺季
if (k==1){
return 0.9*money;
}else {
return 0.85*money;
}
}else if (month==11||month==12||month>=1&&month<=4){//淡季
if (k==1){
return 0.7*money;
}else {
return 0.65*money;
}
}
else {
System.out.println("你输入的月份错误");
return -1;
}
}
}
判断101-200之间有多少个素数,并输出所有素数。.
/**
* 判断101-200之间有多少个素数,并输出所有素数。
* 说明
* 素数:如果除了1和它本身以外,不能被其他正整数整除,就叫素数。
*/
public class base {
public static void main(String[] args) {
for (int i = 101; i <= 200; i++) {
boolean flag=true;
for (int j = 2; j <(i/2); j++) {
if (i%j==0){
flag=false;
break;
}
}
if (flag){
System.out.print(i+" ");
}
}
}
}
import java.util.Random;
/**
* 需求:
* 定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母。
*/
public class base {
public static void main(String[] args) {
System.out.println(verification(5));
}
private static String verification(int n) {
Random random = new Random();
String code="";
for (int i = 0; i < n; i++) {
int type=random.nextInt(3);// 0 1 2
switch(type){
case 0://大写字母
char ch=(char) (random.nextInt(26)+65);
code+=ch;
break;
case 1://小写字母
char ch1=(char) (random.nextInt(26)+97);
code+=ch1;
break;
case 2://数字
code+= random.nextInt(10);
break;
}
}
return code;
}
}
随机验证码的核心实现逻辑是如何进行的?
活动地址:CSDN21天学习挑战赛