I need to keep saving different results of the same formula

2

Hello good afternoon, I am new to this and for a university project I found myself in need of a bit of basic programming. I'll paste the script and then relatare what I need.

import java.util.Scanner;

public class ModosNormales {
    public static void main(String[] ar) {
        Scanner teclado=new Scanner(System.in);
        int nx,ny,c,n;
        float lx;
        float ly;

    float fn;
    c=172;

    System.out.print("Ingrese valor de nx:");
    nx=teclado.nextInt();
    System.out.print("Ingrese valor de ny:");
    ny=teclado.nextInt();
    System.out.print("Ingrese lx:");
    lx=teclado.nextFloat();
    System.out.print("Ingrese ly:");
    ly=teclado.nextFloat();
    fn=c*(float) Math.sqrt(Math.pow((nx/lx),2)+(float)(Math.pow((ny/ly),2)));

    System.out.print("Fn=");
    System.out.println(fn);

    for(n=1;n>0;n++) { 

    int nx1,ny1;
    float fn1;


       System.out.print("Ingrese valor de nx:");
       nx1=teclado.nextInt();
       System.out.print("Ingrese valor de ny:");
       ny1=teclado.nextInt();
       fn1=c*(float) Math.sqrt(Math.pow((nx1/lx),2)+(float)(Math.pow((ny1/ly),2)));
       System.out.print("Fn=");
       System.out.println(fn1);



       if(fn1>=500)
    {System.out.println("Frecuencia mayor o igual a 500Hz"); break;





}}}}

I need to know how many "fn1" are repeated since each "fn1" is different result because here ..

fn1 = c * (float) Math.sqrt (Math.pow ((nx1 / lx), 2) + (float) (Math.pow ((ny1 / ly), 2))); They vary the Nx1 ... lx ... etc

Then I get different "fn1" so how can I do that to save it and tell me how many fn1 are repeated? I do not know if he explains me well since I am a novice in this. I would appreciate it

    
asked by Matias Retamales 09.10.2017 в 23:50
source

1 answer

0

To begin with, the code is not very well written. I do not understand very well how you use the for. Anyway, a solution that occurs to me is that you create an array of elements, in this case float type, and save the different values in it. Imagine that you want 10 fn1

for(n=0;n<10;n++) { 

int nx1,ny1;
float[10] fn1;


   System.out.print("Ingrese valor de nx:");
   nx1=teclado.nextInt();
   System.out.print("Ingrese valor de ny:");
   ny1=teclado.nextInt();
   fn1[n]=c*(float) Math.sqrt(Math.pow((nx1/lx),2)+(float)(Math.pow((ny1/ly),2))); //en cada posición del array guardas un valor de fn1
   System.out.print("Fn=");
   System.out.println(fn1[n]);


}

To know how many are repeated, you can go through the array in the following way:

int aux = 0;

for (int i=0;i<fn1.length-1;i++)
 {
      if (fn1[i] = fn1[i+1])
 {
    aux++; //si el elemento actual es igual al siguiente, incrementamos la variable aux
  }
}

If the number of elements is variable, then this can be implemented with an ArrayList:

for(n=0;n<10;n++) { 

int nx1,ny1;
ArrayList<float> fn1= new ArrayList<>();


   System.out.print("Ingrese valor de nx:");
   nx1=teclado.nextInt();
   System.out.print("Ingrese valor de ny:");
   ny1=teclado.nextInt();
   fn1.add(n,c*(float) Math.sqrt(Math.pow((nx1/lx),2)+(float))(Math.pow((ny1/ly),2))); //en cada posición del array guardas un valor de fn1
   System.out.print("Fn=");
   System.out.println(fn1.get(n));


}


for (int i=0;i<fn1.size()-1;i++)
 {
      if (fn1.get(i) = fn1.get(i+1))
 {
    aux++; //si el elemento actual es igual al siguiente, incrementamos la variable aux
  }
}
    
answered by 10.10.2017 / 00:02
source