目录
- int day = 1;
- switch(day)
- {
- case 1:
- System.out.println("星期一");
- break;
- case 2:
- System.out.println("星期二");
- break;
- case 3:
- System.out.println("星期三");
- break;
- case 4:
- System.out.println("星期四");
- break;
- case 5:
- System.out.println("星期五");
- break;
- case 6:
- System.out.println("星期六");
- break;
- case 7:
- System.out.println("星期日");
- break;
- default:
- System.out.println("输入有误");
- break;
- }
- double num = 1.0;
- switch(num)
- {
- case 1.0:
- System.out.println("hehe");
- break;
- case 2.0:
- System.out.println("haha");
- break;
- }
编译出错Test . java : 4 : 错误 : 不兼容的类型 : 从 double 转换到 int 可能会有损失switch ( num ){^1 个错误
- public static void main7(String[] args) {
- int j = 1;
- int sum = 0;
- while (j <= 5) {
- int i = 1;
- int ret = 1;
- while (i <= j) {
- ret = ret * i;
- i++;
- }
- sum += ret;
- j++;
- }
- System.out.println(sum);
- }
- int sum = 0;
- for (int i = 1; i <= 5; i++)
- {
- int tmp = 1;
- for (int j = 1; j <= i; j++)
- {
- tmp *= j;
- }
- sum += tmp;
- }
- System.out.println("sum = " + sum);
System . out . println ( msg ); // 输出一个字符串 , 带换行System . out . print ( msg ); // 输出一个字符串 , 不带换行System . out . printf ( format , msg ); // 格式化输出
- public static void main15(String[] args) {
- System.out.println("输出且换行!");
- System.out.print("输出且不换行!");
- System.out.printf("%s\n","真的没换行吗?");
- }
使用 Scanner 读取字符串/整数/浮点数
- public static void main16(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
- System.out.println("请输入你的姓名:");
- String str = scanner.nextLine();
- System.out.println(str);
-
- System.out.println("请输入你的年龄:");
- int age = scanner.nextInt();
- System.out.println(age);
-
- System.out.println("请输入你的工资:");
- float money = scanner.nextFloat();
- System.out.println(money);
- }
使用 Scanner 循环读取 N 个数字,并求取其平均值
Scanner sc = new Scanner ( System . in );int sum = 0 ;int num = 0 ;while ( sc . hasNextInt ()){int tmp = sc . nextInt ();sum += tmp ;num ++ ;}System . out . println ( "sum = " + sum );System . out . println ( "avg = " + sum / num );sc . close ();// 执行结果1040.050.5^Zsum = 150.5avg = 30.1
游戏规则:系统自动生成一个随机整数(1-100), 然后由用户输入一个猜测的数字. 如果输入的数字比该随机数小, 提示 "低了", 如果输入的数字比该随机数大, 提示 "高了" , 如果输入的数字和随机数相等, 则提示 "猜对了" .
- import java.util.Random;
- import java.util.Scanner;
-
-
- public static void main18(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Random random = new Random();
- int randNum = random.nextInt(101);//[0,101)==>[0,100]
- System.out.println("随机数是"+randNum);
- while (true) {
- System.out.println("请输入你要猜的数字:");
- int num = scanner.nextInt();
- if(num < randNum) {
- System.out.println("猜小了!");
- }else if(num == randNum) {
- System.out.println("猜对了!");
- break;
- }else {
- System.out.println("猜大了!");
- }
- }
-
- }
- public static void main1(String[] args) {
- Scanner scan = new Scanner(System.in);
- int age = scan.nextInt();
- if(age < 18) {
- System.out.println("少年!");
- }else if(age >= 18 && age < 28) {
- System.out.println("青年!");
- }else if(age >= 29 && age < 55) {
- System.out.println("中年!");
- }else {
- System.out.println("老年!");
- }
- }
打印 1 - 100 之间所有的素数
- public static void main(String[] args) {
-
- for (int j = 1; j < 101; j++) {
- //int n = 19;
- int i = 2;
- for (; i <= Math.sqrt(j); i++) {
- if(j % i == 0) {
- break;
- }
- }
- if(i > Math.sqrt(j)) {
- System.out.println(j+" 是素数!");
- }
- }
-
- }
- public static boolean isPrime(int n) {
- 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+": 是素数!");
- return true;
- }
- return false;
- }
-
- public static void main5(String[] args) {
- for (int i = 1; i < 101; i++) {
- boolean ret = isPrime(i);
- if(ret) {
- System.out.println(i+" 是素数!");
- }
- }
- }
求两个正整数的最大公约数
- public static void main(String[] args) {
- //辗转相除法
- Scanner scanner = new Scanner(System.in);
- 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+" 是最大公约数!");
- }
求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如: 153=1^3+5^3+3^3 ,则153是一个“水仙花数”。)
- public static void main(String[] args) {
- for (int i = 1; i < 999999; i++) {
- int count = 0;
- int tmp = i;
- while (tmp != 0) {
- count++;
- tmp = tmp / 10;
- }
- tmp = i;
- int sum = 0;
- while (tmp != 0) {
- sum += Math.pow(tmp % 10,count);
- tmp /= 10;
- }
- if(sum == i) {
- System.out.println(i);
- }
- }
- }
写一个函数返回参数二进制中 1 的个数比如: 15 0000 1111 4 个 1
- 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);
- }
- public static void main(String[] args) {
- int n = -1;
- int count = 0;
- while (n != 0) {
- if((n & 1) != 0) {
- count++;
- }
- n = n >>> 1;
- }
- System.out.println(count);
- }
- public static void main00(String[] args) {
- int n = -1;
- int count = 0;
- while (n != 0) {
- n = n &(n-1);
- count++;
- }
- System.out.println(count);
- }
获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列。
- 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)+" ");
- }
- }
根据输入的行数画出星号组成的图案
- 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) {
- System.out.print("*");
- }else if( i+j+1 == n) {
- System.out.print("*");
- }else{
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
找出出现一次的数字
- public static void main(String[] args) {
- int[] array = {1,2,3,2,1};
- int sum = 0;
- for (int i = 0; i < 5; i++) {
- sum = sum ^ array[i];
- }
- System.out.println(sum);
- }