随笔-159  评论-114  文章-7  trackbacks-0

作为程序员,思维要缜密,对于技术人员,创造只是一小部分,大部分还是要脚踏实地。

所以每个问题一定要想到各种情况,测试人员可能会用你想不到的数据进行测试。

练习1:计算某数的阶乘!

package com.ljl;

public class Factorial {
    
    
public static void main(String[] args)
    
{
        
try{
            
int num = Integer.parseInt(args[0]);
            
if(num < 0)
            
{
                System.out.println(
"please input positive number!");
                System.exit(
0);
            }

            
if(num > 170)
            
{
                System.out.println(
"this program can't calculate this num!");
                System.exit(
0);
            }

            
double result = 1d;
            
for(int i = 2; i <= num; i++)
            
{
                result 
*= i;
            }

            System.out.println(
"The factorial of " + num + " is " + result);
        }

        
catch(Exception ex)
        
{
            
if(ex instanceof ArrayIndexOutOfBoundsException)
            
{
                System.out.println(
"Please input the num to calculate!");
                System.exit(
0);
            }

            
if(ex instanceof NumberFormatException)
            
{
                System.out.println(
"Please input a number!");
                System.exit(
0);
            }

            System.out.println(
"error occured!please run it again!");
            System.exit(
0);
            
        }
        
    }

}


那么对于数据输入的合法性判断,对于数据是否合理,对于程序是否能够计算所有的数,有无上界。

记住,即使不能计算,也要告诉用户,决不能给用户提供错误的答案!!

result 类型如果为int只能算到12,如果为long能算到20,如果为float只能算到32,如果为double,则可以算到170。

为什么double和long同占用8个字节,为什么表示数的范围差那么多?因为double类型支持科学计算法。

The factorial of 170 is 7.257415615307994E306

所以可能用指数方式来表达大数。

那么如果表示170以后的数的阶乘呢?可以猜用类,也可以找两个变量,一个存底数,一个存指数。


=================================

数组

int[] mark = new int[10];40字节存储空间。

-----------------------
对于局部变量,需要初始个值。

而局部变量的数组,不需要初值,会根据类型给定相应默认值。数值为0/0.0,布尔false,对象null,char /u0000

----------------------

mark 是一个int[]数组类型的对象,它保存的是那40字节存储空间的首地址。

Java中,引用和地址都保存在栈中,而具体的值开辟的空间存储在堆中。


System.arraycopy(a,0,b,0,a.length);
int[] a = {1,2,3,4,5}
int[] b = new int[10];
a.length

数组特点
1.不可变长
2.可以保存简单类型数据
3.查找快,插入和删除效率低。



2维数组

int[][] a = {{1,2},{3,4},{5,6}};
int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[1];

a类型?是一个对象,但是是虚拟机级别对象,无法用getClass().getName()显示。

哥德巴赫猜想

package com.ljl;

public class Goldbach {
    
    
public static void main(String[] args)
    
{
        
int num = Integer.parseInt(args[0]);
        
if(num < 6)
        
{
            System.out.println(
"the num must larger than six!");
            System.exit(
0);
        }

        
if(num%2 != 0)
        
{
            System.out.println(
"it should be a even!");
            System.exit(
0);
        }

        
for(int i = 1; i <= num/2; i++)
        
{
            
if(isZhiShu(i)&&isZhiShu(num-i)){
                System.out.println(num 
+ "=" + i + "+" + (num-i));
                
break;
            }

        }

        
    }

    
    
private static boolean isZhiShu(int num)
    
{
        
for(int i = 2; i <= (int)Math.sqrt(num); i++)
        
{
            
if(num%== 0)
                
return false;
        }

        
return true;
    }

}



posted on 2005-11-25 23:35 北国狼人的BloG 阅读(296) 评论(0)  编辑  收藏 所属分类: 达内学习总结

只有注册用户登录后才能发表评论。


网站导航: