今天跟着黑马做了7个小案例
-买飞机票
package practice;
import java.util.Scanner;
//完成飞机票的价格计算
public class practicedemo1 {
public static void main(String[] args) {
//输入购票信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价:");
double money = sc.nextDouble();
System.out.println("请输入购票月份:");
int month = sc.nextInt();
System.out.println("请输入仓位类型");
String type = sc.next();
System.out.println("机票优惠后的价格为:"+calc(money, month, type));
}
//定义方法接收信息统计优惠价格返回
public static double calc(double money, int month, String type)
{
//判断用户选择的信息情况
if(month >= 5&& month <= 10)
{
//旺季
switch(type)
{
case "头等舱":
money = month * 0.9;
break;
case "经济舱":
money = month * 0.85;
break;
default:
System.out.println("您输入的仓位类型有误");
}
}
else if(month == 11|| month == 12 || month >= 1 && month<= 4)
{
//淡季
switch(type)
{
case "头等舱":
money = money * 0.7;
break;
case "经济舱":
money = money * 0.65;
break;
default:
System.out.println("您输入的仓位类型有误");
}
}
else {
System.out.println("对不起您输入的月份有问题");
money = -1;
}
return money;
}
}
-找素数
package practice;
//找素数
public class practicedemo2
{
public static void main(String[] args) {
//定义循环找到101-200之间的全部数据
for (int i = 101; i < 201; 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+"\t");
}
}
}
}
-做验证码
package practice;
import java.util.Random;
import java.util.Scanner;
//随机产生一个五位验证码每位有可能是数字 大写字母 小写字母
public class practicedemo3 {
public static void main(String[] args) {
//调用
System.out.println("随机验证码为:"+createCode(5));
}
//定义一个方法返回随机验证码
public static String createCode(int n)
{
//定义一个字符串记录随机字符
String code = "";
//生成五位验证码
Random r = new Random();
for (int i = 0; i < n; i++) {
//生成一个随机字符:
int type = r.nextInt(3);//0 1 2
switch(type)
{
case 0:
//大写字母(A-Z)(A 65 - Z 65 + 25)(0 - 25) + 65
char ch = (char) (r.nextInt(26)+65);
code = code + ch;
break;
case 1:
//小写字母(a-z)(A 97 - Z 97 + 25)(0 - 25) + 97
char ch1 = (char) (r.nextInt(26)+97);
code = code + ch1;
break;
case 2:
code = code + r.nextInt(10);
//数字
break;
}
}
return code;
}
}
-数组的复制
package practice;
//数组元素复制(不是直接赋地址)
public class practicedemo4 {
public static void main(String[] args) {
int[] arr1 = {11, 22, 33, 44};
int[] arr2 = new int[4];
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 practice;
import java.util.Scanner;
//评委打分
public class practicedemo5 {
public static void main(String[] args) {
int[] scores = new int[6];//定义一个数字存放6位评委所打的分值
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("请您输入第"+(i+1)+"个评委的打分:");
int score = sc.nextInt();
scores[i] = score;
}
//遍历每个数据找出最大值最小值
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 = sum + scores[i];
}
//统计平均分
double average = 0;
average = (sum - max - min)*1.0/(scores.length-2);
System.out.println("选手的最高分是"+max);
System.out.println("选手的最低分是"+min);
System.out.println("选手的最终得分为"+average);
}
}
-数字加密
package practice;
import java.util.Scanner;
//数字加密
public class practicedemo6 {
public static void main(String[] args) {
//需求:数据加密
//定义一个数组存入需要加密的数据
System.out.println("请您输入需要加密的数字个数:");
Scanner sc= new Scanner(System.in);
int length = sc.nextInt();
int[] arr = new int[length];
//录入数字
for(int i = 0; i < arr.length; i++)
{
System.out.println("请您输入加密的第"+(i+1)+"个数字:");
int number = sc.nextInt();
arr[i] = number;
}
//打印数组内容看一下
printArray(arr);
//加密
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
//对数组加密的数据反转,定义两个变量一个放数组头,一个放数组尾,当翻转一次就头进一尾退一一次知道头i = 尾j
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);
}
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("]");
}
}
-双色球开奖
package practice;
import java.util.Random;
public class practicedemo7 {
public static void main(String[] args) {
int[] lucknumbers = createLuckNumber();
}
public static int[] createLuckNumber() {
//定义一个动态初始化数组存储七个数字
int[] numbers = new int[7];
//遍历数组为每个位置生成对应号码(遍历前六个位置放红球,最后一个放蓝球)
Random r = new Random();
for (int i = 0; i < numbers.length - 1; i++) {
//当生成的随机数时要重新生成一个随机数再装入数组中
while (true) {
int data = r.nextInt(33) + 1;
boolean flag = true;
for (int j = 0; j < i; j++) {
if (numbers[j] == data) {
flag = false;
//data这个数据之前出现过
break;
}
if (flag) {
numbers[i] = data;
}
}
}
}
//为第七个位置生成一个1-16的号码作为蓝球号码
numbers[numbers.length - 1] = r.nextInt(16) + 1;
return numbers;
}
}
双色球开奖只做了双色球明天继续完成剩下的两个模块吧。
又恢复线上了真麻烦。