java案例
买飞机票
package com.item.huang;
//买飞机票
public class Test01 {
public static void main(String[] args) {
double price = calculate(1000,5,"经济舱");
System.out.println(price);
}
//方法头需接收机票原价,当前月份,舱位类型
//方法头需返回计算出的机票优惠价
public static double calculate(double price,int month,String type){
//方法体
//1.判断当前月份是淡季还是旺季(通过判断数据在哪个区间,来决定执行哪个业务,应用if分支结构实现)
//2.判断舱位类型(通过判断数据匹配哪个值,来决定执行哪个业务,应用switch分支结构实现)
if (month>=5 && month<=10){
switch (type) {
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
}
}else{
switch (type) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
}
}
return price;
}
}
开发验证码
package com.item.huang;
import java.util.Random;
//开发验证码
public class Test02 {
public static void main(String[] args) {
String code = createCode(5);
System.out.println(code);
}
//方法名
//1.接收验证码的位数
//2.返回验证码
public static String createCode(int number){
//方法体
//1.使用Random类产生随机数,定义字符串code以便返回验证码
//2.使用switch分支结构判断随机数,从而随机一个数字(0-9),大写字母还是小写字母
//3.使用"+"运算符,连接字符
String code = "";
Random r = new Random();
for (int i = 0; i < number; i++) {
int type = r.nextInt(3);
switch (type){
case 0:
//随机一个数字
code += r.nextInt(10);
break;
case 1:
//随机一个大写字母
code += (char)(r.nextInt(26)+65);
break;
case 2:
//随机一个小写字母
code += (char)(r.nextInt(26)+97);
break;
}
}
return code;
}
}
评委打分
package com.item.huang;
import java.util.Scanner;
//评委打分
public class Test03 {
public static void main(String[] args) {
System.out.println("该选手分数是"+getAverageScore(6));
}
public static double getAverageScore(int number){
//动态创建数组,存储评委打分
int[] scores = new int[number];
//遍历数组,输入评委打分,同时增加可读性
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第"+(i+1)+"个评委的分数:");
sc.hasNextInt();
int score = sc.nextInt();
scores[i] = score;
}
//减掉最高分和最低分,再求平均分,返回最终的分数
int sum = 0;
int max = scores[0];
int min = scores[0];
for (int i = 0; i < scores.length; i++) {
//计算总分
sum += scores[i];
//计算最高分
if (scores[i]>max){
max = scores[i];
}
//计算最低分
if (scores[i]<min){
min = scores[i];
}
}
//返回最终的分数
return 1.0*(sum-max-min)/(number-2);
}
}
数字加密
package com.item.huang;
import java.util.Scanner;
//数字加密
public class Test04 {
public static void main(String[] args) {
System.out.println("加密后的密码是:"+enCrypt(1983));
}
public static String enCrypt(int number){
int[] numbers = split(number);
//numbers[1,9,8,3]
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (numbers[i]+5)%10;
}
//numbers[6,4,3,8]
reverse(numbers);
//numbers[8,3,4,6]
String data = "";
for (int i = 0; i < numbers.length; i++) {
data += numbers[i];
}
//data = "8346"
return data;
}
public static int[] split(int number){
//number 1983
int[] numbers = new int[4];
numbers[0] = number/10/10/10%10;
numbers[1] = number/10/10%10;
numbers[2] = number/10%10;
numbers[3] = number%10;
//numbers[1,9,8,3]
return numbers;
}
public static void reverse(int[] numbers){
for (int i = 0,j = numbers.length-1; i < j; i++,j--) {
int temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
//numbers[8,3,4,6]
}
}
数组拷贝
package com.item.huang;
//数组拷贝(得到一个和原数组一摸一样的数组)
public class Test05 {
public static void main(String[] args) {
int[] array = {11,22,33};
int[] array2 = copy(array);
printArray(array2);
}
public static int[] copy(int[] array){
//创建新数组array2
int[] array2 = new int[array.length];
//将原数组array的元素拷贝给新数组array2
for (int i = 0; i < array.length; i++) {
array2[i] = array[i];
}
//返回新数组array2[11,22,33]
return array2;
}
public static void printArray(int[] array){
System.out.print("[");
for (int i = 0; i < array.length; i++) {
System.out.print(i==array.length-1 ? array[i] : array[i]+",");
}
System.out.print("]");
}
}
抢红包
package com.item.huang;
import java.util.Random;
//抢红包
public class Test06 {
public static void main(String[] args) {
int[] moneys = {6,88,199,520,1314};
start(moneys);
}
public static void start(int[] moneys){
//moneys[6,88,199,520,1314]
//Random函数随机产生下标
Random r = new Random();
//采用交换规则-打乱原数组
int temp = 0;
for (int i = 0; i < moneys.length; i++) {
int index = r.nextInt(moneys.length); //index: 0-moneys.length-1
temp = moneys[index];
moneys[index] = moneys[i];
moneys[i] = temp;
}
//按照新数组顺序发红包
for (int i = 0; i < moneys.length; i++) {
System.out.println("恭喜你抢到"+moneys[i]+"元红包");
}
}
}
找素数
package com.item.huang;
//找素数 101-200
//采用多个方法可以简化问题
public class Test07 {
//遍历101-200,将素数打印出来
public static void main(String[] args) {
//记录素数的个数
int count = 0;
for (int i = 101; i <= 200 ; i++) {
if(check(i)){
System.out.println(i);
count++;
}
}
System.out.println("总共有"+count+"个素数");
}
//判断该数是否为素数,是返回true,不是则返回false
public static boolean check(int data) {
for (int i = 2; i <= data / 2; i++) {
if (data % i == 0) {
return false;
}
}
return true;
}
}
打印9*9乘法表
package com.item.huang;
//打印9*9乘法表
//计算机只能一行一行的打印数据
public class Test08 {
public static void main(String[] args) {
for (int i = 1; i <= 9 ; i++) {
for (int j = 1; j <= i ; j++) {
//j 列 i 行
System.out.print(j + "X" + i + "=" + (j*i) + "\t");
}
System.out.println();
}
}
}
打印三角形
package com.item.huang;
//打印三角形
//计算机只能一行一行的打印数据
public class Test09 {
public static void main(String[] args) {
// n 代表打印的行数
int n = 4;
//1.循环打印 n 行
for (int i = 1; i <= n ; i++) {
//2.先打印空格
for (int j = 1; j <= n-i ; j++) {
System.out.print(" ");
}
//3.再打印星星
//如果列数为偶数,打印空格否则打印星星(使用三元运算符)
for (int j = 1; j <= 2*i-1 ; j++) {
System.out.print(j%2==0 ? " " : "*");
}
//4.最后换行
System.out.println();
}
}
}
双色球案例
package com.item.huang;
import com.sun.deploy.panel.ITreeNode;
import java.util.Random;
import java.util.Scanner;
//双色球
public class Test10
{
//main方法
public static void main(String[] args) {
int[] userNumbers = userSelectNumbers();
printArray(userNumbers);
int[] luckyNumbers = randomLuckyNumbers();
printArray(luckyNumbers);
outcome(userNumbers,luckyNumbers);
}
//用户投注号码
public static int[] userSelectNumbers(){
//创建一个数组存储投注号码
int[] userNumbers = new int[7];
Scanner sc = new Scanner(System.in);
//1.先投注红球号码
for (int i = 0; i < userNumbers.length-1; i++) {
while (true) {
System.out.println("请输入一个红球号码(1-33之间):");
sc.hasNextInt();
int number = sc.nextInt();
if(number<1||number>33){
System.out.println("输入红球号码有误!");
}else{
if(repeat(userNumbers,number)){
System.out.println("输入红球号码重复!");
}else {
userNumbers[i] = number;
System.out.println("该次投注红球号码为"+number);
break;//结束这次投注红球号码
}
}
}
}
//2.再投注蓝球号码
while (true) {
System.out.println("请输入一个蓝球号码(1-16)之间:");
sc.hasNextInt();
int number = sc.nextInt();
if(number<1||number>16){
System.out.println("输入的蓝球号码错误!");
}else {
userNumbers[6] = number;
System.out.println("该次投注蓝球号码为"+number);
break;//结束这次投注蓝球号码
}
}
return userNumbers;
}
//随机中奖号码
public static int[] randomLuckyNumbers(){
int[] luckyNumbers = new int[7];
Random r = new Random();
//1.随机生成红球中奖号码(1-33)
for (int i = 0; i < luckyNumbers.length-1; i++) {
while (true) {
int number = r.nextInt(33)+1;
if (!repeat(luckyNumbers,number)){
luckyNumbers[i] = number;
break;
}
}
}
//2.随机生成蓝球中奖号码(1-16)
int number = r.nextInt(16)+1;
luckyNumbers[6] = number;
return luckyNumbers;
}
//中奖情况
public static void outcome(int[] userNumbers,int[] luckyNumbers){
int redCount = 0;
int blueCount = 0;
//1.计算红球中将个数
for (int i = 0; i < userNumbers.length-1; i++) {
for (int j = 0; j < luckyNumbers.length-1; j++) {
if(userNumbers[i] == luckyNumbers[j]){
redCount++;
break;
}
}
}
//2.计算蓝球的中奖个数
if(userNumbers[6] == luckyNumbers[6]){
blueCount++;
}
//3.统计中奖结果
if (redCount == 6 && blueCount == 1){
System.out.println("恭喜中奖1000万");
}else {
if (redCount ==6 && blueCount == 0){
System.out.println("恭喜中奖500万");
}else {
if (redCount == 5 && blueCount == 1){
System.out.println("恭喜中奖3000元");
}else{
if (redCount == 5 && blueCount == 0 || redCount == 4 && blueCount == 1){
System.out.println("恭喜中奖200元");
}else {
if (redCount == 4 && blueCount == 0 || redCount == 3 && blueCount == 1){
System.out.println("恭喜中奖10元");
}else{
if (redCount < 3 && blueCount == 1){
System.out.println("恭喜中奖5元");
}else {
System.out.println("谢谢参与!");
}
}
}
}
}
}
}
//判断红球号码是否重复
public static boolean repeat(int[] userNumbers,int number){
for (int i = 0; i < userNumbers.length-1; i++) {
if (number == userNumbers[i]){
return true;
}
}
return false;
}
//打印数组
public static void printArray(int[] array){
System.out.print("[");
for (int i = 0; i < array.length; i++) {
System.out.print(i==array.length-1 ? array[i] : array[i]+"," );
}
System.out.println("]");
}
}
标签:java,int,基础,System,number,案例,static,public,out
From: https://www.cnblogs.com/qingandji/p/18085011