第四周作业-键盘输入数据 和 数据类型 网络131

来源:互联网 发布:sqlite数据库下载 编辑:程序博客网 时间:2024/05/19 03:30

1.程序练手。P21 例2-1 计算圆面积,在Eclipse中输入该程序的代码,得到运行结果。把源程序发送到自己邮箱或优盘中。类名:ComputeArea

public class ComputerArea{  public static void main(String args[])  {  final double PI=3.14159;  double area;  area=PI*10*10;  System.out.println("The area for the circle of 10 is"+area);  }}


2:键盘输入数据。 修改程序例2-1,改为从键盘输入数据,计算相应的圆面积。把源程序发送到自己邮箱或优盘中。类名:ComputeArea2

import java.io.*;public class ComputeArea2{ public static void main(String []args) {  System.out.println(10*10*3.14159); }}class MyInput{ public static String readString() {  BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);   String string=" ";    try     {      string=br.readLine();     }    catch (IOException ex)     {   System.out.println(ex);      }         return string;      } public static int readInt() {  return Integer.parseInt(readString()); }  public static double readDouble() {  return Double.parseDouble(readString()); }}



3.数值类型范围与数值运算。结合P22例2-2 和 P25例2-4,理解数值的范围与基本的数值运算。类名:  MathDemo

public class MathDemo  {      public static void main(String args[])    {       System.out.println("abs(-5)="+Math.abs(-5));       System.out.println("max6.75,3.14)="+Math.max(6.75,3.14));       System.out.println("min(100,200)="+Math.min(100,200));       System.out.println("round(3.5)="+Math.round(3.5));       System.out.println("round(-6.5)="+Math.round(-6.5));       System.out.println("sqrt(2)="+Math.sqrt(2));       System.out.println("pow(2,5)="+Math.pow(2,5));       System.out.println("E="+Math.E);       System.out.println("exp(2)="+Math.exp(2));       System.out.println("log(2)="+Math.log(2));       System.out.println("ceil(6.75)="+(int)Math.ceil(6.75));                              System.out.println("floor(6.75)="+Math.floor(6.75));       System.out.println("Pi="+Math.PI);       System.out.println("sin(Pi/4)="+Math.sin(Math.PI/4));       System.out.println("cos(1)="+Math.cos(1));    }  }  


public class TypeMaxValue{ public static void main(String args[]) { //整数类型   byte largestByte=Byte.MAX_VALUE;//定义一个byte类型的变量   short largestShort=Short.MAX_VALUE;//定义一个short类型的变量   int largestInteger=Integer.MAX_VALUE;//定义一个int类型的变量   long largestLong=Long.MAX_VALUE;   float largestFloat=Float.MAX_VALUE;   double largestDouble=Double.MAX_VALUE;   System.out.println("最大的byte值是:"+largestByte);   System.out.println("最大的short值是:"+largestShort);   System.out.println("最大的integer值是:"+largestInteger);   System.out.println("最大的long值是:"+largestLong);   System.out.println("最大的float值是:"+largestFloat);   System.out.println("最大的double值是:"+largestDouble); }}




0 0