码迷,mamicode.com
首页 > 其他好文 > 详细

第六周

时间:2018-10-02 14:10:28      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:lse   程序   false   etc   int   input   dex   pen   tac   

 1

技术分享图片
import java.util.*;
import shapeclass.Shape;
import shapeclass.Rectangle;
import shapeclass.Circle;
import java.math.*;

public class Count {


    
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String rect="rect";
        String cir="cir";
        System.out.print("n:");
        int n=in.nextInt();
        Shape[] score=new Shape[n];
        for(int i=0;i<n;i++){
        System.out.println("rect&cir:");
        String input=in.next();
        if(input.equals(rect)) {//System.out.print("rect:");
        double length = in.nextDouble();
        double width = in.nextDouble();
        System.out.println("Rectangle["+"length:"+length+"  width:"+width+"]");
        score[i]=new Rectangle(width,length);
        }
        if(input.equals(cir)) {//System.out.print("cir:");
        double radius = in.nextDouble();
        System.out.println("Circle["+"radius:"+radius+"]");
        score[i]=new Circle(radius);
        }
        }
        Count c=new Count();
        System.out.println(c.sumAllPerimeter(score));
        System.out.println(c.sumAllArea(score));
        for(Shape s:score) {
        //System.out.println(s.getArea()+s.getPerimeter());
        System.out.println(s.getClass()+"     ,    "+s.getClass().getSuperclass());
        }


    }

    public double sumAllArea(Shape score[])
    {
         double sum=0;
         for(int i=0;i<score.length;i++)
             sum+= score[i].getArea();
         return sum;
    }
    
    public double sumAllPerimeter(Shape score[])
    {
         double sum=0;
         for(int i=0;i<score.length;i++)
             sum+= score[i].getPerimeter();
         return sum;
    }
    
}
main
技术分享图片
package shapeclass;

public abstract class  Shape {

    double PI=3.14;

    public abstract double  getPerimeter();

    public abstract double  getArea();
}
shape
技术分享图片
package shapeclass;

public class Circle extends Shape{

    private double radius;
    public Circle(double r)
    {
        radius=r;
    }
    public double getPerimeter()
    {
        double Perimeter=2*PI*radius;
        return Perimeter;
    }
    public double getArea()
    {
        double Area=PI*radius*radius;
        return Area;
    }

    
}
Circle
技术分享图片
package shapeclass;

public class Rectangle extends Shape{
    private double width;
    private double length;
    public Rectangle(double w,double l)
    {
        this.width=w;
        this.length=l;
    }
    public double getPerimeter()
    {
        double Perimeter=(width+length)*2;
        return Perimeter;
    }
    public double getArea()
    {
        double Area=width*length;
        return Area;
    }
    

    
    
}
Rectangle

技术分享图片

2

技术分享图片
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class New{
    private static ArrayList<Student> studentlist;
    public static void main(String[] args) {
        studentlist = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        File file = new File("D:/身份证号.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            String temp = null;
            while ((temp = in.readLine()) != null) {
                
                Scanner linescanner = new Scanner(temp);
                linescanner.useDelimiter(" ");    
                String name = linescanner.next();
                String number = linescanner.next();
                String sex = linescanner.next();
                String age = linescanner.next();
                String province =linescanner.nextLine();
                Student student = new Student();
                student.setName(name);
                student.setnumber(number);
                student.setsex(sex);
                student.setage(age);
                student.setprovince(province);
                studentlist.add(student);

            }
        } catch (FileNotFoundException e) {
            System.out.println("学生信息文件找不到");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("学生信息文件读取错误");
            e.printStackTrace();
        }
        boolean isTrue = true;
        while (isTrue) {

            System.out.println("1.按姓名查询");
            System.out.println("2.按身份证号查询");
            System.out.println("3.退出");
            int nextInt = scanner.nextInt();
            switch (nextInt) {
            case 1:
                System.out.println("请输入姓名");
                String studentname = scanner.next();
                int nameint = findStudentByname(studentname);
                if (nameint != -1) {
                    System.out.println("查找信息为:身份证号:"
                            + studentlist.get(nameint).getnumber() + "    姓名:"
                            + studentlist.get(nameint).getName() +"    性别:"
                            +studentlist.get(nameint).getsex()   +"    年龄:"
                            +studentlist.get(nameint).getage()+"  地址:"
                            +studentlist.get(nameint).getprovince()
                            );
                } else {
                    System.out.println("不存在该学生");
                }
                break;
            case 2:
                System.out.println("请输入身份证号");
                String studentid = scanner.next();
                int idint = findStudentByid(studentid);
                if (idint != -1) {
                    System.out.println("查找信息为:身份证号:"
                            + studentlist.get(idint ).getnumber() + "    姓名:"
                            + studentlist.get(idint ).getName() +"    性别:"
                            +studentlist.get(idint ).getsex()   +"    年龄:"
                            +studentlist.get(idint ).getage()+"   地址:"
                            +studentlist.get(idint ).getprovince()
                            );
                } else {
                    System.out.println("不存在该学生");
                }
                break;
            case 3:
                isTrue = false;
                System.out.println("程序已退出!");
                break;
            default:
                System.out.println("输入有误");
            }
        }
    }


    public static int findStudentByname(String name) {
        int flag = -1;
        int a[];
        for (int i = 0; i < studentlist.size(); i++) {
            if (studentlist.get(i).getName().equals(name)) {
                flag= i;
            }
        }
        return flag;

    }


    public static int findStudentByid(String id) {
        int flag = -1;

        for (int i = 0; i < studentlist.size(); i++) {
            if (studentlist.get(i).getnumber().equals(id)) {
                flag = i;
            }
        }
        return flag;

    }
    
    
}
main
技术分享图片
public class Student {

    private String name;
    private    String number ;
    private    String sex ;
    private    String age;
    private    String province;

    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getnumber() {
        return number;
    }
    public void setnumber(String number) {
        this.number = number;
    }
    public String getsex() {
        return sex ;
    }
    public void setsex(String sex ) {
        this.sex =sex ;
    }
    public String getage() {
        return age;
    }
    public void setage(String age ) {
        this.age=age ;
    }
    public String getprovince() {
        return province;
    }
    public void setprovince(String province) {
        this.province=province ;
    }


}
Student

 

 

第六周

标签:lse   程序   false   etc   int   input   dex   pen   tac   

原文地址:https://www.cnblogs.com/2017xinghui/p/9736111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!