package practice; /**练习数字取位和++运算*/ public class practiceOnly { public static void main(String[] args) { System.out.println(25/10);//取十位包含几个10 System.out.println(25%10);//取各位 int x=59 ; System.out.println(x/10); System.out.println(x%10); int y=11259; System.out.println(y/10000);//取第一位 System.out.println(y%10000/1000);//取1259-》1 System.out.println(y%1000/100);//直接取259-》2 System.out.println(y%100/10);//取59,取到不能/100的那一位,再取十位 System.out.println(y%10);//直接取各位 /** * x/10--取十位,几个10 * x/100-取百位,几个100 * x/1000-取千位,几个1000 * * x%10--取各位,取到不能/10的位数 * x%100-取最后两位,取到不能/100的位数 * x%1000-取最后三位,取到不能/1000的位数 * x%10000-取最后四位,取到不能/10000的位数 * */ System.out.println("我是一个无情的分界线"); int a = 1; System.out.println(a++);//输出1,先输出a的值,再+1 System.out.println(a);//输出2,a的值已经变为2 a=1; System.out.println(++a);//输出2,先计算+1,再输出a的值 System.out.println(a);//输出2 int c = 1; System.out.println(--c);//0,符号在前,先自减,再打印 System.out.println(--c-c-c--);//-1-(-1)-(-1)=1 System.out.println(c);//-2 } }
* x/10--取十位,几个10
* x/100-取百位,几个100
* x/1000-取千位,几个1000
*
* x%10--取各位,取到不能/10的位数
* x%100-取最后两位,取到不能/100的位数
* x%1000-取最后三位,取到不能/1000的位数
* x%10000-取最后四位,取到不能/10000的位数
package practice; import java.util.Scanner; /**比较大小函数*/ public class practiceFindMax { public static void main(String[] args) { System.out.println("input the first number:"); int a= new Scanner(System.in).nextInt(); System.out.println("input the second number:"); int b= new Scanner(System.in).nextInt(); int max = a>b ? a : b ; System.out.println("the max number of two is "+max); /** * int max = 1 ? 2:3 ; * * 1 是表达式 , * 1 的结果为true-返还2的值 , false-返还3的值 * * */ int max = a>b ? (a>c?a:c) : (b>c?b:c)//判断三个值 先比较ab 再 ac bc } }
package practice; import java.util.Scanner; /**判断闰年和平年 * 如果年份是闰年,需要满足下面两个条件之一: * 条件1:能被4整除,并且不能被100整除 * 条件2:能被400整除 * */ public class judgeYear { public static void main(String[] args) { System.out.println("please inout the year you want to judge:"); int year = new Scanner(System.in).nextInt() ; if((year%4==0 && year%100!=0)||year%400==0) { System.out.println(year+"年是闰年"); }else { System.out.println(year+"年不是闰年"); } } }
package practice; /**练习打折活动 * 满1000打9折; * 满2000打8折; * 满5000打5折*/ import java.util.Scanner; public class PractiseDiscount { public static void main(String[] args) { System.out.println("input how much money you've spent:"); double money= new Scanner(System.in).nextDouble(); if (money>=5000) { money=money*0.5; }else if (money>=2000) { money=money*0.8; }else if (money>=1000) { money=money*0.9; } // if (money>=1000 && money<2000) { // money = money*0.9; // }else if (money>=2000&&money<5000) { // money=money*0.8; // }else if(money>=5000) { // money=money*0.5; // } System.out.print("the total money is:"+money); } }
package practice; import java.util.Scanner; public class PractiseScore { public static void main(String[] args) { System.out.println("input the score:"); int score = new Scanner(System.in).nextInt(); if (score<0||score>100) { System.out.println("please input the right score"); }else if(score>=90) { System.out.println("优秀"); }else if (score>=80) { System.out.println("良好"); }else if (score>=70) { System.out.println("中等"); }else if(score>=60) { System.out.println("及格"); }else if (score<60) { System.out.println("不及格"); } } }
package practice; import java.util.Scanner; public class practiceswi { public static void main(String[] args) { System.out.println("input the number:"); int n= new Scanner(System.in).nextInt(); switch(n) { case 1: System.out.println("1"); break; case 2: System.out.println("2"); break; default: System.out.println("wrong"); } } }
package practice; public class forloopnumbercount { public static void main(String[] args) { int i,sum=0,count=0; //1+。。。+100 for(i=1;i<101;i++) { sum=sum+i; } System.out.println(sum); //100以内偶数之和 sum=0; for(i=1;i<101;i++) { if(i%2==0) { sum=sum+i; count++; } } System.out.println(sum); System.out.println("count="+count); //100奇数之和 sum=0; for(i=1;i<101;i++) { if(i%2 !=0) { sum=sum+i; } } System.out.println(sum); } }
package practice2; public class PractiseVariable { static String name;//默认为null 下面全是0 static byte b; static short c; static int d; static long e; static float f; static double g; static char h; static boolean i;//默认值为false public static void main(String[] args) { int abc; System.out.println(name); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); System.out.println(g); System.out.println(h); System.out.println(i); } }
package practice2; public class PractiseVari2 { static int count; static int sum=200; public static void main(String[] agrs) { int sum=100; System.out.println(sum); System.out.println(count);//0 for (int i = 0; i < 10; i++) {//在for循环里定义所以只能在forloop里使用 System.out.println(i); } } }