这是为了找到矩形的周长,面积和对角线度量。
这是我的代码:
public class Exer1Sano_an{
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String L,W;
int L2,W2;
String x;
int x2;
choice(x);
measureW(W);
measureL(L);
x2=Integer.parseInt(x);
L2=Integer.parseInt(L);
W2= Integer.parseInt(W);
processing();
System.exit(0);
}// end of main meathod
// for length and width
public static String measureW(String W){
W=JOptionPane.showInputDialog("Enter the Width: ");
return W;
}
public static String measureL(String L){
L=JOptionPane.showInputDialog("Enter the Length: ");
return L;
}
// for chioce
public static String choice(String x){
x=JOptionPane.showInputDialog("n\1. Perimiter of a Rectangle n\2. Area of Rectangle n\3. Measurement of the Diagonals of a Rectangle");
return x;
}
// for computation and printing
public static void processing(){
int areaRect;
int periRect;
double diagNal;
int L2,W2;
int x2;
if (x2==1)
periRect=(2*L2)+(2*W2);
JOptionPane.showMessageDialog(null, "The perimiter of your rectanlge is" + periRect);
if (x2==2)
areaRect=L2*W2;
JOptionPane.showMessageDialog(null, "The area of your rectanlge is" + areaRect);
if (x2==3)
diagNal=Math.sqrt((L2*L2)+(W2*W2));
JOptionPane.showMessageDialog(null, "The measurement of the diagonals of your recangle is" + diagNal);
}
}// end of class
有人能帮帮我吗?它说我没有声明变量......但我已经有了。
答案 0 :(得分:0)
在初始化之前,无法访问方法范围的变量引用。
这可能是你得到的编译错误。
例如,在致电choice(x)
之前,必须为x
显式指定一个值,event为String
的默认值(null
)。
答案 1 :(得分:0)
将其更改为
import javax.swing.JOptionPane;
public class Exer1Sano_an {
public static void main(String[] args) {
String L = null, W = null;
String x = null;
choice(x);
measureW(W);
measureL(L);
processing();
System.exit(0);
}// end of main meathod
// for length and width
public static String measureW(String W) {
W = JOptionPane.showInputDialog("Enter the Width: ");
return W;
}
public static String measureL(String L) {
L = JOptionPane.showInputDialog("Enter the Length: ");
return L;
}
// for chioce
public static String choice(String x) {
x = JOptionPane
.showInputDialog("n\1. Perimiter of a Rectangle n\2. Area of Rectangle n\3. Measurement of the Diagonals of a Rectangle");
return x;
}
// for computation and printing
public static void processing() {
int areaRect = 0;
int periRect = 0;
double diagNal = 0;
int L2 = 0, W2 = 0;
int x2 = 0;
if (x2 == 1)
periRect = (2 * L2) + (2 * W2);
JOptionPane.showMessageDialog(null,
"The perimiter of your rectanlge is" + periRect);
if (x2 == 2)
areaRect = L2 * W2;
JOptionPane.showMessageDialog(null, "The area of your rectanlge is"
+ areaRect);
if (x2 == 3)
diagNal = Math.sqrt((L2 * L2) + (W2 * W2));
JOptionPane.showMessageDialog(null,
"The measurement of the diagonals of your recangle is"
+ diagNal);
}
}// end of class
答案 2 :(得分:0)
您不需要在processing()函数中再次声明x2,L2,W2。将它们移出main并使它们成为类变量。