第六周
Posted 2017xinghui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六周相关的知识,希望对你有一定的参考价值。
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; } }
package shapeclass; public abstract class Shape { double PI=3.14; public abstract double getPerimeter(); public abstract double getArea(); }
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; } }
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; } }
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; } }
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 ; } }
以上是关于第六周的主要内容,如果未能解决你的问题,请参考以下文章