1.出现9的次数(0-100中)
public class TestDemo {
public static void main(String[] args) {
//求0-100中9出现的次数
int i=0;
int count=0; //利用count进行计数
for(i=0;i<=100;i++){
if(i%10==9){
count++;
}
if(i/10==9){//此处不能用else if(因为99有两个9需要被计算两次)
count++;
}
}
System.out.println("0-100中9出现的次数为:"+count);
}
}
2.输出1000-2000里的全部闰年
public class TestDemo {
public static void main(String[] args) {
System.out.println("1000-2000中闰年如下:");
int i = 1000;
for (i = 1000; i <= 2000; i++) {
//加括号更好理解;前者为公历闰年;后者为世纪闰年
if (((i % 4 == 0) && (i % 100) != 0) || (i % 400 == 0)) {
System.out.println(i);
}
}
}
}
3.打印素数(0-100内的所有素数)
public class TestDemo {
public static void main(String[] args) {
System.out.println("素数如下:");
int i=1;
int j=1;
for(i=1;i<=100;i++){
int count=0;
for(j=1;j<=i;j++){
if(i%j==0){
count++;
}
}
if(count==2){
//一个素数只有1和它本身两个因数;
//上面的循环也是从1开始到其本身结束,与此处形成对应
System.out.println(i);
}
}
}
}
4.输入一个数,判断其是否为素数
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
System.out.println("请输入一个数并判断其是否为素数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
suShu(n);
}
public static void suShu(int x) {
int i = 2;
for (i = 2; i < x; i++) {
if (x % i == 0)
System.out.println(x+"不是素数");
}
System.out.println(x + "是素数");
}
}
5.打印X型
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
System.out.println("打印X型:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
daYin(n);
}
public static void daYin(int x) {
int i = 0;
int j = 0;
for (i = 0; i < x; i++) {
for (j = 0; j < x; j++) {
if (i == j) {
System.out.println("*");
} else if (i + j
== x - 1) {
System.out.println("*");
}else
System.out.println(" ");
}
}
System.out.println();}
}
6.计算分数
public class TestDemo {
public static void main(String[] args) {
System.out.println("请计算1-1/2+1/3-1/4+1/5+...+...1/99-1/100的结果");
int i=1;
double sum=0.0;
int flg=1;
for(i=1;i<=100;i++){
sum=sum+1.0/i*flg;//先思考连加的形式,再在加的基础上更改成为减
flg=-flg;
}
System.out.println(sum);
}
}
7.水仙花(输入一个整数,使它满足水仙花的规律;例如:153=1^3+5^3+3^3)
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
System.out.println("请输入一个数,求得0-这个数所有满足水仙花的数如下:");
//求是几位数,利用count进行计数
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
findNum(n);
}
public static void findNum(int n) {
for (int i = 0; i <= n; i++) {
//count从1开始是因为数至少都是一位数
int count = 1;
int tmp = i;
while (tmp / 10 != 0) {
count++;
tmp /= 10;
}//用临时变量tmp是为了确保i值的不变性,为后续i继续计算做铺垫
tmp =i;
int sum = 0;
while (tmp != 0) {
//Math.pow(底数, 指数);
//Math.pow(a, 3);等价于 a*a*a;
sum += Math.pow(tmp % 10, count);
tmp /= 10;
}
if (sum == i ){
System.out.println(i);
}
}
}
}
8.最大公约数(方法:利用辗转相除法)
辗转相除法:即较大数除以较小数,将除数作为被除数,余数作为除数,再次除,直到余数
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("请输入两个数,并求出两个数里的最大公约数:"); Scanner sc=new Scanner(System.in); int n1=sc.nextInt(); int n2=sc.nextInt(); int tmp=0; while(n1%n2!=0){ tmp=n1%n2; n1=n2; n2=tmp; } System.out.println(n2); } }
为零时求得的除数即最大公约数
9.二进制中1的个数
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("请输入一个数,并求出它的二进制中1的个数"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.println(geShu(n)); } public static int geShu(int x){ int count=0; while((x&1)!=0){ count++; x=x>>>1;//>>>相比于>>的好处在于无符号的限制,就可以计算类似于-1的位数 } return count; } }
原理如下
10.模拟登陆(可登录三次密码,输入成功就进入,三次输入失败就宣布结束)
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("请输入你的密码"); login(); } public static void login() { Scanner sc = new Scanner(System.in); int count = 3; while (count != 0) { String str = sc.nextLine();//每次输入的需要,确保密码错误的时候可以再输入的保证 if (str.equals("123456")) { //比较两个字符串是否相等,不能直接用=进行连接。 //要调用equals函数进行调用 System.out.println("密码正确!"); break; } else { count--; System.out.println("密码错误,你还有" + count + "次机会"); } } } }
11.输出一个数的每一位(注意此处为倒序输出)
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("请输入一个数字,并倒序输出它的每一位如下:"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); everyOne(n); } public static void everyOne(int x){ while(x!=0){ System.out.println(x%10);//求出末位 x/=10;//缩小一位数,比如,百位变成十位 } } }
12.求n*n的乘法表(输入一个数后可对应生成n*n的乘法表)
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("请输入一个数n,并求由这个数n构成的n*n的乘法表"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int i=1; int j=1; for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ int ret=i*j; System.out.println(i+"*"+j+"="+ret); } } System.out.println(); } }
13.找出现一次的数字
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("找出不同的数字"); int []array={1,2,3,2,1}; int n=array[0]; int i=0; for(i=1;i< array.length;i++){ n=n ^array[i]; } System.out.println("只出现一次的数字为:"+n); } }
14.输入一个数字,求其的阶乘
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("输入一个数n并求它的阶乘:"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int i=1; int ret=1; for(i=1;i<=n;i++){ ret*=i; } System.out.println(ret); } }
15.求阶乘和
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { System.out.println("输入一个数n并求1到n的阶乘之和:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(allSum(n)); } //求某个数的阶乘 public static int fac(int x) { int i = 1; int ret = 1; for (i = 1; i <= x; i++) { ret *= i; } return ret; } //求阶乘之和 public static int allSum(int x) { int sum = 0; int i = 0; for (i = 1; i <= x; i++) { sum = sum +fac(i);} return sum; } }
16.使奇数在偶数前面
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { //调整数组顺序使得奇数位于偶数之前。 int[] array = {1, 2, 3, 4, 5}; System.out.println("数组交换前:"); printNum(array); paiXu(array); System.out.println("交换数组后:"); printNum(array); } public static void printNum(int[] array) { int i = 0; for (i = 0; i < array.length; i++) { System.out.print(array[i]); } System.out.println(); } public static void paiXu(int[] array) { int left = 0; int right = array.length - 1; while (left < right) { while (array[left] % 2 != 0 && left < right) { left++; } while (array[right] % 2 == 0 && left < right) { right--; } if (left < right) { int tmp = array[left]; array[left] = array[right]; array[right] = tmp; } } } }