比如这个程序,就必须在外面初始化每个值(包括数组)?否则就不能运行try...catch后面的那个for循环?

解决方案 »

  1.   


    import java.util.*;
    import java.io.*;public class F02 { /**
     * @param args
     */
    public F02()
    {

    }

    public static void main(String[] args) {
    // TODO 自动生成方法存根

    int lowTem = 0, highTem = 0; // The low and high Temperature
    double lowRes = 0.0, highRes = 0.0; // The low and high Resistance
    int num = 0; // The number of data
    double[] arr, xita;

    try
    {
    Scanner sc = new Scanner(new File("F02.txt"));
    num = sc.nextInt(); // Read the number of data
    System.out.println("The number of data is:  " + num);
    arr = new double[num]; // Create the array for the data
    xita = new double[num]; // Create the array for Xita
    // Xita is transformed from data
    for (int i = 0; i < num; i++)
    {
    arr[i] = sc.nextDouble();
    }

    lowTem = sc.nextInt(); // Read the low and high Temperature
    lowRes = sc.nextDouble(); // Read the low and high Resistance
    highTem = sc.nextInt();
    highRes = sc.nextDouble();

    /*------------------Check the input-----------------------*/

    /*------------------Need Test-----------------------------*/
    if (lowTem >= highTem)  // Set the assert to abort
    assert false : "The lowest temperature you input is higher " +
    "than the highest temperature!";
    if (lowRes >= arr[0])
    assert false : "The lowest Resistance is higher than the data!";
    if (highRes <= arr[num])
    assert false : "The highest Resitance is lower than the data!";


    /*------------------Verify the input-----------------------*/
    System.out.println("The lowest tempearture you input is " + lowTem);
    System.out.println("The lowest resistance is " + lowRes);
    System.out.println("The highest tempearture you input is " + highTem);
    System.out.println("The highest resistance is " + highRes);

    }
    catch(FileNotFoundException e)
    {
    System.out.println("File not found.");
    }

    for (int i = 0; i < num; i++)
    {
    xita[i] = 273.15 + lowTem + (arr[i] - lowRes) * ((highTem - lowTem) / (highRes - lowRes));

    }

    }

    }
      

  2.   

    java里面的变量的作用范围为一个大括号的范围,大括号的范围有多大,它的作用范围就有多大如
    {
    int a;
           {int b;}//这里开始就不能访问b了
           {{}{{int c;}//这里开始就不能访问c了}}
           {}
           {}}//在这里之前都可以访问a
      

  3.   

    import java.util.*;
    import java.io.*;public class F02 {    /**
         * @param args
         */
        public F02()
        {
            
        }
        
        public static void main(String[] args) {
            // TODO 自动生成方法存根
            
            int lowTem = 0, highTem = 0;        // The low and high Temperature
            double lowRes = 0.0, highRes = 0.0;        // The low and high Resistance
            int num = 0;        // The number of data
            double[] arr, xita;
                    
            try
            {
                Scanner sc = new Scanner(new File("F02.txt"));
                num = sc.nextInt();            // Read the number of data
                System.out.println("The number of data is:  " + num);
                arr = new double[num];    // Create the array for the data
                xita = new double[num];    // Create the array for Xita
                                                    // Xita is transformed from data
                for (int i = 0; i < num; i++)
                {
                    arr[i] = sc.nextDouble();    
                }
                
                lowTem = sc.nextInt();        // Read the low and high Temperature
                lowRes = sc.nextDouble();    // Read the low and high Resistance
                highTem = sc.nextInt();
                highRes = sc.nextDouble();
                
                /*------------------Check the input-----------------------*/
                
                /*------------------Need Test-----------------------------*/
                if (lowTem >= highTem)             // Set the assert to abort        
                    assert false : "The lowest temperature you input is higher " +
                            "than the highest temperature!";
                if (lowRes >= arr[0])
                    assert false : "The lowest Resistance is higher than the data!";
                if (highRes <= arr[num])
                    assert false : "The highest Resitance is lower than the data!";
                
                
                /*------------------Verify the input-----------------------*/
                System.out.println("The lowest tempearture you input is " + lowTem);
                System.out.println("The lowest resistance is " + lowRes);
                System.out.println("The highest tempearture you input is " + highTem);
                System.out.println("The highest resistance is " + highRes);            
                
            }
            catch(FileNotFoundException e)
            {
                System.out.println("File not found.");
            }
            
            for (int i = 0; i < num; i++)//楼主是想说这里如果把num放在try catch里面定义的话,后面的try catch后面的for循环就不能用num是吧。这是因为try catch中定义的变量如果发生异常的话那他后面的for循环中根本就不认识num这个变量当然报错。不知道我理解的对不对,有不对的请指教!!!        {
                xita[i] = 273.15 + lowTem + (arr[i] - lowRes) * ((highTem - lowTem) / (highRes - lowRes));
                    
            }
            
        }
        
    }
      

  4.   

    现在它显示的是xita, arr没有初始化……另外请问能否告诉我更即时一些的联系方式呢?呵呵
      

  5.   

    都在外面初始化为空或者零之类不影响的值,因为在try语句的时候,如果异常就有的代码是执行不到的,而异常只能在动态类型检查的时候才知道,在编译阶段就得先把可能要用到的变量初始化
      

  6.   


    Object[] objs = new Object[1];Object[] objs = {"Obj"};Object[] objs = new Object[]{"Obj"};
      

  7.   

    其实你只要给数组赋值null,就可以了import java.util.*;
    import java.io.*;public class F02 {    /**
         * @param args
         */
        public F02()
        {
            
        }
        
        public static void main(String[] args) {
            // TODO 自动生成方法存根
            
            int lowTem = 0, highTem = 0;        // The low and high Temperature
            double lowRes = 0.0, highRes = 0.0;        // The low and high Resistance
            int num = 0;        // The number of data
            double[] arr=null, xita=null;     // Array initial
                    
            try
            {
                Scanner sc = new Scanner(new File("C:\\F2.txt"));
                num = sc.nextInt();            // Read the number of data
                System.out.println("The number of data is:  " + num);
                arr = new double[num];    // Create the array for the data
                xita = new double[num];    // Create the array for Xita
                                                    // Xita is transformed from data
                for (int i = 0; i < num; i++)
                {
                    arr[i] = sc.nextDouble();    
                }
                
                lowTem = sc.nextInt();        // Read the low and high Temperature
                lowRes = sc.nextDouble();    // Read the low and high Resistance
                highTem = sc.nextInt();
                highRes = sc.nextDouble();
                
                /*------------------Check the input-----------------------*/
                
                /*------------------Need Test-----------------------------*/
                if (lowTem >= highTem)             // Set the assert to abort        
                    assert false : "The lowest temperature you input is higher " +
                            "than the highest temperature!";
                if (lowRes >= arr[0])
                    assert false : "The lowest Resistance is higher than the data!";
                if (highRes <= arr[num])
                    assert false : "The highest Resitance is lower than the data!";
                
                
                /*------------------Verify the input-----------------------*/
                System.out.println("The lowest tempearture you input is " + lowTem);
                System.out.println("The lowest resistance is " + lowRes);
                System.out.println("The highest tempearture you input is " + highTem);
                System.out.println("The highest resistance is " + highRes);            
                
            }
            catch(FileNotFoundException e)
            {
                System.out.println("File not found.");
            }
            
            for (int i = 0; i < num; i++)
            {
                xita[i] = 273.15 + lowTem + (arr[i] - lowRes) * ((highTem - lowTem) / (highRes - lowRes));
                    
            }
            
        }
        
    }这样没有任何问题了
      

  8.   


    添加这句:
    double[] arr = null, xita = null;
      

  9.   

    对,try是方法,所以在{}中的就是局部变量,不能在try{}之外用。
    如果想把那个变量作为返回参数必须在try{}之外初始化。
      

  10.   

         不一定非的在外面定义,这主要看你设置的这个变量的作用域的范围而定,如果是全局变量的话就必须在try外定义,如果是局部变量的话就必须在try里面定义 .
    你程序中的
    arr = new double[num];  
    xita = new double[num];
    必须在外面定义,因为你在下面还有一个for语句使用到了这两个变量.for (int i = 0; i < num; i++)
            {
                xita[i] = 273.15 + lowTem + (arr[i] - lowRes) * ((highTem - lowTem) / (highRes - lowRes));
                    
            }
    只要在try外面定义xita   arr程序就可以运行.