haihongyuan.com
海量文库 文档专家
全站搜索:
您现在的位置:首页 > 初中教育 > 初中数学初中数学

提高题答案

发布时间:2014-01-04 13:44:29  

abstract class shape{ //定义了一个抽象类

abstract public double area(); //定义了一个 抽象实例方法 }

//三角形面积

class trangle extends shape{

double a,b,c;

public trangle(double a,double b,double c){ //初始化属性 this.a=a; this.b=b; this.c=c;} public double area(){ double d=(a+b+c)/2; double q=d*(d-a)*(d-b)*(d-c); return Math.sqrt(q); } //海伦公式求三角形面积 }

//矩形面积

class rectangle extends shape{

double f,g;//矩形的长和宽 public rectangle(double f,double g){ this.f=f; //初始化属性长和宽 this.g=g;} public double area(){ return f*g; }}

//正方形的面积

class square extends shape{

double a;

public square(double r){

this.a=r;}

public double area(){

return a*a;}}

//自定义一个异常

class myException extends Exception{

public String toString(){

return "数组下标越界!";

}

}

public class Test{

public static void fn()throws myException{

throw new myException();

}

public static void main(String[] args){

try

{

int a=args.length;

switch(a){

case 1:{ //长度为一 是正方形 square i=new square(Integer.parseInt(args[0])); System.out.println("正方形的面积为:"+i.area()); break; }

case 2:{//长度为二 是矩形

rectangle j=new rectangle(Integer.parseInt(args[0]),Integer.parseInt(args[1]));

System.out.println("矩形的面积为:"+j.area()); break; }

case 3:{//长度为三 是三角形

shape k=new trangle(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]));

System.out.println("三角形的面积为:"+k.area()); break; } default: Test.fn(); break; } }catch(myException ex){ System.out.println(ex); } }}

public class RandomDemo {

//一行三个元素输出函数

public static void arrays(int[] s){

for(int i=0;i<20;i++){

System.out.print(s[i]+"\t");

if((i+1)%3==0)

System.out.println();

}

}

//输出最小值最大值函数

public void fn(int[] s){

int min=s[0];//最小值 int max=s[0];//最大值 for(int i=0;i<20;i++){ if(min>s[i]){ min=s[i]; }} for(int i=0;i<20;i++){ if(max<s[i]){ max=s[i]; } }System.out.println("\n最小值是:"+min+"\n最大值是:"+max); } public static void main(String[] args) { int[] s=new int[20]; RandomDemo t=new RandomDemo(); int count=0; for(int i=0;i<20;i++){ s[i]=(int)(Math.random()*100); } arrays(s); t.fn(s); for(int i=0;i<20;i++){ count+=s[i]; }

System.out.println("平均分是:"+count/s.length);

}}

fibonacci函数:0,1,1,2,3,5,8,13……

public class FibonacciDemo { //Fibonacci函数

public static int Fi(int i){

if(i==1||i==2)

return i-1;

else

return Fi(i-1)+Fi(i-2);

}

public static void main(String[] args) { int[] a=new int[20]; for(int i=0;i<20;i++){

a[i]=Fi(i+1);

System.out.print(a[i]+"\t");

if((i+1)%4==0)

System.out.println();

}

}

}

//定义一个抽象类

abstract class Human{

} String name;//姓名 String gender;//性别 String birthday;//出生日期 //Human(String name,String gender,String birthday){ //this.name=name; //this.gender=gender; //this.birthday=birthday;

//教师类

class Teacher extends Human{

String teachschool;//任课学校

String classes;//类别

Teacher(String name,String gender,String teachschool,String classes){

this.name=name;

this.gender=gender;

this.birthday=birthday;

this.teachschool=teachschool;

this.classes=classes;

}

public String toString(){

} } birthday,String String s="任课学校:"+teachschool; s+="\n任课类别:"+classes; s+= "\n姓名:"+name+"\n性别:"+gender+"\n出生日期:"+birthday; return s;

//学生类

abstract class Students extends Human{

String school;//在读学校 String schID;//学号 String grade;//年级 }

//大学生类

class undergraduate extends Students{

String major;//专业

String grade;//年级

}

//中学生类

class midschstudent extends Students{

String grade;//年级

}

//小学生类

class esstudents extends Students{

String grade;//年级

}

public class StudentsDemo {

public static void main(String[] args) {

Teacher te=new Teacher("李四","男","1993.6","交大理工","理科"); System.out.println(te.toString());

}

}

public class CountDemo {

public static int count1(String s,char c){

int x=0;

} for(int i=0;i<s.length();i++)//判断左右括号是否相等 { if(s.charAt(i)==c) x++; } return x;

public static void main(String[] args) { CountDemo cd=new CountDemo(); String s = args[0]; System.out.println("左右括号是否成对:"); if(count1(s,'(')==count1(s,')')){ System.out.println("Yes"); } else { System.out.println("No"); }

System.out.println("s出现的次数为:"+cd.count1(s, 's'));

}

}

import java.util.Scanner;

public class C{

public static int fun1(int a1,int b1){

int c;

c=a1%b1;

while (c>0)

{a1=b1;

b1=c;

c=a1%b1;

}

return b1;

}

public static void main(String[] args) {

int a,b,temp;

int m;

a=in.nextInt();

System.out.println("请输入第二个整数");

b=in.nextInt();

if(a<b)

{temp=a;a=b;b=temp;

}

m=fun1(a,b);

System.out.println("最大公约数为:"+m);

System.out.println("最小公倍数为:"+(a*b)/m);

}

}

import java.util.Scanner;

public class A {

public int fn(int b){

if(b==0||b==1)

return 1;

else

return b*fn(b-1);

}

public static void main(String[] args) {

A a=new A();

Scanner in=new Scanner(System.in);

System.out.println("2!+4!+5!="+a.fn(2)+a.fn(4)+a.fn(5));

System.out.println("请输入一个整数:"); int b=in.nextInt();

System.out.println(b+"的阶乘是:"+a.fn(b));

}

}

public class triangleTest {

class triangle{

int a,b,c;//三角形的三条边 triangle(int a,int b,int c){//初始化三边 this.a=a; this.b=b;

this.c=c; } public static double fn1(int a,int b,int c){ double q; if((a+b>c)&&(a-b<c)){ double m=(a+b+c)/2; double n=m*(m-a)*(m-b)*(m-c);

q=Math.sqrt(n);} //用海伦公式计算三角形面积

else {

double min = 0 ;

if(a<b&&a<c)

min=a;

else if(b<a&&b<c)

min=b;

else if(c<a&&c<b)

min=c;

else if(a==b&&a==c)

min=a;

q=(Math.sqrt(3)*min*min)/4;

}

return q;

}

}

public static void main(String[] args) {

try{

BufferedReader in= new BufferedReader(new InputStreamReader(System.in));

} System.out.println("请输入三个三角形的三边:"); int a=Integer.parseInt(in.readLine()); int b=Integer.parseInt(in.readLine()); int c=Integer.parseInt(in.readLine()); triangle A=new triangle(a,b,c); System.out.println("三角形的面积是:"+A.fn1(a, b, c));} catch(IOException ex){ System.out.println(ex);}

}

从键盘输入一串字符,以#作为结束标记,求这些字符中的最小者。注意:输入数据建议采用一行输入(例如:afdsfaasdsd#)

import java.util.Scanner;

public class test1{

public static void getmin(String s){

char a=s.charAt(0);

for(int i=1;i<s.length();i++){

if(a>s.charAt(i))

a=s.charAt(i);

if(s.charAt(i)=='#')

System.out.println(s.charAt(i-1));

break;}

System.out.println(a);

}

public static void main(String[] args){

Scanner in=new Scanner(System.in);

System.out.println("请输入一串字符:");

String x=in.nextLine();

getmin(x);

}

}

网站首页网站地图 站长统计
All rights reserved Powered by 海文库
copyright ©right 2010-2011。
文档资料库内容来自网络,如有侵犯请联系客服。zhit326@126.com