java第一次实训

1.编写程序: 声明一个整型变量a,并赋初值5,在程序中判断a是奇数还是偶数,然后输出判断的结果。

public class shixun1 {

    public static void main(String[] args) {
    
         int a=5;
            if(a%2==0)
            {
            System.out.println("a是偶数");}
            else{ System.out.println("a是奇数");}
    }

}

2.编写程序:从键盘输入圆的半径,计算圆的面积并输出。

import java.util.*;
public class shixun2 {

    public static void main(String[] args) {
        
    final double p=3.14;
    System.out.println("输入圆的半径:");
    Scanner reader = new Scanner(System.in);
    double r,s;
    r=reader.nextDouble();
    s=p*r*r;
    System.out.println("圆的面积="+s);
    }

}

3.编写程序:实现一个数字加密器。运行时输入加密前的整数,通过加密运算后,输出加密后的结果,加密结果仍为一整数。加密规则为:加密结果 = (整数*10+5) / 2 + 3.14159

import java.util.*;
public class shixun3 {
    public static void main(String[] args) {
    System.out.println("输入你需要加密的数字:");
    Scanner reader = new Scanner(System.in);
    int a;
    a = reader.nextInt();
    a=(int) ((a*10+5) / 2 + 3.14159);
    System.out.println("加密后整数为:"+a);
    }
}

4.编写程序公鸡5元/只,母鸡3元/只,小鸡3只/元,问100元买100只鸡,公鸡、母鸡、小鸡各几只?

public class shixun4 {
    public static void main(String[] args) {
    int x,y,z; 
    for(x=0;x<=100;x++) {
        for(y=0;y<=100;y++) {
            for(z=0;z<=100;z++) {
                   if(x+y+z==100&&5*x+3*y+z/3==100&&z%3==0)
                    System.out.println("公鸡:"+x+"母鸡:"+y+"小鸡:"+z);
                
            }
         }
      }
    }
}

 

5.编写程序:有1、2、3、4共4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

public class shixun5 {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        int k = 0;
        int t = 0;
        for(i = 1; i <= 4; i++) {
            for(j = 1; j <= 4; j++) {
                for(k = 1; k <= 4; k++) {
                    if(i != j&& j != k && i!= k) {
                        t += 1;
                        System.out.println(i*100 + j*10 + k);
                    }
                }
            }
        }
        System.out.println("总共能够组成" + t + "个数字!");
    }

}

 

6.编写程序:判断输入的整数是否为素数。

 

package shiyan;
import java.util.Scanner;
public class shixun4 {
    public static void main(String[] args) {
        int a;
        int i;
        System.out.println("请输入一个数字:");
        Scanner num = new Scanner(System.in);
        a = num.nextInt();
        if(a==1){
            System.out.println("不是素数");
        }
        for(i=2;i<a;i++){
            if(a%i==0){
                System.out.println("不是素数");
                break;
             }
           }
            if(a==i) {
                System.out.println("是素数");
            }
    }

}

 

posted on 2019-04-03 11:36  周橙梓  阅读(143)  评论(0编辑  收藏  举报

导航