经典题目
判断闰年
/**
* 判断闰年 1
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int year = scanner.nextInt();
if (year % 100 == 0){
if (year % 400 == 0){
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}
}else{
if (year % 4 == 0) {
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}
}
}
}
/**
* 判断闰年 2
* @param args
*/
public static void main2(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int year = scanner.nextInt();
if ((year % 4 == 0 || year % 100 != 0) && year % 400 == 0){
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}
}
}
swich 语句
/**
* switch 语句
* 面试问题: 不能做switch参数的类型有哪些?
* float long double boolean
* @param args
*/
public static void main(String[] args) {
int a = 1;
switch (a) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("输入错误!");
break;
}
}
面试问题: 不能做switch参数的类型有哪些?
float long double boolean
计算1-100的奇数和, 1-100的偶数和,1 -100的和
/**
*计算1-100的奇数和, 1-100的偶数和,1 -100的和
* @param args
*/
public static void main(String[] args) {
int sum_j = 0;
int sum_o = 0;
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0){
sum_o += i;
}if (i % 2 == 1) {
sum_j += i;
}
sum += i;
}
System.out.println("奇数之和: " + sum_j);
System.out.println("偶数之和: " + sum_o);
System.out.println("1--100之和: " + sum);
}
计算5的阶乘
/**
* 计算5的阶乘
* @param args
*/
public static void main(String[] args) {
int i = 1;
int ret = 1;
while (i <= 5) {
ret = ret * i;
i++;
}
System.out.println("5的阶乘: " + ret);
}
计算1! + 2! + 3! + 4! + 5!
/**
* 计算1! + 2! + 3! + 4! + 5!
* @param args
*/
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
int ret = 1;
for (int j = 1; j <= i; j++) {
ret = ret * j;
}
sum += ret;
}
System.out.println(sum);
}
写出1-100之间既能被3整除也能被5整除的数, 使用continue
/**
* 写出1-100之间既能被3整除也能被5整除的数
* 使用continue
* @param args
*/
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 != 0 || i % 5 != 0) {
continue;
}
System.out.print(i + " ");
}
}
判断素数(三种方法)
// 法一:
public static void main(String[] args) {
int n = 17;
int i = 2;
for (; i < n; i++) {
if (n % i == 0) {
System.out.println(n +" 不是素数");
break;
}
}
if (i == n) {
System.out.println(n + "是素数");
}
}
// 法二:
public static void main(String[] args) {
int n = 17;
int i = 2;
for (; i <= n/2; i++) {
if (n % i == 0) {
System.out.println(n +" 不是素数");
break;
}
}
if (i > n/2) {
System.out.println(n + "是素数");
}
}
// 法三:
public static void main(String[] args) {
int n = 17;
int i = 2;
for (; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
System.out.println(n +" 不是素数");
break;
}
}
if (i > Math.sqrt(n)) {
System.out.println(n + "是素数");
}
}
写一个函数返回参数二进制中 1 的个数
/**
* 写一个函数返回参数二进制中 1 的个数
* @param args
*/
public static void main(String[] args) {
int n = 15;
int count = 0;
while(n != 0) {
if ((n & 1) != 0) {
count++;
}
n = n >>> 1;
}
System.out.println(count);
}
原理
n & 1 == (0或1)
public static void main(String[] args) {
int n = 15;
int count = 0;
while(n != 0) {
n = n & n-1;
count++;
}
System.out.println(count);
}
获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
/**
*获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
* @param args
*/
public static void main(String[] args) {
int n = 7;
System.out.print("偶数位: ");
for (int i = 31; i >= 1; i-=2) {
System.out.print(((n >> i) & 1)+" ");
}
System.out.println();
System.out.print("奇数位: ");
for (int i = 30; i >= 0; i-=2) {
System.out.print(((n >> i) & 1)+" ");
}
}
猜数字游戏
/**
* 猜数字游戏
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int randNum = random.nextInt(100);//生成[0, 100)的随机数
int count = 3;
// System.out.println(randNum);
while (count != 0){
System.out.println("请输入你想要猜的数字:> ");
int think = scanner.nextInt();
if (randNum > think){
System.out.println("猜小了");
}else if (randNum < think){
System.out.println("猜大了");
}else if (randNum == think) {
System.out.println("猜对了");
break;
}
count--;
System.out.println("您还有"+count+"次机会!");
}
}
用户实现登录
/**
* 模拟登录
* @param args
*/
public static void main(String[] args) {
int count = 3;
String mypassword = "123456";
while (count != 0) {
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
if (mypassword.equals(password)) {
System.out.println("密码正确!");
break;
}else {
count--;
System.out.println("密码错误, 你还有"+count+"次机会!");
if (count == 0) {
System.out.println("很遗憾机会已用尽!");
}
}
}
}
2022年9月27日22:31:03