1、输入一个字符,判断它是不是字母,是不是数字,是不是空格字符?
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 14:16
*/
public class CharJudge01 {
public static void main(String[] args) {
System.out.println("请输入字符,然后按回车键!");
Scanner s =new Scanner(System.in);
String str=s.nextLine();
while(str.length()>1)
{
System.out.println("请输入一个字符!");
str=s.next();
}
Pattern p1 = compile("[a-zA-Z]");
Pattern p2 = compile("[0-9]");
Pattern p3 = compile("[\\s]");
Matcher m1 = p1.matcher(str);
Matcher m2 = p2.matcher(str);
Matcher m3 = p3.matcher(str);
if(m1.find()) {
System.out.println("字母");
} else if(m2.find()) {
System.out.println("数字");
} else if(m3.find()) {
System.out.println("空格");
} else {
System.out.println("其他符号");
}
}
}
2、输入一个整数,判断它是偶数还是奇数?
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 15:00
*/
public class OddEven02 {
// 2.输入一个数字判断他是奇数还是偶数
public static void OddEven() {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个整数:");
try {
int Intnumber = s.nextInt();
if (Intnumber % 2 == 0) {
System.out.println("您输入的" + Intnumber + "是一个偶数!");
} else {
System.out.println("您输入的" + Intnumber + "是一个奇数!");
}
} catch (Exception e) {
System.out.println("您的输入有误!");
OddEven();
}
}
public static void main(String[] args) {
OddEven();
}
}
3、企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;
40万到60万之间时高于40万元的部分,可提成3%;
60万到100万之间时,高于60万元的部分,可提成1.5%;
高于100万元时,超过100万元的部分按1%提成。
从键盘输入当月利润I,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
import java.sql.SQLOutput;
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 15:19
* 3、企业发放的奖金根据利润提成。
* 利润(I)低于或等于10万元时,奖金可提10%;
* 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
* 20万到40万之间时,高于20万元的部分,可提成5%;
* 40万到60万之间时高于40万元的部分,可提成3%;
* 60万到100万之间时,高于60万元的部分,可提成1.5%;
* 高于100万元时,超过100万元的部分按1%提成。
* <p>
* 从键盘输入当月利润I,求应发放奖金总数?
* 程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
*/
public class axis03 {
public static void main(String[] args) {
System.out.println("请输入当月利润:");
Scanner s = new Scanner(System.in);
long I = s.nextLong();
int dw = 100000;
long bonus;
if (I <= dw) {
bonus = (long) (I * 0.1);
} else if (I < 2 * dw) {
bonus = (long) (dw * 0.1 + (I - dw) * 0.75);
} else if (I < 4 * dw) {
bonus = (long) (dw * 0.1 + dw * 0.75 + (I - 2 * dw) * 0.5);
} else if (I < 6 * dw) {
bonus = (long) (dw * 0.1 + dw * 0.75 + 2 * dw * 0.5 + (I - 4 * dw) * 0.3);
} else if (I < 10 * dw) {
bonus = (long) (dw * 0.1 + dw * 0.75 + 2 * dw * 0.5 + 2 * dw * 0.3 + (I - 6 * dw) * 0.15);
} else {
bonus = (long) (dw * 0.1 + dw * 0.75 + 2 * dw * 0.5 + 2 * dw * 0.3 + 4 * dw * 0.15 + (I - 10 * dw) * 0.1);
}
System.out.println("该月发放的奖金为:" + bonus);
}
}
4、已知某年某月,问这个月共有多少天?
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 16:09
* <p>
* 4、已知某年某月,问这个月共有多少天?
*/
public class MonthDay {
public static void main(String[] args) {
System.out.println("请输入年份:");
Scanner s1 = new Scanner(System.in);
int year = s1.nextInt();
System.out.println("请输入月份:");
Scanner s2 = new Scanner(System.in);
int month = s2.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.println(year + "年" + month + "月一共有31天");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println(year + "年" + month + "月一共有30天");
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "年" + month + "月一共有29天");
} else {
System.out.println(year + "年" + month + "月一共有28天");
}
}
}
}
5、已知某年某月某日,判断这一天是这一年的第几天?
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 16:16
* 5、已知某年某月某日,判断这一天是这一年的第几天?
*/
public class WhatDay {
public static void main(String[] args) {
System.out.println("请输入年份:");
Scanner s1 = new Scanner(System.in);
int year = s1.nextInt();
System.out.println("请输入月份:");
Scanner s2 = new Scanner(System.in);
int month = s2.nextInt();
System.out.println("请输入天份:");
Scanner s3 = new Scanner(System.in);
int day = s3.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (month == 1) {
System.out.println("这一天是当年第"+day+"天");
} else if (month == 2) {
System.out.println("这一天是当年第"+(31+day)+"天");
} else if (month == 3 || month == 5 || month == 7) {
System.out.println("这一天是当年第"+(31*(month-1)/2+30*(month-3)/2+29+day)+"天");
} else if (month == 4 || month == 6 || month == 8) {
System.out.println("这一天是当年第"+(31*(month)/2+30*(month-4)/2+29+day)+"天");
} else if (month == 9 || month == 11) {
System.out.println("这一天是当年第"+(31*(month+1)/2+30*(month-5)/2+29+day)+"天");
} else if (month == 10 || month == 12) {
System.out.println("这一天是当年第"+(31*(month)/2+30*(month-8)+29+day)+"天");
} else {
System.out.println("你输入的这天是tm的哪一天?");
}
} else {
if (month == 1) {
System.out.println("这一天是当年第"+day+"天");
} else if (month == 2) {
System.out.println("这一天是当年第"+(31+day)+"天");
} else if (month == 3 || month == 5 || month == 7) {
System.out.println("这一天是当年第"+(31*(month-1)/2+30*(month-3)/2+28+day)+"天");
} else if (month == 4 || month == 6 || month == 8) {
System.out.println("这一天是当年第"+(31*(month)/2+30*(month-4)/2+28+day)+"天");
} else if (month == 9 || month == 11) {
System.out.println("这一天是当年第"+(31*(month+1)/2+30*(month-5)/2+28+day)+"天");
} else if (month == 10 || month == 12) {
System.out.println("这一天是当年第"+(31*(month)/2+30*(month-8)+28+day)+"天");
} else {
System.out.println("你输入的这天是tm的哪一天?");
}
}
}
}
6、输入一个正整数,判断它是不是质数?
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入您要检测是否为质数的数:");
long number=scanner.nextLong();
long i;
for(i=2; i<=number; i++){
if(number%i==0){
if(number==i){
System.out.println(number+"是质数");
break;
}
else{
System.out.println(number+"不是质数");
break;
}
}
}
}
7、打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
例如:
153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 17:20
*/
public class Daffodil {
public static void main(String[] args) {
long sum, a, b, c;
System.out.println("请输入你要查找水仙花数的范围");
System.out.println("范围最小值:");
Scanner s1 = new Scanner(System.in);
long min = s1.nextLong();
System.out.println("范围最大值:");
Scanner s2 = new Scanner(System.in);
long max = s2.nextLong();
while (min < max) {
a = min / 100;//取出个位上的值
b = min % 100 / 10;//取出十位数的值
c = min % 10;//取出百位数的值
sum = a * a * a + b * b * b + c * c * c;
if (sum == min) {
System.out.println(min);
}
min += 1;
}
}
}
8、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问第10月的兔子总数为多少?
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21…
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 17:41
*/
import java.util.Scanner;
public class FibonacciSequence {
public static void main(String[] args) {
System.out.println("月份数:");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println("总数:"+"\n"+f(n));
}
public static int f(int n) {
if(n!=1&&n!=2) {
if(n!=3) {
return f(n-1)+f(n-2);
}
return 2;
}
else {
return 1;
}
}
}
9、猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
程序分析:采取逆向思维的方法,从后往前推断。
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 17:44
*/
public class MPeach {
public static void main(String[] args) {
int Num = 1 ;
System.out.println("猴子第十天吃的桃子数为="+Num);
for(int i = 1;i<10;i++) {
Num = (Num+1)*2;
System.out.println("第"+(10-i)+"天的桃子的数目为"+Num);
}
}
}
10、有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/21 17:46
*/
public class Sequence {
public static void Seq(){
double x=2,y=1;
double sum=0;
double t;
for(int i=0;i<20;i++){
sum+=x/y;
t=x;
x=x+y;
y=t;
}
System.out.println(sum);
}
public static void main(String[] args) {
Seq();
}
}