Faces on Dice program in Java Programming Language

Faces on Dice program in Java Programming Language

Avatar

 BY TEJAS




All of you know java is the king of programming languages. Everybody knows java even if that student doesn't belong to the computer field. Java is the most popular programming language in the whole world.
Because of the power of java "java do everything." In today's world, java is used in almost all platforms such as Android applications, Hardware, Websites, Desktops, Servers, Databases and in many more things.

So let's get continue to our program
After a read of heading, you will get some idea about what is this program.
So in this java example we will see how to create faces which are present on the dices. This program is nothing just shows output in star marks such like this

      
          *
  
            *
          *
                *
              *
            *
      
    

We used the basics of java to create this program. We use three times public static void main.... syntax. All of you know what is this syntax.
But whenever we use this syntax every time it's meaning is different we use all this syntaxes for every specific work.



This example is a little bit hard to newbies, but you also get easily practice it and learn more in java programming. Use this sites to learn java w3Schools

In this example, we created two different functions which are isField and printDice.
Also we used for...... loops and if.....else statements. We use isField function for arranging the values and printDice for showing the output.



In below video shows program how works


below here is a Java code and output of Java program.

demo.java :-
    
        
public class demo
{
   
    public static int isField(int a, int b, int n){
        if(a == 1 && b == 1) return n % 2;

        if(a == b) return n / 4;

        if(b == 1) return 0;

        if(a == 1) return n / 6;

        if(a + b + n > 3) return 1;

        return 0;
    }

    public static void printDice(int n){

        for(int i = 0; i < 3; i++){

            for(int j = 0; j < 3; j++){

                if(isField(i,j,n) == 1){

                    System.out.print("*");

                }else{

                    System.out.print(" ");
                }
            }

            System.out.println();
        }
    }

    public static void main(String[] args) {

        for(int i = 1; i <= 6; i++){

            printDice(i);
            System.out.println();
        }
    }
}

    


output :-
    
         
      *  

      *
     *  
   
      *
     * 
    *  
   
     * *
     * *
   
     * *
      * 
     * *
   
     * *
     * *
     * *
   
   
     Process finished with exit code 0
   
    
  
    
        
public class demo
{
   
    public static int isField(int a, int b, int n){
        if(a == 1 && b == 1) return n % 2;

        if(a == b) return n / 4;

        if(b == 1) return 0;

        if(a == 1) return n / 6;

        if(a + b + n > 3) return 1;

        return 0;
    }

    public static void printDice(int n){

        for(int i = 0; i < 3; i++){

            for(int j = 0; j < 3; j++){

                if(isField(i,j,n) == 1){

                    System.out.print("*");

                }else{

                    System.out.print(" ");
                }
            }

            System.out.println();
        }
    }

    public static void main(String[] args) {

        for(int i = 1; i <= 6; i++){

            printDice(i);
            System.out.println();
        }
    }
}

    


output :-
    
         
 *  

   *
  *  

   *
  * 
 *  

  * *
  * *

  * *
   * 
  * *

  * *
  * *
  * *


  Process finished with exit code 0

    
  

We are adding advance example which is similar in this topic in this example we see only how to show faces on dices but in advance example we make game which gives output like this

Advance game in java
Thank You

Search
Advertisement
Share With
Follow us on
Advertisement