机票价格按照,淡季和旺季、头等舱和经济舱收费。输入机票原价、月价和头等舱或经济舱
按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折
package math.math;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入票价:");
double ticket = sc.nextInt();
System.out.println("请输入月份:");
int moon = sc.nextInt();
System.out.println("请输入仓位:1为头等舱 0为经济舱");
int dinner = sc.nextInt();
if (moon >= 5 && moon <= 10) {
if (dinner == 1) {
ticket = ticket * 0.9;
} else if (dinner == 0) {
ticket = ticket * 0.85;
} else {
System.out.println("输入不合法");
}
} else if ((moon >= 1 && moon <= 4) || (moon >= 11 && moon <= 12)) {
if (dinner == 1) {
ticket = ticket * 0.7;
} else if (dinner == 0) {
ticket = ticket * 0.65;
} else {
System.out.println("输入不合法");
}
} else {
System.out.println("录入月份不合法");
}
System.out.println(ticket);
}
}
判断101~202之间有多少个质数,并输出所以质数
package math.math;
public class test {
public static void main(String[] args) {
int count = 0;
for (int i = 101; i <= 200; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.print(i + " ");
count++;
}
}
System.out.println(count);
}
}
定义方法实现随机产生一个5位的验证码
验证码格式:长度为5;前四位是大写字母或者小写字母;最后一位是数字
package math.math;
import java.util.Random;
public class test {
public static void main(String[] args) {
char[] chs = new char[52];
for (int i = 0; i < chs.length; i++) {
if(i <= 25){
chs[i] = (char)(97 + i) ;
}else {
chs[i] = (char)(65 + i - 26);
}
}
String result = "";
Random r = new Random() ;
for (int i = 0; i < 4; i++) {
int random = r.nextInt(chs.length);
result = result + chs[random] ;
}
int number = r.nextInt(10);
result= result + number ;
System.out.println(result);
}
}
把一个数组中的元素复制到另一个新数组中去
package math.math;
public class newclass {
public static void main(String[] args) {
int[] ne = {1,3,5,7,8,10} ;
int[] me = new int[ne.length];
for (int i = 0; i < ne.length; i++) {
me[i] = ne[i] ;
}
for (int i = 0; i < me.length; i++) {
System.out.println(me[i]);
}
}
}
在歌唱比赛中,有6名评委给选手打分,分数范围是【0 - 100】之间的整数。
选手的最后得分为:去掉最高分、最低分后的4个评委的平均分。
请完成上述过程并计算出选手的得分。
package math.math;
import java.util.Scanner;
public class newclass {
public static void main(String[] args) {
int[] scoreA = get();
int max = max(scoreA);
int min = min(scoreA);
int sum = sum(scoreA);
int get = (sum - max - min) / (scoreA.length - 2);
System.out.println("平均分为:" + get);
}
//成绩录入数组
public static int[] get() {
int[] arr = new int[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 6; ) {
System.out.println("请输入评委的打分");
int score = sc.nextInt();
if (score >= 0 && score <= 100) {
arr[i] = score;
i++;
} else {
System.out.println("成绩无效,重新输入");
}
}
return arr;
}
//求录入数组最大值
public static int max(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
//求录入数组最小值
public static int min(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
//求和
public static int sum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
}
某系统的数字密码(大于0),比如1983,采用加密方式进行传输
规则:先得到每位数,然后每位数都加上5,再对10求余,最后将所有数字反转,得到一串新数
package math.math;
import java.util.Scanner;
public class newtest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入密码:");
int number = sc.nextInt();
int temp = number;
//确定数组长度
int count = 0;
while (number != 0) {
number = number / 10;
count++;
}
int[] ra = new int[count];
//输入值添加入数组
int index = ra.length - 1;
while (temp != 0) {
int ge = temp % 10;
temp = temp / 10;
ra[index] = ge;
index--;
}
//加法
for (int i = 0; i < ra.length; i++) {
ra[i] = ra[i] + 5;
}
//取余
for (int i = 0; i < ra.length; i++) {
ra[i] = ra[i] % 10;
}
//调换
for (int i = 0, j = ra.length - 1; i < j; i++, j--) {
int tem = ra[i];
ra[i] = ra[j];
ra[j] = tem;
}
//求和
int numbe = 0;
for (int i = 0; i < ra.length; i++) {
numbe = numbe * 10 + ra[i];
}
System.out.println(numbe);
}
}
分别有{2,588,888,1000,10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。
package math.math;
import java.util.Random;
public class threeTest {
public static void main(String[] args) {
int[] arr = {2, 588, 888, 1000, 10000};
int[] newarr = new int[arr.length];
Random r = new Random();
for (int i = 0; i < 5; ) {
int rand = r.nextInt(arr.length);
int prize = arr[rand];
boolean flag = contains(newarr, prize);
if (!flag) {
newarr[i] = prize;
i++;
}
}
for (int i = 0; i < newarr.length; i++) {
System.out.println(newarr[i] + "已经被抽出");
}
}
public static boolean contains(int[] arr, int prize) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == prize) {
return true;
}
}
return false;
}
}
package math.math;
import java.util.Random;
import java.util.Scanner;
public class fourTest {
public static void main(String[] args) {
int[] arr = CNumber();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
int[] userinput = user();
for (int i = 0; i < userinput.length; i++) {
System.out.print(userinput[i] + " ");
}
int redCount = 0;
int blueCount = 0;
for (int i = 0; i < userinput.length - 1; i++) {
int redNumber = userinput[i] ;
for (int j = 0; j < arr.length - 1; j++) {
if(redNumber == arr[j]){
redCount++ ;
break;
}
}
}
int blueNumber = userinput[userinput.length - 1];
if (blueNumber == arr[arr.length - 1]){
blueCount++;
}
if(redCount == 6 && blueCount == 1){
System.out.println("头奖");
}else if (redCount == 6 && blueCount == 0){
System.out.println("次奖");
}else {
System.out.println("低等奖");
}
}
public static int[] user() {
int[] arr = new int[7];
Scanner sc = new Scanner(System.in);
//红球号码输入
for (int i = 0; i < 6; ) {
System.out.println("请输入红球号码:");
int redNumber = sc.nextInt();
if (redNumber >= 1 && redNumber <= 33) {
boolean flag = contains(arr, redNumber);
if (!flag) {
//不存在,可以添加
arr[i] = redNumber;
i++;
} else {
System.out.println("号码重复");
}
} else {
System.out.println("输入号码不合规");
}
}
//篮球号码输入
while (true) {
System.out.println("请输入篮球号码:");
int blueNumber = sc.nextInt();
if (blueNumber >= 1 && blueNumber <= 16){
arr[arr.length - 1] = blueNumber ;
break;
}else {
System.out.println("输入号码不合规");
}
}
return arr ;
}
public static int[] CNumber() {
int[] arr = new int[7];
Random r = new Random();
for (int i = 0; i < 6; ) {
int redNumber = r.nextInt(33) + 1;
boolean flag = contains(arr, redNumber);
if (!flag) {
arr[i] = redNumber;
i++;
}
}
int blueNumber = r.nextInt(16) + 1;
arr[arr.length - 1] = blueNumber;
return arr;
}
public static boolean contains(int[] arr, int number) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == number) {
return true;
}
}
return false;
}
}
Tips
以上学习内容均来自于B站黑马程序员