1.1从键盘输入
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//int a = 10;
System.out.println("请输入你的年龄");
int age = scan.nextInt();
System.out.println("年龄:" + age);
String name = scan.nextLine();
System.out.println("姓名:" + name);
}
}
此时这个nextLine会失效,更改方式就是上下调换一下,先输入name在输入age如System.out.println("请输入你的姓名");
String name = scan.nextLine();
System.out.println("姓名:" + name);
int age = scan.nextInt();
System.out.println("年龄:" + age);
这样的话可以在输入名字的时候加空格,如feng junqing,因为nextline是识别一整行,next只会识别空格前的
而当代码这样更改时
System.out.println("请输入你的姓名"); String name = scan.next(); System.out.println("姓名:" + name); int age = scan.nextInt(); System.out.println("年龄:" + age);
不能输入名字时加空格他只会识别空格前的feng,而junqing就会给到后面年龄,这样就会报错
Exception in thread "main" java.util.InputMismatchException---输入有误的
结论:当输入的数据有很多不同类型的时候,优先去处理字符串的部分(即,把字符串放在最前面)
如果我要循环输入很多数据,就加一个循环
while (scan.hasNextLine()) { System.out.println("请输入你的姓名"); String name = scan.nextLine(); System.out.println("姓名:" + name); }
在idea中要结束你的代码运行就按键盘上的Ctrl+d
1.2猜数字小游戏
import java.util.Random; public class Main { public static void main(String[] args) { Random random = new Random(); //0-99 int n = random.nextInt(bound:100); System.out.println(n ); }
使用一个Random来进行计算机的随机数选取,他本质是通过时间来进行选取的,要想让随机数每次都一样的话,可以在这里加一个数字Random random = new Random();
生成随机数还有一个Math.random
1.2.1成品展示
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
int n = random.nextInt(100);
Scanner scan = new Scanner(System.in);
System.out.println("请您进行1-100之间的数字猜谜");
System.out.println("随机数" + n);
while (true) {
int num = scan.nextInt();
System.out.println("请输入您猜的数字:" + num);
if (num > n) {
System.out.println("您猜大了,请重新猜");
} else if (num < n) {
System.out.println("您猜小了,请重新猜");
} else {
System.out.println("恭喜您,您猜对了");
break;
}
1.3练习
1.求一个整数,在内存中储存时,二进制1的个数
解析:一个整数换算为二进制数是32位,假设5就是前面省略28个0,0101。让它和0001进行与操作,1和1与就是1,1和0与就是0。与操作进行一次让上面的二进制数右移一位。
答案:
public class Main { public static void main(String[] args) { int n = 7; int count = 0; for(int i = 0;i < 32; i++){ if(((n >> i) & 1 ) != 0){ count++; } } System.out.println(count); }
但是这样的话不管啥数字都必须移动31次
优化:
public class Main { public static void main(String[] args) { int n =7; int count = 0; while(n != 0){ if( (n & 1) != 0){ count++; } n = n >>> 1; } System.out.println(count); }
算法还不够优化n&(n-1),每&一次n就少一个1。
public class Main { public static void main(String[] args) { int n = 7; int count = 0; while (n != 0) { n = n & (n - 1); count++; } System.out.println(count); }
2.计算1/1-1/2+1/3-1/4.....+1/99-1/100的值
public class Main { 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(sum); }
3.水仙花数
如153----1^3+3^3+5^3=153
分析
1.算 是几位数
2.求每一位数字是几
3.把每一位数字是几,位数的次幂是几
public class Main { public static void main(String[] args) { for (int i = 1; i <= 9999999; i++){ int count = 0; int tmp = i; int sum = 0; while(tmp !=0 ) { tmp /= 10; count++; } //count里面就储存了当前数字i是几位数 tmp = i; while(tmp != 0) { //第一个是参数,第二个是多少次方 sum += (int) pow(tmp % 10, count); tmp /= 10; } if (sum == i){ System.out.println(i); } } }
看java某个方法的源码:
鼠标点到这个方法上
按住Ctr+鼠标左键跳转
想得到每一位就%10÷10
4.打印x图案
public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNextInt()) { int n = scan.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i + j == n-1) || (i == j)) { System.out.print("*"); } else { System.out.print(" "); } }System.out.println() } } }
5.输出乘法口诀表
public class Main { 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(" "); } }
6.输出一个整数的每一位
自己写的芜湖 public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int tmp = 0; int j; while(n != 0){ j= n % 10; tmp = n / 10; n = tmp; System.out.println(j); } }
优化 public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); while(n != 0){ System.out.println(n%10); n /= 10; } }
7.模拟登录,编写代码模拟三次密码输入的场景,最多能输入三次密码,密码正确,提示登录成功,密码错误,可以重新输入,最多输入三次,三次均错,提示退出程序
public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count = 3; while(count != 0){ System.out.println("请输入你的密码。你共有" + count + "次机会!"); String pass = scan.nextLine(); if(pass.equals("123")){ System.out.println("登录成功"); break; }else { System.out.println("登录失败"); } count--; } }
字符串的相等不能用== ,得用String.equals(" ")
8.二进制序列,获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
解析:与0001与,1与1得1,0与1得0。
public static void main(String[] args) { int n = 7; 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) + " "); }//奇数
得出:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
9.判断一个数字是否是素数
public static void main(String[] args) { int n = 7; int i = 2; for(; i < n; i++){ if(n % i == 0 ){ System.out.println("不是素数"); break; } } if(i == n){ System.out.println("是素数"); } }
优化:
public static void main(String[] args) { int n = 7; int i = 2; for(; i <= Math.sqrt(n); i++){ if(n % i == 0 ){ System.out.println("不是素数"); break; } } if(i > Math.sqrt(n)){ System.out.println("是素数"); } }
变例:输出100内的素数
public static void main(String[] args) { for (int k = 1; k <= 100; k++) { int i = 2; for (; i <= Math.sqrt(k); i++) { if (k % i == 0) { break; } } if (i > Math.sqrt(k)) { System.out.println(k + "是素数"); } } }
10.最大公约数
public class Main { public static void main(String[] args) { int a =24; int b = 18; int c = a % b; while(c != 0){ a = b; b = c; c = a % b; } System.out.println(b); }
辗转相除法
最小公约数 = 两整数的乘积 / 最大公约数