public class Checkerboard {
public static void main(String[] args) {
int size = 8;
//定义行,和初始化
int row = 0;
//定义列,和初始化
int col = 0;
for (int i = 1; i <= size * size; i++) {
row = (i - 1) / size + 1;
col = (i - 1) % size + 1;
if ((row % 2 == 1 && col % 2 == 1) || (row % 2 == 0 && col % 2 == 0)) {
System.out.print("□");
} else {
System.out.print("⬛");
}
if (col == size) {
System.out.println();
}
}
}
}
三角型
public class Demo03 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i - j);
}
System.out.println();
}
}
}
倒转直角三角形带*
public class Demo04 {
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
倒转直角三角形带数字
public class Demo05 {
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
倒转直角三角形带数字反转
public static void main(String[] args) {
for (int i = 1; i < 6; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print(i - j);
}
System.out.println();
}
}
}
百钱买百鸡
/**
* 5、公鸡5文钱一只,母鸡3文钱一只, 小鸡3只-文钱,朋100文钱买一百只鸡,
* 其中公鸡,母鸡,小鸡都必须要有,间公鸡,母鸡,小鸡要买多少只刚好谈足100文钱。
*/
public class Demo09 {
public static void buyChickens() {
int count = 0;
// 定义公鸡的数量
for (int cock = 0; cock <= 20; cock++) {
// 定义母鸡的数量
for (int hen = 0; hen <= 33; hen++) {
//获取小鸡的数量
int chicken = 100 - cock - hen;
if (5 * cock + 3 * hen + chicken / 3 == 100) {
count++;
System.out.printf(String.format("方案 %d: 公鸡 %d 只,母鸡 %d 只,小鸡 %d 只%n", count, cock, hen, chicken));
}
}
}
System.out.printf("总共有 %d 种购买方案。%n", count);
}
public static void main(String[] args) {
buyChickens();
}
}
实心菱形
public class Demo06_PrintDiamond {
public static void printDiamond(int height) {
// 打印上半部分
for (int i = 0; i < height; i++) {
// 打印空格,使星号居中对齐
for (int j = 0; j < height - i - 1; j++) {
System.out.print(" ");
}
// 打印星号
for (int g = 0; g < 2 * i + 1; g++) {
System.out.print("*");
}
System.out.println();
}
// 打印下半部分
for (int i = height - 2; i >= 0; i--) {
// 打印空格,使星号居中对齐
for (int j = 0; j < height - i - 1; j++) {
System.out.print(" ");
}
// 打印星号
for (int g = 0; g < 2 * i + 1; g++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形的高度(高度必须是大于等于3的奇数): ");
int height = scanner.nextInt();
printDiamond(height);
}
}
空心菱形
public class Demo07_PrintDiamond {
public static void printHollowDiamond(int height) {
// 打印上半部分
for (int i = 0; i < height; i++) {
// 打印空格,使星号居中对齐
for (int j = 0; j < height - i - 1; j++) {
System.out.print(" ");
}
// 打印星号
for (int g = 0; g < 2 * i + 1; g++) {
if (g == 0 || g == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 打印下半部分
for (int i = height - 2; i >= 0; i--) {
// 打印空格,使星号居中对齐
for (int j = 0; j < height - i - 1; j++) {
System.out.print(" ");
}
// 打印星号
for (int g = 0; g < 2 * i + 1; g++) {
if (g == 0 || g == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入空心菱形的高度(高度必须是大于等于3的奇数): ");
int height = scanner.nextInt();
printHollowDiamond(height);
}
}
1000~ 5000之间有多少整数,其各位数字之和为5,分别是哪些数(例如整数2003的各位数字之和为 2+0+0+3 ,等于5)),并统计满足条件的整数有多少个。
public class Demo04_SumDivisibleByFive {
/**
* 控制台输入,打印显示
*/
public static void InputRange() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入最低范围:");
Integer MinIndex = scanner.nextInt();
System.out.print("请输最高范围:");
Integer MaxIndex = scanner.nextInt();
// 计数器,计算统计符合条件的个数
int count = 0;
for (int num = MinIndex; num <= MaxIndex; num++) {
int sum = sumOfDigits(num);
if (sum == 5) {
System.out.println("符合条件的数:" + num);
count++;
}
}
System.out.println("符合条件的数的个数:" + count);
}
/**
* 分解1000-9999的整数,并将每一位进行求和操作
*/
public static int sumOfDigits(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
public static void main(String[] args) {
InputRange();
}
}
打印100 - 999中不能被7整除又不包含7的数
public class Demo08_PrintNotSeven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入最低范围:");
Integer MinIndex = scanner.nextInt();
System.out.print("请输最高范围:");
Integer MaxIndex = scanner.nextInt();
printNumbers(MinIndex, MaxIndex);
}
/**
* 打印100 - 999中不能被7整除又不包含7的数
*/
public static void printNumbers(Integer MinIndex, Integer MaxIndex) {
for (int num = MinIndex; num <= MaxIndex; num++) {
if (isExcludedNumber(num)) {
System.out.println(num);
}
}
}
/**
* 判断一个数是否不能被7整除且不包含7
*/
public static boolean isExcludedNumber(int num) {
return num % 7 != 0 && !containsDigit(num, 7);
}
/**
* 判断一个数是否包含指定的数字
*/
public static boolean containsDigit(int number, int digit) {
while (number > 0) {
if (number % 10 == digit) {
return true;
}
number /= 10;
}
return false;
}
}
书写任意一个任意位数的整数,求出它是几位数
public class Demo01_AnyNumberOfInteger {
public Integer CalculatorDigit(long nextLong) {
if (nextLong == 0) {
return 1;
}
int count = 0;
while (nextLong != 0) {
count++;
nextLong = nextLong / 10;
}
return count;
}
public void InputAnyIntNumber() {
Scanner scanner = new Scanner(System.in);
System.out.println("请用户输入任意位数的整数:");
long nextLong = scanner.nextLong();
Integer digit = CalculatorDigit(nextLong);
System.out.println("位数:" + digit);
}
public static void main(String[] args) {
new Demo01_AnyNumberOfInteger().InputAnyIntNumber();
}
}
从控制台输入一个正整数,实现反转,如:输入的是123,打印的结果为321
public class Demo03_InvertNumber {
/**
* 进行数据反转操作
*
* @param number
*/
public Integer InvertNumber(Integer number) {
Integer FinalNumber = 0;
while (number != 0) {
int num1 = number % 10;
FinalNumber = FinalNumber * 10 + num1;
number = number / 10;
}
return FinalNumber;
}
/**
* 控制台输入操作
*/
public void InputNumber() {
Scanner scanner = new Scanner(System.in);
System.out.print("请用户输入一个正整数:");
int inputNumber = scanner.nextInt();
System.out.println("原数为:" + inputNumber);
Integer invertNumber = InvertNumber(inputNumber);
System.out.println("反转后的数为:" + invertNumber);
}
public static void main(String[] args) {
new Demo03_InvertNumber().InputNumber();
}
}
编写程序,输出公元1800年到2000年所有闰年的年号,每输出3个年号换一行
public class Demo04_OutputYearNumber {
public static void main(String[] args) {
OutputYear();
}
/**
* 控制台输出年份
*/
private static void OutputYear() {
int Count = 0;
for (int year = 1800; year < 2000; year++) {
if (isLeapYear(year)) {
if (Count % 3 == 0 && Count != 1 && Count != 0 && Count >= 3) {
System.out.println();
}
System.out.print("年号:" + year + "\t");
Count++;
}
}
}
/**
* 判断是否时闰年
*/
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
5、猴子第一天摘下若干个桃子,当即吃了一半,还不够,又多吃了一个,当第二天早上将剩下的桃子吃掉一半,又多吃了一个。 以后每天早上都吃了前一天的一半多一个,到第10天早上想再吃时,就只剩下一个桃子了,求第一天摘了多少个桃子
public class Demo05_MonkeysPickingPeaches {
public static void main(String[] args) {
int day = 1;
int peachCount = 1;
while (day < 10) {
peachCount = (peachCount + 1) * 2;
day++;
}
System.out.println("第一天摘了 " + peachCount + " 个桃子");
}
}
从键盘输入学号,然后输出学号中百位数字是3的学号,输入0时结束循环
public class Demo06_ScannerInputStudentNo {
public static void main(String[] args) {
boolean Identifier = true;
while (Identifier) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个学号:");
int nextInt = scanner.nextInt();
if ((nextInt / 100) % 10 == 3) {
System.out.println("学号中百位数字是3的学号:"+nextInt);
}
if (nextInt == 0) {
Identifier = false;
}
}
}
}
每个苹果0.8元,第一天买2个苹果,第2天开始,每天买前一天的2倍,直到购买的苹果个数达到不超过100的最大值,编程计算每天平均花多少钱
public class Demo08_ApplePurchase {
public static void main(String[] args) {
double totalCost = calculateTotalCost();
System.out.println("每天平均花费:" + totalCost / 10 + " 元");
}
/**
* 计算苹果花费多少钱
*/
public static double calculateTotalCost() {
double cost = 0.8; // 每个苹果的价格
double totalCost = 2 * cost; // 第一天购买2个苹果
for (int day = 2, apples = 2; day <= 10 && apples <= 100; day++, apples *= 2) {
totalCost += apples * cost;
}
return totalCost;
}
}
有5个人坐在一起,问第5个人多少岁,他说比第4个大2岁问第四个他说比第三个大两岁,问第三个他说比第2个大2岁,问第2个他说比第一个人大2岁,问最后一个,他说他10岁,请问第5个人多大。
public class Demo09_AgeProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要计算几个人的年龄:");
Integer yearNumber = scanner.nextInt();
int age = findFifthPersonAge(yearNumber);
System.out.println("第"+yearNumber+"个人的年龄是:" + age);
}
/**
* 计算前几个的年龄
*/
public static int findFifthPersonAge(Integer yearNumber) {
int age = 10;
for (int i = 1; i <= yearNumber - 1; i++) {
age += 2; // 每个人比前一个人大2岁
}
return age;
}
}
求出3位数中,各位数之和能被12整除的数并统计出它们的个数
public class Demo10_SumDivisibleByTwelve {
public static void main(String[] args) {
int count = 0;
for (int num = 100; num <= 999; num++) {
int sum = sumOfDigits(num);
if (sum % 12 == 0) {
System.out.println("符合条件的数:" + num);
count++;
}
}
System.out.println("符合条件的数的个数:" + count);
}
/**
* 分解100-999的整数,并将每一位进行求和操作
*/
public static int sumOfDigits(int number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
}
求1+2!+3!+...+10!的和
public class Demo01_Factorial {
/**
* 计算阶乘
*/
public static int factorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
/**
* 计算求和
*/
public static int calculateSum(int num) {
int totalSum = 0;
for (int i = 1; i <= num; i++) {
totalSum += factorial(i);
}
return totalSum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要计算的多少项:");
int num = scanner.nextInt();
int result = calculateSum(num);
System.out.printf("1 + 2! + 3! + ... + %d! 的和为:%d%n", num, result);
}
}