Answer the question
In order to leave comments, you need to log in
What is the correct way to access a variable?
Good day, I'm a newbie, so don't be too hard on everyone.
Problem: I count the determinants of square matrices, I want to refer to the lgth variable in main, and so that the excess is not displayed, determine what dimension and output the desired method.
Here is main where I want to correctly access the lgth variable that I am reading from the Field class:
package com.company;
public class Main {
public static void main(String[] args) {
Field f1 = new Field();
f1.create();
if (f1.lgth == 1);
{
System.out.println(Field.determinant(f1.a[0][0]));
}
if (f1.lgth == 2);
{
System.out.println(Field.determinant(f1.a[0][0], f1.a[0][1], f1.a[1][0], f1.a[1][1]));
}
if (f1.lgth == 3);
{
System.out.println(Field.determinant(f1.a[0][0], f1.a[0][1], f1.a[0][2], f1.a[1][0], f1.a[1][1], f1.a[1][2], f1.a[2][0], f1.a[2][1], f1.a[2][2]));
}
}
}
package com.company;
import java.lang.*;
import java.util.Scanner;
public class Field {
public static int lgth;
int a[][];
public void create()
{
Scanner len=new Scanner(System.in);
System.out.println("Введите размерность матрицы");
int lgth = len.nextInt();
a=new int[lgth][lgth];
for (int i=0;i < a.length;i++){
for (int j=0;j < a[i].length;j++){
a[i][j]=(int)(Math.random()*10);
}
}
for (int i=0;i < a.length;i++,System.out.println()){
for (int j=0;j < a[i].length;j++){
System.out.print(a[i][j]+" ");
}
}
}
public static double determinant (int a00){
int tempdetermin = a00;
return tempdetermin;
}
public static double determinant (int a00, int a01, int a10, int a11){
int tempdetermin = (a00*a11)-(a10*a01);
return tempdetermin;
}
public static double determinant (int a00, int a01,int a02, int a10, int a11,int a12,int a20,int a21,int a22){
int tempdetermin = (a00*a11*a22)+(a10*a21*a02)+(a01*a12*a20)-(a02*a11*a20)-(a01*a10*a22)-(a00*a21*a12);
return tempdetermin;
}
}
Answer the question
In order to leave comments, you need to log in
He barely wiped the blood from his eyes and keyboard.
You have an extremely useless mess, briefly what is wrong:
1) static should be avoided in all possible ways. Imagine what you will have if you want to create another instance of Field, they will interfere with each other.
2) The Field class has no practical meaning. everything that can and cannot be static.
3) The class should execute the maximum possible "narrow" logic
4) What ,s to communicate with classes you need to use getSomethTo to get and setSomethTo to set parameters and actions.
5) Give the most meaningful names to everything, but without fanaticism
.
public class Main {
public static void main(String[] args) {
Scanner len=new Scanner(System.in);
System.out.println("Введите размерность матрицы");
int lengh = len.nextInt();
Field field1 = new Field(lengh);
System.out.println(field1.toString()());
System.out.println(field1.getDeterminant());
}
public class Field {
private int lgth;
private int a[][];
public Field(int lengh){
lgth = lenght;
a=new int[lgth][lgth];
for (int i=0;i < a.length-1;i++){
for (int j=0;j < a[i].length-1;j++){
a[i][j]=(int)(Math.random()*10);
}
}
}
public int getDeterminant(){
switch(lght):
case 1: return determinant1();
case 2: return determinant2();
case 3: return determinant3();
default: return -1; //Если не реализовано
}
private int determinant1(){
int tempdetermin = матан;
return tempdetermin;
}
private int determinant2(){
int tempdetermin = матан;
return tempdetermin;
}
private int determinant3(){
int tempdetermin = матан;
return tempdetermin;
}
@Override
public String toString(){
String str ="";
for (int i=0;i < a.length-1;i++){
for (int j=0;j < a[i].length-1;j++){
str+=(a[i][j]+" ");
}
str+="\n";
}
return str;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question