案例一:飞机票查看优惠系统
项目需求:
- 机票价格按照淡季旺季、头等舱和经济舱收费,输入机票原价、月份和头等舱或经济舱。
- 机票最终优惠价格的计算方案如下 :旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。
代码实现:
package anli;
import java.util.Scanner;
public class planone {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入机票原价:");
int price = sc.nextInt();
System.out.println("请您输入仓位类型(头等舱/经济舱):");
String plantypes = sc.next();
System.out.println("请您输入月份(1-12):");
int month = sc.nextInt();
System.out.println("优惠后的价格为:"+Preferential(price,plantypes,month));
}
public static double Preferential(int price,String plantypes,int month){
if(month >= 5 && month <= 10){
switch (plantypes){
case "头等舱":
price *= 0.9;
break;
case "经济舱":
price *= 0.85;
break;
default:
System.out.println("对不起,您输入的仓位有误~~~");
price = -1;
}
}else if(month == 11 || month == 12 || month >= 1 && month <= 4){
switch (plantypes) {
case "头等舱":
price *= 0.7;
break;
case "经济舱":
price *= 0.65;
break;
default:
System.out.println("对不起,您输入的仓位有误~~~");
price = -1;
}
}else {
System.out.println("对不起,您输入的月份有误~~~");
price = -1;
}
return price;
}
}
案例二:获取素数
项目需求:判断101-200之间有多少个素数,并输出所有数据。
代码实现:
package anli;
public class sushu {
public static void main(String[] args) {
int sum = 0;
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){
sum++;
System.out.print(i+"\t");
}
}
System.out.println("共有"+sum+"个质数。");
}
}
案例三:验证码模块
项目需求:定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母。
代码实现 :
package anli;
import java.util.Random;
import java.util.Scanner;
public class yanzhengma {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入验证码的位数:");
int sum = sc.nextInt();
System.out.println("生成的验证码为:"+createCode(sum));
}
public static String createCode(int n){
String code = "";
Random r = new Random();
for (int i = 0; i < n; i++) {
int type = r.nextInt(3);
switch (type){
case 0:
char chA = (char)(r.nextInt(26) + 65);
code += chA;
break;
case 1:
char cha = (char)(r.nextInt(26) + 97);
code += cha;
break;
case 2:
code += r.nextInt(10);
break;
}
}
return code;
}
}
案例四:数组元素的复制
项目需求:把一个数组中的元素复制到另一个新数组中去。
代码实现:
package anli;
public class suzufuzhi {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55,66,77,88,99};
int[] arr1 = new int[arr.length];
list(arr,arr1);
System.out.println("原始数组为:");
printarr(arr);
System.out.println("复制的数组为:");
printarr(arr1);
}
public static void list(int[] arr,int[] arr1){
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
}
public static void printarr(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("]");
}
}
案例五:评委打分
项目需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
代码实现:
package anli;
import java.util.Scanner;
public class pingweidafen {
public static void main(String[] args) {
int[] scores = new int[6];
System.out.println("选手的最终得分为:"+ scos(scores));
}
public static float scos(int[] scores){
int min = scores[0],max = scores[0],sum = 0;
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;
}
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];
}
sum = (sum-min-max)/(scores.length-2);
return sum;
}
}
案例六:数字加密程序
项目需求:某系统的数字密码:比如193,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5,在对10求余,最后将所有数字反转,得到一串新数。
代码实现:
package anli;
import java.util.Scanner;
public class mimajiami {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要加密的数字的位数:");
int length = sc.nextInt();
int [] arr = new int[length];
addarr(arr);
System.out.println("加密的数字为:");
printarr(arr);
jiamiarr(arr);
System.out.println("加密后的数字为:");
printarr(arr);
}
public static void addarr(int[] arr){
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("请您输入加密的第"+(i+1)+"位数字");
int number = sc.nextInt();
arr[i] = number;
}
}
public static void jiamiarr(int[] arr){
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
for (int i = 0,j = arr.length-1; i < j; i++,j--) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
public static void printarr(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length-1 ? arr[i]+"\n" : arr[i]);
}
}
}
案例七:模拟双色球系统
项目需求:投注号码由6个红色球号码和一个蓝色球号码组成,红色球号码从1-33中选择;蓝色球号码从1-16中选择。
代码实现:文章来源:https://www.toymoban.com/news/detail-481837.html
package anli;
import java.util.Random;
import java.util.Scanner;
public class suangseqiu {
public static void main(String[] args) {
int[] lucknbr = cratenb();
int[] usernbr = usernubr();
judge(lucknbr,usernbr);
}
public static void judge(int[] lucknbr,int[] usernubr){
int redHitNub = 0;
int blueHiMbr = 0;
for (int i = 0; i < usernubr.length - 1; i++) {
for (int j = 0; j < lucknbr.length - 1; j++) {
if(usernubr[i] == lucknbr[j]){
redHitNub ++;
break;
}
}
}
blueHiMbr = lucknbr[6] == usernubr[6] ? 1:0;
System.out.println("中奖号码是:");
printArray(lucknbr);
System.out.println("您投注号码是:");
printArray(usernubr);
System.out.println("您命中了"+redHitNub+"个红球");
System.out.println("您"+(blueHiMbr == 1 ? "命中了":"没有命中")+"蓝球");
if(blueHiMbr == 1 && redHitNub < 3){
System.out.println("恭喜您,中了5元奖!");
}else if(blueHiMbr == 1 && redHitNub == 3 || blueHiMbr == 0 && redHitNub == 4){
System.out.println("恭喜您,中了10元奖!");
}else if(blueHiMbr == 1 && redHitNub == 4 || blueHiMbr == 0 && redHitNub == 5){
System.out.println("恭喜您,中了200元奖!");
}else if(blueHiMbr == 1 && redHitNub == 5){
System.out.println("恭喜您,中了300元奖!");
}else if(blueHiMbr == 0 && redHitNub == 6){
System.out.println("恭喜您,中了500万元奖!");
}else if(blueHiMbr == 1 && redHitNub == 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[] usernubr(){
int[] nubers = new int[7];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nubers.length-1; i++) {
System.out.println("请您输入第" + (i+1) +"个红球号码(1-33,要求不重复):");
int date = sc.nextInt();
nubers[i] = date;
}
System.out.println("请您输入篮球号码(1-16):");
nubers[6] = sc.nextInt();
return nubers;
}
public static int[] cratenb(){
int[] numb = new int[7];
Random r = new Random();
for (int i = 0; i < numb.length - 1; i++) {
while (true) {
int data = r.nextInt(33) + 1;
boolean flag = true;
for(int j = 0; j < i; j++){
if(numb[j] == data){
flag = true;
break;
}
}
if(flag){
numb[i] = data;
break;
}
}
}
numb[numb.length - 1] = r.nextInt(16) + 1;
return numb;
}
}
文章来源地址https://www.toymoban.com/news/detail-481837.html
到了这里,关于java基础编程案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!