import java.util.Random;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random random = new Random();
int rand = random.nextInt(100);//[0-100)->[0-99]
while (true) {
System.out.println("请输入你要猜的数字:");
int num = scan.nextInt();
if(num > rand) {
System.out.println("你猜的数字大了");
}else if (num == rand) {
System.out.println("猜对了!");
break;
}else {
System.out.println("你猜的数字小了");
}
}
System.out.println("游戏结束了!");
}
素数
//素数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int num = 1; num <= n ; num++) {
int i;
for (i = 2; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
break;
}
}
//程序走到这里的时候,有2种情况:
//1、循环正常的退出 素数
//2、因为break 不是素数
if(i > Math.sqrt(num)) {//i > n || i == n
System.out.println(num + " 是素数 ");
}
}
}
乘法表
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i = 1;i <= n ; i++) {
for (int j = 1;j <= i ;j++ ) {
System.out.print (i+"*"+j+"="+i*j +" ");
}
System.out.println();
}
}
最大公约数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();//18
int b = scan.nextInt();//24
int c = a%b;//余数 18 % 24 = 18
while (c != 0) {
a = b;//a = 24 a = 18
b = c;//b = 18 b = 6
c = a % b;//c = 24 % 18 = 6 c = 0
}
System.out.println("最大公约数:"+ b);
}
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("9的个数是: "+ count);
}
水仙花数
public static void main(String[] args) {
for (int i = 0;i <= 999999 ; i++) {
//判断每一个数字i是不是xxxx数
//1、判断当前i是几位数
//123 123/10=12 12/10=1 1/10=0
//总结:一般用除法--》一直往下除就好了
int count = 0;//数字的位数
int tmp = i;//先保存i到tmp
while (tmp != 0) {
count++;//1 2 3
tmp = tmp/10;//123/10=12 12/10=1 1/10=0
}
tmp = i;
int sum = 0;
while (tmp != 0) {
//sum = sum + (Math.pow(i%10,count));//1^count
sum += Math.pow(tmp%10,count);//复合运算符-》
tmp = tmp/10;//123/10 = 12 12/10=1 1/10=0
}
//3、sum = 就是每个位上数字的次方和
if(sum == i) {
System.out.println(sum + "是要找的数字!");
}
}
}
输入三次密码
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 3;
System.out.println("请输入正确的密码,您有3次机会!");
while(count != 0) {
System.out.println("请输入正确的密码:");
String password = scan.next();
//在java当中,判断两个字符串是否一样使用方法 equals
//password.equals("123456")
if(password.equals("123456")) {//"=="
System.out.println("登录成功!");
break;
}else {
count--;
System.out.println("登录失败,你还有 " + count +"次机会! ");
}
}
}
写一个函数返回参数二进制中 1 的个数 比如: 15 0000 1111 4 个1
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();//-1
int count = 0;
while(n != 0) {
if((n & 1) != 0 ){
count++;
}
n = n>>>1;//针对负数
}
System.out.println(count);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();//-1
int count = 0;
while(n != 0) {
count++;
n = n & (n-1);
}
System.out.println(count);
}
获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();//-1
for (int i = 31; i >= 1; i -= 2) {
System.out.print ( ((n >>> i) & 1) + " " );
}
System.out.println();
for (int i = 30; i >= 0; i -= 2) {
System.out.print ( ((n >>> i) & 1) + " " );
}
}