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));
}
}
}
java里面的变量的作用范围为一个大括号的范围,大括号的范围有多大,它的作用范围就有多大如 { int a; {int b;}//这里开始就不能访问b了 {{}{{int c;}//这里开始就不能访问c了}} {} {}}//在这里之前都可以访问a
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));
Object[] objs = new Object[1];Object[] objs = {"Obj"};Object[] objs = new Object[]{"Obj"};
其实你只要给数组赋值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));
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));
}
}
}
{
int a;
{int b;}//这里开始就不能访问b了
{{}{{int c;}//这里开始就不能访问c了}}
{}
{}}//在这里之前都可以访问a
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));
}
}
}
Object[] objs = new Object[1];Object[] objs = {"Obj"};Object[] objs = new Object[]{"Obj"};
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));
}
}
}这样没有任何问题了
添加这句:
double[] arr = null, xita = null;
如果想把那个变量作为返回参数必须在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程序就可以运行.