第三周作业-循环与判断语句(网络131黄宇倩)

1:程序练手。P24 例2-4 数值运算程序,在Eclipse中输入该程序的代码,得到运行结果。把源程序发送到自己邮箱或优盘中。类名:MathDemo

public class MathDemo 
{
	public static void main(String args[])
	{
		System.out.println("abs(-5)="+Math.abs(-5));
		System.out.println("max(6.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)="+(int)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));
	}
}

运行结果:



2:for循环练习。 P45 编程题第3题:编写一个程序,其输入结果如下。类名:LoopExample

public class LoopExample 
{
	public static void main(String args[])
	{
	  for(int i=1;i<5;i++)
	  {
	  for(int j=1;j<5-i;j++)
	     {
	     System.out.print(" ");
		 }
		 for(int k=1;k<=2*i-1;k++)
		    {
			System.out.print("*");
			}
			System.out.println();
	   }
	}
}

运行结果如下:



3:从控制台读取输入数据。 《Java语言程序设计-基础篇》第二章-基本程序设计 P21(pdf文件的第42页) “程序清单2-2”,修改该程序,完成教材P33中的例2-5的功能——从键盘读入一系列整数并计算其和,输入0则表示输入结束。类名:ConsoleInput

import java.io.*;  
public class ConsoleInput   //从键盘读入一系列整数并计算其和  
 {  
  public static void main(String arge[])  
  {  
  int data;  
  int sum=0;  
  System.out.println("Enter an int value");  
  data=MyInput.readInt();  
  while(data!=0);  
  {  
   sum+=data;  
   System.out.println("Enter an int value,the program exits if the input is 0");  
   data=MyInput.readInt();  
   }  
   System.out.println("The sum is"+sum);  
   }  
 }  
  
class MyInput  
 {  
  public static String readString()  
  {  
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);  
  int k;    //声明一个存放整型且名是k的变量  
  float x,y;//声明浮点型变量x,y  
  String studentname="WangXin";  
  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());  
 }  
}  

运行结果如下:



4:循环语句使用。计算Pi的近似值,计算公式为 Pi = 4*(1-1/3+1/5-1/7+1/9-1/11+……),要求精度优于 3.14159。类名:CalculatePi

public class CalculatePi 
{
  public static void main(String[] args) 
  {
  double pi = 0;
  int i = 1;
  for (i = 1; Math.abs(1.0d / i) > 1E-6; i += 2) 
  {   
   if (i % 4 == 1) 
   {
    pi += (1.0d / i);
   } else 
   {
    pi -= (1.0d/ i);
   }
  }
  pi = pi * 4;
  System.out.println(pi);
 }

}

运行结果:



5:计算闰年。教材P45 编程题2:判断某一年份是否为闰年。(如果这个年份能被4整除,但不能被100整除;或者,如果这个年份能被4整除,又能被400整除;满足以上两个条件中的一个的年份为闰年)。类名:LeapYear

import java.util.Scanner;  

 public class LeapYear 
 {  
   public static void main(String []args)  
   {  
         Scanner input= new Scanner(System.in); 
         System.out.println( "输入年份"); 
         double year = input.nextDouble(); 		 
        if (year % 4 == 0 && year % 100!=0||year%400==0)
		{  
        System.out.println("是闰年");  
              }  
        else 
		{  
        System.out.println("不是闰年");  
        }  
    }   
	}

运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值