流程控制
成绩等级判断
要求:输入一个百分制成绩(0-100),输出对应的等级
90-100:A
80-89:B
70-79:C
60-69:D
0-59:E
其他:提示输入错误
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
int score;
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的成绩:");
score = scan.nextInt();
if(score < 0 || score > 100) {
System.out.println("wrong");
}else if(score >= 90) {
System.out.println("A");
}else if(score >= 80) {
System.out.println("B");
}else if(score >= 70) {
System.out.println("C");
}else if(score >= 60) {
System.out.println("D");
}else {
System.out.println("E");
}
}
}
//边界值必须从大到小——限定范围从小到大
//break必须有,确保只输出一个正确的等级
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
int score;
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的成绩:");
score = scan.nextInt();
// 定义边界值和对应等级
int[] boundaries = {90, 80, 70, 60, 0};
String[] grades = {"A", "B", "C", "D", "E"};
if(score < 0 || score > 100) {
System.out.println("wrong");
} else {
for(int i = 0; i < boundaries.length; i++) {
if(score >= boundaries[i]) {
System.out.println(grades[i]);
break;
}
}
}
scan.close(); // 关闭Scanner,释放资源
}
}
季节判断
要求:输入月份(1-12),输出对应的季节
3-5月:春季
6-8月:夏季
9-11月:秋季
12-2月:冬季
其他:提示输入错误
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
int month;
Scanner scan = new Scanner(System.in);
System.out.println("请输入月份(1-12):");
month = scan.nextInt();
// 定义一个数组,索引0-11对应1-12月的季节
String[] seasons = {"Winter", "Winter", "Spring", "Spring", "Spring",
"Summer", "Summer", "Summer", "Autumn", "Autumn",
"Autumn", "Winter"};
if(month >= 1 && month <= 12) {
System.out.println(seasons[month - 1]);
} else {
System.out.println("wrong");
}
scan.close();//不要忘记关闭输入
}
}
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
int month;
Scanner scan = new Scanner(System.in);
System.out.println("请输入月份(1-12):");
month = scan.nextInt();
String season = switch(month) {
case 3, 4, 5 -> "Spring";
case 6, 7, 8 -> "Summer";
case 9, 10, 11 -> "Autumn";
case 12, 1, 2 -> "Winter";
default -> "wrong";
};
System.out.println(season);
scan.close();
}
}
简单计算器
要求:输入两个数字和一个运算符(+、-、*、/),输出计算结果 注意:除数为0时要给出错误提示
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
double a = scan.nextDouble();
double b = scan.nextDouble();
scan.nextLine(); // 消耗掉换行符
String op = scan.nextLine();
System.out.println(
op.equals("+") ? a + b :
op.equals("-") ? a - b :
op.equals("*") ? a * b :
op.equals("/") ? (b == 0 ? "wrong" : a / b) :
"wrong"
);
scan.close();
}
}
nextLine()会读取上一个输入后的换行符,导致如果输入的String类型的变量可能变成空字符串。
double a = scan.nextDouble(); // 输入 5,按下回车,缓冲区有:5\n
double b = scan.nextDouble(); // 输入 3,按下回车,缓冲区有:5\n3\n
String op = scan.nextLine(); // 读取的是 3 后面的 \n,而不是用户输入的运算符
闰年判断
要求:输入年份,判断是否为闰年 闰年条件:能被4整除但不能被100整除,或者能被400整除
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();
System.out.println(year + "年是" +
(((year%4==0&&year%100!=0)||(year%400==0))? "LeapYear" : "NotLeapYear"));
scan.close();
}
}
循环练习
乘法表
要求:打印9×9乘法表 输出格式:
1×1=11×2=2 2×2=41×3=3 2×3=6 3×3=9
public class Person {
public static void main(String args[]) {
for(int i=1;i<=9;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j + "*" + i + "=" + j*i + " ");
}
System.out.println();
}
}
}
println输入自带换行,print输入不会换行
数字统计
要求:输入一串整数(以0结束),统计:
- 正数的个数
- 负数的个数
- 所有数的总和
- 平均值(保留两位小数)
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int positiveCount = 0;
int negativeCount = 0;
int sum = 0;
int count = 0;
System.out.println("请输入整数");
while(true) {
int num = scan.nextInt();
if(num == 0) {
break;
}
if(num > 0) {
positiveCount++;
sum += num;
}else {
negativeCount++;
sum += num;
}
count++;
}
System.out.print(positiveCount + "\n" + negativeCount + "\n" + sum);
if(count > 0) {
double average = (double)sum / count;
System.out.printf("平均值:%.2f\n ",average);
}
scan.close();
}
}
连续输入
最大值最小值
要求:输入10个整数,找出其中的最大值和最小值 不允许使用数组(用循环和变量实现)
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入10个整数:");
int max = scan.nextInt();
int min = max;
for(int i = 1; i < 10; i++) {
int num = scan.nextInt();
if(num > max) max = num;
if(num < min) min = num;
}
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
scan.close();
}
}
水仙花数
要求:找出100-999之间的所有水仙花数 水仙花数定义:各位数字的立方和等于该数本身 例如:153 = 1³ + 5³ + 3³
public class Person {
public static void main(String args[]) {
for(int i=100;i<1000;i++) {
int hun = i/100;
int ten = i/10%10;
int one = i%10;
if(i == Math.pow(hun,3) + Math.pow(ten,3) + Math.pow(one, 3)) {
System.out.print(i + " ");
}
}
}
}
圆形打印(嵌套循环)
打印矩形
要求:输入行数和列数,打印由
*组成的矩形
// 例如:行数=3,列数=5
// *****
// *****
// *****
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("length = ");
int length = scan.nextInt();
System.out.println("width = ");
int width = scan.nextInt();
for(int i=0;i<length;i++) {
for(int j=0;j<width;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
打印直角三角形
要求:输入行数,打印直角三角形 例如:行数=5
// *
// **
// ***
// ****
// *****
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("length = ");
int length = scan.nextInt();
for(int i=1;i<=length;i++) {
for(int j=0;j<i;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
打印菱形
要求:输入行数(奇数),打印菱形 例如:行数=5
// *
// ***
// *****
// ***
// *
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入行数:");
int length = scan.nextInt();
while(length%2==0) {
System.out.println("请重新输入:");
length = scan.nextInt();
}
int mid = length/2;
for(int i=0;i<mid+1;i++) {
for(int j=0;j<mid-i;j++) {
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++) {
System.out.print("*");
}
System.out.println();
}
for(int i = mid-1; i>=0; i--) {
for(int j=0; j<mid-i; j++) {
System.out.print(" ");
}
for(int j=0; j<2*i+1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
打印空心矩形
要求:输入行数和列数,打印空心矩形 例如:行数=4,列数=6
// ****** // * * // * * // ******
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入行数:");
int length = scan.nextInt();
System.out.println("请输入列数:");
int width = scan.nextInt();
for(int i=0;i<width;i++) {
System.out.print("*");
}
System.out.println();
for(int i=0;i<length-2;i++) {
System.out.print("*");
for(int j=0;j<width-2;j++) {
System.out.print(" ");
}
System.out.print("*");
System.out.println();
}
for(int i=0;i<width;i++) {
System.out.print("*");
}
}
}
数组遍历
数组求和
要求:给定数组 int[] arr = {23, 45, 67, 12, 89, 34}; 使用for-each循环计算所有元素的总和
public class Person {
public static void main(String args[]) {
int arr[] = {23,45,67,12,89,34};
int sum = 0;
for(int n: arr) {
sum += n;
}
System.out.println(sum);
}
}
查找元素
要求:给定数组 String[] names = {“张三”, “李四”, “王五”, “赵六”, “钱七”}; 输入一个姓名,查找是否在数组中,并输出结果
import java.util.Scanner;
public class Person {
public static void main(String args[]) {
String names[] = {"张三","李四","王五","赵六","钱七"};
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
boolean found = false;
for(String n : names) {
if(name.equals(n)) {
found = true;
break;
}
}
System.out.println(found ? "在其中" : "不在其中");
scan.close();
}
}
统计成绩
要求:给定数组 int[] scores = {85, 92, 78, 90, 88, 76, 95, 89}; 统计:
- 90分以上的人数
- 80-89分的人数
- 70-79分的人数
- 60-69分的人数
- 60分以下的人数
public class Homework {
public static void main(String args[]) {
int[] scores = {85,92,78,90,88,76,95,89};
int[] num = new int[5];
for(int n:scores) {
if(n<60) {
num[4]++;
}else if(n<70) {
num[3]++;
}else if(n<80) {
num[2]++;
}else if(n<90) {
num[1]++;
}else {
num[0]++;
}
}
for(int m:num) {
System.out.println(m);
}
}
}
二维数组遍历
// 要求:给定二维数组 int[][] matrix = { // {1, 2, 3, 4}, // {5, 6, 7, 8}, // {9, 10, 11, 12} // }; // 使用for-each循环打印所有元素,并计算所有元素的总和
public class Person {
public static void main(String args[]) {
int matrix[][] = {{1,2,3,4},
{5,6,7,8},{9,10,11,12}
};
for(int row[] : matrix) {//外层循环,每次取出一行
for(int num : row) {//内层循环,遍历这一行的每个元素
System.out.print(num + " ");
}
System.out.println();
}
}
}
1.穿越时空之门 - 蓝桥云课
- 进制的位权原理
- 每一个数都可以表示为各位数字与其位权的乘积之和
//十进制 1234 = 1×10³ + 2×10² + 3×10¹ + 4×10⁰ = 1×1000 + 2×100 + 3×10 + 4×1 = 1000 + 200 + 30 + 4//二进制 1011₂ = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 1×8 + 0×4 + 1×2 + 1×1 = 8 + 0 + 2 + 1 = 11₁₀ - 十进制转化为其他进制
- 原理:不断除以基数,取余数,直至商为0,将余数倒序排列
-
//十进制转化 String binary2 = Integer.toString(num, x); System.out.println(num + " 的x进制:" + binary2); - 题解
public class Homework { public int calculate(int x,int y) { int sum = 0; while(x > 0) { sum += x % y; x = x/y; } return sum; } public static void main(String[] args) { int index = 0; Homework hom = new Homework(); for(int i=1;i<2025;i++) { int sum1 = hom.calculate(i,2); int sum2 = hom.calculate(i,4); if(sum1 == sum2) { index++; } } System.out.println(index); }
2.握手问题 - 蓝桥云课
public class Homework {
public static void main(String args[]) {
int sum = 0;
for(int i=49;i>0;i--) {
sum += i;
}
for(int i=6;i>0;i--) {
sum -= i;
}
System.out.println(sum);
}
}
3.棋盘 - 蓝桥云课
import java.util.Scanner;
public class Homework {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] arr = new int[n][n];
for(int i=0; i<n;i++) {
for(int j=0;j<n;j++) {
arr[i][j] = 0;
}
}
for(int i=0;i<m;i++) {
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int x2 = scan.nextInt();
int y2 = scan.nextInt();
for(int j=x1-1;j<x2;j++) {
for(int k=y1-1;k<y2;k++) {
if(arr[j][k] == 0) {
arr[j][k] = 1;
}else{
arr[j][k] = 0;
}
}
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}
4.幸运数字 - 蓝桥云课
public class Homework {
public static boolean calculate(int x,int y) {
int sum = 0;
int result = x; // 算到最后x=0
while(x > 0) {
sum += x%y;
x = x/y;
}
return result%sum == 0;
}
public static void main(String args[]) {
int[] result = new int[2023];
int i=1;
int index = 0;
while(index < 2023) {
if(calculate(i,2) && calculate(i,8) && calculate(i,10) && calculate(i,16)) {
result[index] = i;
index++;
}
i++;
}
System.out.println(result[2022]);
}
}
5.英文字母 - 蓝桥云课
import java.util.Scanner;
public class Homework {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] word = {"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"};
System.out.println(word[n-1]);
}
}
6.3 的倍数 - 蓝桥云课
import java.util.Scanner;
public class Homework {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int index = 0;
if((a+b)%3==0) {
index++;
}
if((b+c)%3==0) {
index++;
}
if((a+c)%3==0) {
index++;
}
if(index != 0) {
System.out.print("yes");
}else {
System.out.print("no");
}
scan.close();
}
}
想求自己认真审下题
7.双阶乘 - 蓝桥云课
public class Homework {
public static void main(String args[]) {
int sum = 1;
for(int i=2021;i>=1;i-=2) {
sum = sum * i % 100000;
}
System.out.println(sum);
}
}
2021!!数字过大会溢出,所以采用%100000的方式来取模直接得出最后五位数字
9.浮点数 - 蓝桥云课
- 总位数为64位
- 1字节 = 8位
- 所以一个双精度浮点数占用8字节
4.偶串 - 蓝桥云课
import java.util.Scanner;
public class Homework {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int index = 0;
while(input>0) {
int sum = 0;
int ans = input;
while(ans>0) {
sum += ans%10;
ans = ans/10;
}
input -= sum;
index++;
}
System.out.println(index);
}
}
7.判断蓝桥 - 蓝桥云课
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
scanner.close();
if(s.equalsIgnoreCase("lanqiao")){
System.out.println("yes");
}
else {
System.out.println("no");
}
}
}
8.变换数组 - 蓝桥云课
import java.util.Scanner;
public class Lanqiao {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = scan.nextInt();
}
int m = scan.nextInt();
for(int k=0;k<m;k++) {
for(int i=0;i<n;i++) {
a[i] *= Integer.bitCount(a[i]);
}
}
for(int i=0;i<n;i++) {
System.out.print(a[i] + " ");
}
}
}
哈哈没想到吧,有个东西叫Integer.bitCount