Java array assignment (multiple values)

后端 未结 10 1564
醉梦人生
醉梦人生 2020-12-24 06:45

I have a Java array defined already e.g.

float[] values = new float[3];

I would like to do something like this further on in the code:

10条回答
  •  渐次进展
    2020-12-24 07:16

        public class arrayFloats {
          public static void main (String [] args){
            float [] Values = new float[3];
            float Incre = 0.1f;
            int Count = 0;
    
            for (Count = 0;Count<3 ;Count++ ) {
              Values [Count] = Incre + 0.0f;
              Incre += 0.1f;
              System.out.println("Values [" + Count + "] : " + Values [Count]);
            }
    
    
          }
        }
    
    //OUTPUT:
    //Values [0] : 0.1
    //Values [1] : 0.2
    //Values [2] : 0.3
    

    This isn't the all and be all of assigning values to a specific array. Since I've seen the sample was 0.1 - 0.3 you could do it this way. This method is very useful if you're designing charts and graphs. You can have the x-value incremented by 0.1 until nth time.

    Or you want to design some grid of some sort.

提交回复
热议问题