本篇博客是博主在学习Java语言过程中做过的一些题目,集合在这里分享给大家,并且会不定时更新,大家可以一起学习,共同进步!
输入两个整数a,b,求其中较大的值并输出。
参考代码:
public class TestDemo { public static int Max(int a,int b) { return a>b?a:b;//返回较大值 } public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(Max(a,b));//输出较大值 } }
写一个代码,输入一个整数,判断其奇偶性并输出。
参考代码:
public static void Print(int n) { if( n % 2 == 0) { System.out.println("偶数"); }else { System.out.println("奇数"); } } public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n = scanner.nextInt(); Print(n); }
输入一个年份,判断该年份是否为闰年。
普通闰年:公历年份是4的倍数的,且不是100的倍数。
世纪闰年:公历年份是整百数的,必须是400的倍数。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); if( (year % 100 != 0 && year % 4 == 0) || year % 400 == 0) { System.out.println(year+"是闰年!"); }else { System.out.println(year+"不是闰年!"); } scanner.close(); }
输入一个整数,求这个整数的阶乘。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int i = 1; int ret = 1; while (i <= n) { ret = ret * i; i++; } System.out.println(n+"的阶乘的值为"+ret); }
输入一个n,输出1到n各项整数的阶乘的和。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int j = 1; int sum = 0; while (j <= n) { //每一项的阶乘 int i = 1; int ret = 1; while (i <= j) { ret = ret * i; i++; } sum = sum + ret;//加起来 j++;//+1 } System.out.println(sum); }
在0-100中找出既能被三整除又能被五整除的数并输出。
参考代码:
public static void main(String[] args) { int i = 1; while (i <= 100) { if( i % 15 != 0) { i++; continue; } System.out.println(i); i++; } }
随机生成一个1-100的数字,让玩家输入来猜,正确退出,不正确继续输入。
参考代码:
//记得引import java.util.*; public static void main(String[] args) { Random random = new Random(); int rand = random.nextInt(100); //System.out.println("rand "+rand);//作弊 Scanner scanner = new Scanner(System.in); while (true) { System.out.print("请输入你要猜的数字:"); int n = scanner.nextInt(); if(n < rand) { System.out.println("猜小了!"); }else if(n == rand) { System.out.println("恭喜你,猜对了!"); break; }else { System.out.println("猜大了!"); } } }
输入一个整数,判断其是否为素数。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); 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 + " 是素数!"); } }
输入两个整数,输出其最大公约数。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入两位整数:>"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = a%b; while (c != 0) { a = b; b = c; c = a%b; } System.out.println("最大公约数是"+b); }
计算1-1/2+1/3-1/4+1/5…+1/99-1/100的值。
参考代码:
public static void main(String[] args) { double sum = 0.0; int flg = 1; for (int i = 1; i <= 100 ; i++) { sum = sum + 1.0/i * flg; flg = -flg; } System.out.println("1-1/2+1/3-1/4+1/5...+1/99-1/100的值为"+sum); }
求1-100之间数字中,9的个数,并输出。
参考代码:
public static void main(String[] args) { int count = 0; for (int i = 1; i <= 100; i++) { if(i % 10 == 9) { count++; } if(i / 10 == 9) { count++; } } System.out.println(count); }
输出一个九九乘法表。
参考代码:
public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i+"*"+j+"="+i*j+" ");//\t } System.out.println(); } }
编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输入,最多输入三次。三次均错,则提示退出程序。
参考代码:
public static void main(String[] args) { login(); } public static void login() { Scanner scanner = new Scanner(System.in); int count = 3; while (count != 0) { System.out.println("请输入你的密码:"); String password = scanner.nextLine(); //使用equals()方法判断字符串的内容是否相同 if (password.equals("123456")) { System.out.println("登录成功了!"); break; } else { count--; if (count == 0) { System.out.println("密码错误且机会已经用完,程序关闭!"); } else { System.out.println("密码错误!你还有 " + count + "次机会!"); } } } }
一个数组中的所有元素都出现了两次,只有一个元素出现了一次,找到这个元素并输出。
参考代码:
public static void main(String[] args) { int[] array = {1,2,3,2,1}; int sum = array[0]; for (int i = 1;i < array.length;i++) { sum = sum ^ array[i]; //^上之后相同的互相消为0 } System.out.println(sum); }
调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。
参考代码:
public static void main(String[] args) { int[] array = {1,2,3,4,5}; int left = 0; int right = array.length-1; while (left < right) { while (left < right && array[left] % 2 != 0) { left++; } while (left < right && array[right] % 2 == 0) { right--; } int tmp = array[left]; array[left] = array[right]; array[right] = tmp; } for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } }
输入一个值,求出1到这个值的所有自幂数。(水仙花数题的plus版本)
如果在一个固定的进制中,一个n位自然数等于自身各个数位上数字的n次幂之和,则称此数为自幂数。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextInt(); findNum(n); } public static void findNum(long n) { //计算数字的位数 for (long i = 1; i <= n; i++) { long tmp = i; int count = 0;//数字的位数 while (tmp != 0) { count++; tmp /= 10; } tmp = i; long sum = 0;//自幂的和 while (tmp != 0) { sum += Math.pow(tmp%10,count); tmp /= 10; } if(sum == i) { System.out.println(i); } } }
输入一个整数,逆序输出这个整数的每一位。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); print(n); } public static void print(int n) { while (n != 0) { System.out.print(n%10+" "); n /= 10; } }
输入一个十进制的数,输出其二进制形式时1的个数。
参考代码:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); System.out.println(numOfOne(n)); } public static int numOfOne(int n) { int count = 0; while (n != 0) { if((n & 1) == 1) { count++; } n = n >>>1; } return count; }