【java基础】几个涵盖基础语法练习的小代码
1.猜数(while自建死循环,一定要配合break使用)
import java.util.Scanner;
public class GuessingDemo1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*100+1);
System.out.println("Guess it~");
while (true){
System.out.println("请输入要猜的数");
int guessNum= scan.nextInt();
if (guessNum>num){
System.out.println("猜大了~");
}else if (guessNum<num){
System.out.println("猜小了~");
}else {
System.out.println("恭喜,猜对了~");
break;
}
}
}
}
2 计算票价小代码,对输入的数据进行验证
import java.util.Scanner;
public class CalAirPrice1 {
/*
5-10月 头等舱9 商务舱85 经济舱8
11-4 头等舱7 商务舱65 经济舱6
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入机票原价:");
double airPrice = scan.nextDouble();
System.out.println("请选择月份:");
int month = scan.nextInt();
while (true){
if (month>=1 && month<=12){
break;
}else {
System.out.println("请输入正确月份:");
month = scan.nextInt();
}
}
System.out.println("请选择舱位:1.头等舱 2.商务舱 3.经济舱 ");
int type = scan.nextInt();
while (true){
if (type>=1 && type<=3){
break;
}else {
System.out.println("请输入正确舱位:");
type = scan.nextInt();
}
}
double finalPrice = calAirPrice(airPrice, month, type);
System.out.println(finalPrice);
}
public static double calAirPrice(double airPrice, int month, int type){
double finalPrice = 0.0;
if (month>=5 && month<=10){
switch (type){
case 1:
finalPrice = airPrice * 0.9;
break;
case 2:
finalPrice = airPrice * 0.85;
break;
case 3:
finalPrice = airPrice * 0.80;
break;
}
}else {
switch (type){
case 1:
finalPrice = airPrice * 0.7;
case 2:
finalPrice = airPrice * 0.65;
case 3:
finalPrice = airPrice * 0.60;
}
}
return finalPrice;
}
}
3 数组求最值问题
import java.util.Arrays;
import java.util.Scanner;
/*
6个评分1-100
去掉最高分,和最低分后的4个评委的平均分
*/
public class CalTotalAvg1 {
public static void main(String[] args) {
double[] scores = enterData(6);
//System.out.println(Arrays.toString(scores));
double totalScore = calTotal(scores);
//System.out.println(totalScore);
}
public static double[] enterData(int len){
Scanner scan = new Scanner(System.in);
double[] scores = new double[len];
for (int i=0;i<scores.length;i++){
System.out.println("请输入第"+ (i+1)+ "位评委的成绩: ");
scores[i] = scan.nextDouble();
}
return scores;
}
public static double calTotal(double[] scores){
double calTotal = 0.0;
double max = scores[0];
double min = scores[0];
for (int i=0;i<scores.length;i++){
if (scores[i]>max){
max = scores[i];
}
if (scores[i]<min){
min = scores[i];
}
calTotal += scores[i];
}
return calTotal - min - max;
}
public static double calAvg(double[] scores){
double avg = calTotal(scores)/(scores.length-2);
return avg;
}
}
4 方法的重载(依据方法签名)
public class OverLoadDemo1 {
public static void main(String[] args) {
show();
show("zhangsan");
show("lisi",23);
show(25,"wangwu");
}
public static void show(){
System.out.println("无参数show方法...");
}
public static void show(String name){
System.out.println("有参show(name)方法"+name);
}
public static void show(String name,int age){
System.out.println("有参show(name+age)"+name + age);
}
public static void show(int age,String name){
System.out.println("有参show(age+name)"+age+name);
}
}
5 判断一个数是否是质数
import java.util.Scanner;
public class PrimeNumber1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("输入一个数判断是否是质数: ");
int num = scan.nextInt();
System.out.println(isPrime(num));
//求0-n的所有质数
printPrimes(100);
}
public static boolean isPrime(int num){
boolean flag = true;
for (int i=2;i<num/2;i++){
if (num%i==0){
flag = false;
break;
}
}
return flag;
}
public static void printPrimes(int rang){
for (int num=2;num<rang;num++){
boolean flag = isPrime(num);
if (flag){
System.out.print(num+" ");
}
}
}
}
6 如何输入验证码
public class VeriCodeDemo1 {
public static void main(String[] args) {
System.out.println("输出4为验证码");
System.out.println(veriCode(4));
}
public static String veriCode(int len){
String code = "";
char[] chs ={'a','b','c','d','e','f','g','h','i','g','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9'};
Random ran = new Random();
for (int i=0;i<len;i++){
code+=chs[ran.nextInt(chs.length)];
}
return code;
}
}
浙公网安备 33010602011771号