1.键盘输入一个数,打印出质因数分解的过程结果
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int num = sc.nextInt();
System.out.println(num+"=");
for (int i = 2; i<num;i++){
while(i<=num){
if (i==num){
System.out.println(""+num);
}
else if (num%i == 0){
System.out.println("*"+i);
num = num/i;
}
else{
break;
}
}
}
System.out.println(""+num);
}
2.键盘输入一个数判断是否是质数(素数)
public static void main(String[] args) {
for (int i =2;i<=100;i++){
for (int j = 1;j<i;j++){
if (i%j==0){
System.out.println("是素数");
break;
}
if(i==j){
System.out.println("不是素数");
}
}
}
}
3.已知斐波那契数列 1 1 2 3 5 8… ,求出第10项的值
/当n大于2时,f(n) = f(n-1) + f(n-2)
public static void main(String[] args) {
int n1 = 1;
int n2 = 1;
int n3 = 0;
for (int i = 3;i<=10;i++){
n3 = n1+n2;
n1 = n2;
n2 = n3;
System.out.println(n3);
}
}
4.键盘输入一个字符串,统计数字,字母,中文字符在此字符串中出现的次数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.next();
int countLetter = 0;
int countChinese = 0;
int countNumber = 0;
for (int i = 0;i<str.length();i++){
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
//-- 0 48 | 9 57
//-- 数字
countNumber ++;
}
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
//-- 字母
countLetter ++;
}
if (c >= '\u4e00' && c<= '\u9fa5') {
//-- 中文
countChinese ++;
}
}
System.out.println("字母数量:" + countLetter);
System.out.println("数字数量:" + countNumber);
System.out.println("中文数量:" + countChinese);
}
5.万年历(重点)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//录入年和月
System.out.println("请输入年份:");
int year = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
//计算总天数
int days = 0;
int totalYear = 0;//1900年到 输入年-1的总数
int current = 0;
int monthDay = 0;
for (int i = 1900;i<totalYear;i++){
//判断闰年
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
totalYear += 366;
} else {
totalYear += 365;
}
}
//上面不带输入的年份所以需要输入的年份的月份+1的天数
for (int i = 1;i < month;i++){
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
current += 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
current += 30;
} else {
// -- 2月
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
current += 29;
} else {
current += 28;
}
}
}
//总天数
days = totalYear + current;
days += 1;
//计算输出月是星期几
int weekdays = days % 7;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
monthDay += 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
monthDay = 30;
} else {
// -- 2月
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
monthDay = 29;
} else {
monthDay = 28;
}
}
System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
// -- 补充空格进来 用 7-week的差值就是要补充的空格数量!
for (int i = 1; i < weekdays; i++) {
System.out.print("\t");
}
// -- 打印日期 只打印当前这个月的天数
for (int i = 1; i <= monthDay; i++) {
System.out.print(i + "\t");
if ((i + weekdays - 1) % 7 == 0) {
System.out.println();
}
}
sc.close();
}
6.万年历2
public static void main(String[] args){
//思路:
//1、构建Scanner扫描器类的对象
Scanner input = new Scanner(System.in);
//2、从控制台接收用户录入的年份、月份
System.out.println("输入年");
int year = input.nextInt();
System.out.println("输入月");
int month = input.nextInt();
//3、获得1900年距离当前年份上一年的总天数
int totalDays = getFromNowYearTo1900TotalDays(year);
//4、获得当前年份所经过的天数
int totalDaysThisYear = getNowYearPassedTotalDays(year,month);
//5、求得给定月份第一天的星期数
int week = (totalDays + totalDaysThisYear +1) % 7;//星期日是,week = 0
//6、格式化输出日历
formatCalendarOutput(week,year,month);
}
/**
*格式化输出日历
*@param week:当前月份第一天的星期数
*@param year:当前年份
*@param month:当前月份
*/
public static void formatCalendarOutput(int week,int year,int month){
int cnt = 0;//计数器,记录空白数和日期数的和
//1) 打印表头
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
//2)打印空白(观察星期与之前空个数之间的规律)
for(int i=1;i<= week;i++){
System.out.print("\t\t");
cnt++;
}
//3) 打印日历
for(int i=1;i<=getNowMonthDays(year,month);i++){
System.out.print(i+"\t\t");
cnt++;
//若记录空白数和日期数的和是七的倍数,应该换行输出
if(cnt % 7 == 0){
System.out.println();
}
}
}
/**
*判断给定的年份是否为闰年
*@param year:给定的年份
*@return true:闰年;false:平年
*/
public static boolean isLeapYear(int year){
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
/**
*根据参数指定的年份,月份,求出当前月的总天数
*@param year:年份
*@param month:月份
*@return 月的总天数
*/
public static int getNowMonthDays(int year,int month){//year:设计该参数的原因,2月份根据年份是否是闰年来确定其天数的
switch(month){
case 2:
return isLeapYear(year) ? 29 : 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
/**
*获得当前年份的上一年距离1900年所经过的总天数
*@param year 当前年份
*@return 总天数
*/
public static int getFromNowYearTo1900TotalDays(int year){
int totalDays = 0;
for(int i = 1900; i< year; i++){//i:年份
totalDays += isLeapYear(i) ? 366 : 365;
}
return totalDays;
}
/**
*求出当前年份经过的总天数(从当前年的1月1日到给定月份的上一个月最末一天)
*@param year:年份
*@param month:月份
*@return 总天数
*/
public static int getNowYearPassedTotalDays(int year,int month){
int totalDays = 0;
for(int i=1;i< month;i++){//i:月份
totalDays += getNowMonthDays(year,i);
}
return totalDays;
}
7.十进制转二进制
public static void main(String[] args) {
// 十进制转二进制
Scanner sc = new Scanner(System.in);
System.out.println("输入一个数:");
int i = sc.nextInt();
String str = "";
while(i!=0){
int k = i % 2;//取余数
str += k;//赋值给str
i = i/2;//
}
System.out.println(str);
}
8.判断一个数是否是回文数 aba abba 100001 人人为我,我为人人
public static void main(String[] args) {
//判断一个数是否是回文数 aba abba 100001 人人为我,我为人人
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符:");
String str = sc.next();
String str1 = "";
for (int i = str.length()-1; i >= 0; i--){
str1+=str.charAt(i);
}
if (str.equals(str1)){
System.out.println("是回文数");
}
else if (!str.equals(str1)){
System.out.println("不是回文数");
}
}