练习题

1, 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

public class Text1 {
    public static void main(String[] args) {
        int a;//个位
        int b;//十位
        int c;//百位
        int sum=0;
        int e=0;
        
        for( a=1;a<5;a++){
            for( b=1;b<5;b++){
                for( c=1;c<5;c++){
                    if(a!=b&&b!=c&&c!=a){
                        System.out.println("组成的三位数有:"+a+b+c);
                        sum++;
                        e++;
                        System.out.println("组成无重复的三位数为:"+e+"");
                    }if(sum==3){
                        sum=0;
                    }
                }
            }
        }
        
    }

}

2, 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。

/*2+1=3,3+2=5,5+3=8,8+5=13,13+8=21.
分子+分母=下个数的分子,而分母就为前个数的分子
所以后面的就是44/21 ,65/44,109/65.自己加*/
public class Text2 {
    public static void main(String[] args) {
        float s=1;//分子
        float x=1;//分母
        float sum=0;
        float temp = 0;
        
        for(int i=0;i<20;i++){
            temp=x;
            x=s;
            s=s+temp;
            sum+=s/x;
            
        }
        System.out.println("前20项之和为:"+sum);
    }

}

3, 题目:求1!+2!+3!+...+20!的和(5!表示5的阶乘, 即5*4*3*2*1)

public class Text3 {
    public static long n=1;
    public static long sum=0;
    
    
    
    public static void main(String[] args) {
        for(long i=1;i<=20;i++){
            n*=i;     
            sum+=n;
            System.out.println(n);
        }
        System.out.println("总和为:"+sum);
    }

}

4, 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

public class Text4 {
    public int a;//个位,
    public int b;//万位
    public int c;//十位
    public int d;//千位
    public int e;
    public void panduan(int a,int c,int e,int d,int b){
        if(a==b&&c==d){
            System.out.println("这个5位数是回文数!");
        }else{
            System.out.println("这个5位数不是回文数!");
        }
    }
    
    public static void main(String[] args) {
        Text4 t=new Text4();
        t.panduan(1,2,3,2,1);
    }

}

5, 题目:先写一个程序, 随机生成一个3*3的矩阵数字(1-9数字全部用到不能重复), 然后求这个3*3矩阵对角线元素之和

public class Text5 {
    public  int arr[][]=new int[3][3];
    public int sum=0;
    
    
    
    
    public static void main(String[] args) {
        List<Integer> I=new ArrayList<Integer>();
        for(int i=1;i<10;i++){
            I.add(i-1,i);
        }
        juzhen(I);
    }

    public static void juzhen(List<Integer>I){
        int arr[][]=new int[3][3];
        int sum=0;
        Random r=new Random();
        for(int i=1;i<3;i++){
            for(int j=1;j<3;j++){
                int a=r.nextInt(I.size());//定义一个数接收随机数
                arr[i][j]=I.get(a);//将集合中的数赋给数组
                I.remove(a);//赋值之后删除a
                
            }
        }
        sum=arr[0][2]+arr[2][0]+arr[1][1];
        System.out.println("矩阵的对角线元素之和为:"+sum);
    }

}

1, 从data.txt文件中读取数据到程序中, 实现一个联动功能, 即输入主食会显示"1---馒头 2---煎饼 3---米饭", 

再次输入会显示下一级菜单data.txt文件中每一行都有被 "," 分割的三个值, 第一个值代表这项食物的编号

(对于整个数据来说是唯一的), 第三个值表示所属的上一级食物分类

public class Text10 {
    public static void main(String[] args) {
        List<Food> list=new ArrayList<Food>();
        File file=new File("E:\\java\\data1.txt");
        
        Reader reader=null;
        try {
            reader=new FileReader(file);
            BufferedReader br=new BufferedReader(reader);
            
            String s=null;
            try {
                while((s=br.readLine())!=null){
                    String ss[]=s.split(",");
                    if(ss.length==3) {
                        Food food = new Food(ss[0],ss[1],ss[2]);
                        list.add(food);
                    }
                    
                }
                
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        for(Food ff:list){
            if("0".equals(ff.getParentid())){
                System.out.println(ff.getId()+"---"+ff.getName());
            }
        }
        Scanner scan=new Scanner(System.in);
        boolean flag = true;
        String in_str = scan.nextLine();
        while(flag) {
            if("exit".equals(in_str)) {
                flag = false;
            } else {
                printFood(list, in_str);
                in_str = scan.nextLine();
            }
        }
        scan.close();
    }
    public static void printFood(List<Food> list, String pid){
        for(Food ff : list) {
            if(pid.equals(ff.getParentid())) {
                System.out.println(ff.getId()+"---"+ff.getName());
            }
        }
    }
}


package zonghelianxi;

public class Food {
    public String id;
    public String name;
    public String parentid;
    
    
    
    
    
    public Food(String id, String name, String parentid) {
        super();
        this.id = id;
        this.name = name;
        this.parentid = parentid;
    }
    @Override
    public String toString() {
        return "Food [id=" + id + ", name=" + name + ", parentid=" + parentid + "]";
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getParentid() {
        return parentid;
    }
    public void setParentid(String parentid) {
        this.parentid = parentid;
    }
    
    

}

posted @ 2017-08-05 16:51  *眉间缘*  阅读(238)  评论(0编辑  收藏  举报