interface ComputeWeight {
double computeWeight();
}
class Television implements ComputeWeight{
@Override
public double computeWeight() {
return 16.6;
}
}
class AirConditioner implements ComputeWeight{
@Override
public double computeWeight() {
return 40.0;
}
}
class WashMachine implements ComputeWeight{
@Override
public double computeWeight() {
return 60.0;
}
}
class Truck {
private ComputeWeight[] goods;
public Truck(ComputeWeight[] goods) {
this.goods = goods;
}
public double getTotalWeight() {
double weight=0.0;
for (int i = 0; i < goods.length; i++) {
weight = weight + goods[i].computeWeight();
}
return weight;
}
}
身份证排序
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=Integer.valueOf(sc.nextLine());
String s[]=new String[count];
for (int i=0;i<count;i++){
s[i]=sc.nextLine();
}
String temp;
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
while (true){
String tem=sc.nextLine();
if (tem.equals("sort1")){
Date date[]=new Date[count];
for (int j=0;j<count;j++){
for (int i=0;i<count-j-1;i++){
String bir1=s[i].substring(6,14);
String bir2=s[i+1].substring(6,14);
Date d1;
Date d2;
try {
d1 = df.parse(bir1);
d2 =df.parse(bir2);
if (d1.getTime()>d2.getTime()){
temp=s[i];
s[i]=s[i+1];
s[i+1]=temp;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
for (int i=0;i<count;i++){
String bir=s[i];
System.out.println(bir.substring(6,10)+"-"+bir.substring(10,12)+"-"+bir.substring(12,14));
}
}else if (tem.equals("sort2")){
Date date[]=new Date[count];
for (int j=0;j<count;j++){
for (int i=0;i<count-j-1;i++){
String bir1=s[i].substring(6,14);
String bir2=s[i+1].substring(6,14);
Date d1;
Date d2;
try {
d1 = df.parse(bir1);
d2 =df.parse(bir2);
if (d1.getTime()>d2.getTime()){
temp=s[i];
s[i]=s[i+1];
s[i+1]=temp;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
for (int i=0;i<count;i++){
System.out.println(s[i]);
}
}else {
System.out.println("exit");
}
}
}
}
重复数据输出yes或no的问题
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int a[]=new int[10005];//开辟数组去存储输入的数字
int i=0,sign;//sign是是否出现重复数字的标识变量
while(in.hasNext())//判断输入是否结束
{
a[i]=in.nextInt();
i++;
}
sign=0;
for(int f=0;f<i;f++)
{for(int g=f+1;g<i;g++)
{ if(a[f]==a[g])sign++;//只要出现相等的就可立马退出,减少时间
if(sign!=0)break;}
if(sign!=0)break;}
if(sign!=0)System.out.println("yes");
else if(sign==0)System.out.println("no");
}
}
统计指定字母开头的单词数量
import java.util.Scanner;
/**
* 给出一句英文句子统计有多少个以某字母开头的单词
* @author 佳哥
*
*/
public class TestChar {
public static void main(String[] args) {
char words;
String str;
Scanner sc = new Scanner(System.in);
words = sc.next().charAt(0);
str = sc.nextLine();
int count = 0;
String[] strArray = str.split(" ");//把字符串通过" "(空格)拆分为字符串数组
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].charAt(0) == words) //if(strArray.startswitch(String.valueof(words)))
{
count++;
}
}
System.out.println(count);
}
}
设计一个矩形类Rectangle
class Rectangle {
public double width=1;
public double height=1;
public Rectangle(){
}
public Rectangle(double width,double height){
this.width=width;
this.height=height;
}
public double getArea(){
return width*height;
}
public double getPerimeter(){
return 2*(width+height);
}
}
根据派生类写出基类
public void say() {
System.out.println("I'm a person! My name is " + this.name + ".");
}
public People(String id, String name) {
this.id=id;
this.name=name;
}
public People() {
}
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;
}
设计Worker类及其子类
class Worker {
private double Rate;
public String name;
public double salaried;
public Worker(String name, double salaried) {
this.name=name;
this.salaried=salaried;
}
public void pay(int time){
System.out.println("Not Implemented");
}
}
class HourlyWorker extends Worker {
public HourlyWorker(String name, double salaried) {
super(name, salaried);
}
public void pay(int time){
if (time>40){
System.out.println((time-40)*2*salaried+40*salaried);
}else {
System.out.println(salaried*time);
}
}
public void setRate(int i) {
this.salaried=i;
}
}
class SalariedWorker extends Worker {
public SalariedWorker(String name, double salaried) {
super(name, salaried);
}
public void pay(){
System.out.println("580.0");
}
public void pay(int time){
System.out.println("580.0");
}
}
求圆面积自定义异常类
class Circle{
double r;
public Circle(double r){
this.r=r;
}
double area() throws CircleException {
if(this.r<0){
throw new CircleException(this.r);
}
return 3.14*r*r;
}
}
class CircleException extends Exception{
double r;
public CircleException(double r)
{
super();
this.r=r;
}
void print(){
System.out.println("圆半径为"+this.r+"不合理");
}
}
成绩录入时的及格与不及格人数统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=sc.nextInt();
int pass=0;
for (int i=0;i<count;i++){
int grade=sc.nextInt();
if (grade<0||grade>100){
System.out.println(grade+"invalid!");
}else if (grade>=60){
pass++;
}
}
System.out.println(pass);
System.out.println(count-pass);
}
}