(Java) – how to display the output we set

This is the java code, how can i display like example:

public static void main(String[] args) {
    // TODO code application logic here
    int i = 1;
    int j = 1;


   for (i = 1 ; i <= 10; i++)
   {
       for (j = 1; j<= 5; j++)
       {
        if (i % 2 == 0 )
        {
            System.out.println ( j + "/5 = " + i /10.0);
            //the output should be 
            // 1/5 = 0.2
            // 2/5 = 0.4
            // 3/5 = 0.6
            // 4/5 = 0.8
            // 5/5 = 1.0

        }
       }

   }

    System.out.println("--------------------");
   }

 }

How i can display the output like 1/5 = 0.2 2/5 = 0.4 3/5 = 0.6 4/5 = 0.8 5/5 = 1.0

Answer

You’re doing way too much here. Simply do this:

public static void main(String[] args) {
     for (int i = 1; i<= 5; i++)
     {
         System.out.println ( i + "/5 = " + i / 5.0);
     }
     System.out.println("--------------------");
}

Output:

1/5 = 0.2
2/5 = 0.4
3/5 = 0.6
4/5 = 0.8
5/5 = 1.0
--------------------

Leave a Reply

Your email address will not be published. Required fields are marked *