6.java基础----短暂总结编程题
案例一:买飞机票
package com.china.homework;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
double price=getPrice();
System.out.println("现在机票的价格是:"+price+"元");
}
public static double getPrice()
{
System.out.println("请输入价格");
double price = getDada();
System.out.println("请输入月份");
double month = getDada();
System.out.println("请选择舱号类型:1.(头等舱),2.(经济舱)");
double type = getDada();
if (month >= 6 && month <= 10) {
if (type == 1) {
System.out.println("头等舱");
price *= 0.9;
} else if (type == 2) {
System.out.println("经济舱");
price *= 0.85;
} else {
System.out.println("没有该类型");
}
} else {
if (type == 1) {
System.out.println("头等舱");
price *= 0.7;
} else if (type == 2) {
System.out.println("经济舱");
price *= 0.65;
} else {
System.out.println("没有该类型");
}
}
return price;
}
public static double getDada() {
Scanner scanner = new Scanner(System.in);
double data = scanner.nextInt();
return data;
}
}
案例二:找质数
package com.china.homework;
public class Test10 {
public static void main(String[] args) {
boolean flag=false;
for(int i=101;i<201;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=true;
break;
}
}
if(flag==false)
{
System.out.println(i+"是质数");
}
flag=false;
}
}
}
案例三:开发验证码
package com.china.homework;
import java.util.Random;
public class Test13 {
public static void main(String[] args) {
char[] c=method();
System.out.println(getCode(4,c));
}
public static String getCode(int num,char[] chars)
{
String s="";
Random random=new Random();
for(int k=0;k<num;k++)
{
int i=random.nextInt(62);
s+=chars[i];
}
return s;
}
public static char[] method()
{
char[] chars=new char[62];
int index=0;
for(char num='0';num<='9';num++)
{
chars[index]=num;
index++;
}
for (char upCase = 'A'; upCase <='Z'; upCase++) {
chars[index]=upCase;
index++;
}
for (char lowCase='a';lowCase<'z'; lowCase++) {
chars[index]=lowCase;
index++;
}
return chars;
}
}
案例四:数组·元素复制
package com.china.homework;
public class Test14 {
public static void copy(int[] arr1)
{
int[] arr2=new int[arr1.length];
for(int i=0;i<arr1.length;i++)
{
arr2[i]=arr1[i];
}
System.out.print("[");
for(int i=0;i<arr2.length;i++)
{
System.out.print(arr2[i]);
if(i< arr2.length-1)
{
System.out.print(",");
}
}
System.out.print("]");
}
public static void main(String[] args) {
int[] arr1={1,3,6,7};
copy(arr1);
}
}
案例五:数字元素加密
package com.china.homework;
import java.util.Scanner;
public class Test15 {
public static void method()
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个值");
int num = sc.nextInt();
int count=1;
int[] arr = new int[num];
while (true) {
int ge = num%10;
arr[count-1] = ge;
if (num/10==0){
break;
}else {
num/=10;
count++;
}
}
int[] newarr = new int[count];
for (int i = 0; i < count; i++) {
newarr[i] = (arr[i]+5)%10;
System.out.print(newarr[i]+"\t");
}
for(int begin=0,end=newarr.length-1;begin<end;begin++,end--)
{
int temp=newarr[end];
newarr[end]=newarr[begin];
newarr[begin]=temp;
}
String s="";
for(int i=0;i<newarr.length;i++)
{
s+=newarr[i];
}
System.out.print("最后的值是"+s);
}
public static void main(String[] args) {
method();
}
}
案例六:模拟双色球
package com.china.homework;
import java.util.Random;
public class Test18 {
public static void method()
{
int[] arr=new int[7];
arr[0]=randomNumber(arr);
arr[1]=randomNumber(arr);
arr[2]=randomNumber(arr);
arr[3]=randomNumber(arr);
arr[4]=randomNumber(arr);
arr[5]=randomNumber(arr);
arr[6]=new Random().nextInt(17)+1;
for(int arr2:arr)
{
System.out.print(arr2+" ");
}
}
public static int randomNumber(int[] arr)
{
int number;
while (true) {
number = new Random().nextInt(33) + 1;
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
if (number == arr[i]) {
flag = true;
break;
}
}
if (flag == false) {
break;
}
}
return number;
}
public static void main(String[] args) {
method();
}
}