- int num1=10;
- if(num1%2==0) {
- System.out.println("num1是一个偶数");
- }else{
- System.out.println("num1是一个奇数");
- }
- int num2=-5;
- if(num2>0) {
- System.out.println("num2是正数");
- }else if(num2==0){
- System.out.println("num2是0");
- }else{
- System.out.println("num2是负数");
- }
- System.out.println("请输入一个年份");
- Scanner sc1=new Scanner(System.in);
- int year=sc1.nextInt();
- if((year%4==0)&&(year%100!=0)){
- System.out.println(year+"是一个普通闰年");
- } else if (year%400==0) {
- System.out.println(year+"是一个世纪闰年");
- }else{
- System.out.println(year+"不是闰年");
- }
-
- int a = 1;
- while(a <= 10){
- System.out.println(a);
- a++;//条件要发生变化,不然会死循环
- }
- int b=1;
- int ret=1;
- int sum=0;
- while(b<=5){
- ret=ret*b;
- b++;
- sum=sum+ret;
- }
- System.out.println("ret="+ret);
- System.out.println("sum="+sum);
- int j=0;
- Scanner num3=new Scanner(System.in);
- int number=num3.nextInt();
- for(j=2;j
- {
- if(number%j==0){
- System.out.println("该数不是素数");
- break;
- }else{
- System.out.println("该数是素数");
- }
- }
练习题6:打印0-100之间所有的素数
- int k=0;
- int k1=0;
- for(k=2;k<=100;k++) {
- for(k1=2;k1<=k/2;k1++){
- if(k%k1==0){
- break;
- }
- }
- if(k1>=k/2) {
- System.out.println(k);
- }
- }
练习题7:输出1000-2000之间所有的闰年
- int year3=0;
- for(year3=1000;year3<=2000;year3++){
- if(year3/400==0||(year3%4==0&&year3/100!=0)) {
- System.out.println(year3);
- }
- }
练习题8:1-100间数字9出现的数目
- int i2=0;
- int sum1=0;
- for(i2=1;i2<=100;i2++){
- if(i2==99){
- sum1+=1;
- }else if(i2%10==9||i2/10==9) {
- sum1++;
- }
- }
- System.out.println("数字9出现了"+sum1+"次");