我想知道如何使用变量
a[i][j]
在方法
Scores()
在
MD()
方法中使用它
和
sumD()
在以下代码中:
在我的代码中,方法
MD()
和
sumD()
无法获得结果。
public class Test3 {
public void Scores() {
double[][] a= new double[3][5];
int i,j;
for(i=0; i<3; i++ ){
for(j=0; j<5; j++){
a[i][j]= (double) Math.random();
System.out.println("a[" + i + "][" + j + "] = " +a[i][j]);
}
}
}
public void MD(){
double[][] b =new double[3][5];
int [] m = new int[5];
int i,j;
//double[][] a= new double[3][5];
for(j= 0; j<5; j++)
for(i=0 ; i<3 ; i++)
{
b[i][j]=0.0;
if(a[i][j]>0.0)
m[j]++;
}
for(j= 0; j<5; j++){
for(i=0 ; i<3 ; i++) {
if(a[i][j] > 0.0){
b[i][j]=a[i][j]*m[j];
System.out.println("b[" + i + "][" + j + "] = " + b[i][j]);
}
}
}
}
public void sumD(){
int i,j,n;
double[] sum= new double[3];
double[] k= new double[3];
//double[][] a= new double[3][5];
for(i=0; i<3; i++){
n=0;
sum[i]=0.0;
for(j=0; j<5; j++){
if(a[i][j]>0.0){
sum[i] += (a[i][j])*2;
n++;
}
}
k[i]=sum[i]/n;
System.out.println("k[" + i + "] = " + k[i]);
}
}
public static void main(String[] args){
Test3 print= new Test3();
print.Scores();
print.MD();
print.sumD();
}
}
预先感谢。
最新回答
- 2021-1-111 #
- 2021-1-112 #
看起来像您在使用实例方法而不是静态方法。
如果您不想创建对象,则应将所有方法声明为静态,因此类似
private static void methodName(Argument args...)
如果希望所有这些方法都可以访问变量,则应在方法之外对其进行初始化,并限制其范围,将其声明为私有。
private static int[][] array = new int[3][5];
通常会忽略全局变量(特别是对于像您这样的情况),因为在大型程序中它们可能造成严重破坏,因此将其设置为私有至少可以避免某些问题。
此外,我会说平常的:您应该尝试使代码保持整洁.使用描述性的类,方法和变量名称,并保持代码整洁(带有适当的缩进,换行符等)并保持一致。
这是代码的最终示例(简化):
public class Test3 { //Use this array in your methods private static int[][] scores = new int[3][5]; /* Rather than just "Scores" name it so people know what * to expect */ private static void createScores() { //Code... } //Other methods... /* Since you're now using static methods, you don't * have to initialise an object and call its methods. */ public static void main(String[] args){ createScores(); MD(); //Don't know what these do sumD(); //so I'll leave them. } }
理想情况下,由于您使用的是数组,因此您将在main方法中创建该数组,并将其作为参数传递给每个方法,但是解释它的工作方式可能是一个全新的问题,所以我将
- 2021-1-113 #
只要移动
a
的声明即可 使它成为Test3
类的私有财产 ,就像这样:public class Test3 { private double[][] a; public void Scores() { a= new double[3][5]; int i,j; ...etc...
- 2021-1-114 #
只需将a [i] [j]设为类变量,并在
Scores()
外部进行声明 ,就在班级名称的下方public class Test3 { double[][] a= new double[3][5]; public void Scores() { .... } ..... }
- 2021-1-115 #
,您可以将二维数组指针声明为该类的成员.然后在成员函数中声明大小和值.在这种情况下,您可以通过另一个功能来访问它。
请参见此代码中T [] []的使用
public class Knapsack { //private static Scanner in; int T[][]; int MaximumVal(int wt[],int val[], int total){ int l=total; int m=val.length; T = new int[m+1][l+1]; for (int i=0; i<=m; i++){ for(int j=0; j<=l; j++){ if(i==0 || j==0){ T[i][j]=0; continue; } if(j-wt[i-1] >= 0){ T[i][j]=Math.max(T[i-1][j], val[i-1]+T[i-1][j-wt[i-1]]); } else{ T[i][j]=T[i-1][j]; } } } return T[m][l]; } void PrintPath(int wt[], int val[]){ int i=T.length-1; int j=T[0].length-1; while(j!=0){ if(i>0){ while(T[i][j]==T[i-1][j]){ i--; } } System.out.print(wt[i-1]+" "); j=j-wt[i-1]; i--; } } //Main Function with test case :: public static void main(String args[]){ int wt[]={1,3,4,5}; int val[]={1,4,5,7}; Knapsack K = new Knapsack(); System.out.print("Enter the total value: "); //in = new Scanner(System.in); //int total = in.nextInt(); int result = K.MaximumVal(wt,val,7); // pass total System.out.println("Total value is "+ result); K.PrintPath(wt,val); } }
相关问题
- reflection:如何拦截具有标准Java功能(无AspectJ等)的方法调用?javareflectionmethods2021-01-12 01:28
- 从Java方法返回多个值:为什么没有n元组对象?javamethodsreturntype2021-01-12 00:56
您不能.在方法内部定义的变量是该方法的局部变量。
如果要在方法之间共享变量,则需要将它们指定为类的成员变量.另外,您也可以将它们从一种方法作为参数传递给另一种方法(这并不总是适用)。