Skip to main content

Posts

Showing posts from 2017

SNAKE AND LADDER || JAVA GAME

import java.util.*; class SnakeLadderDemo { public static void main(String args[]) { System.out.println("\t\t\t\t\t\t\tW.E.L.C.O.M.E"); System.out.println(); System.out.println("\t\t\t\t\t\t\tThis is a game of  Snake and Ladder."); System.out.println("\t\t\t\t\t\t\tInstructions."); System.out.println(); System.out.println("\t\t\t\t\t\t\tHere you will be competing against the PC."); System.out.println("\t\t\t\t\t\t\tThere are ladders and snake if you find a snake"); System.out.println("\t\t\t\t\t\t\tyour position will be reduced to the one pointed by Snake."); System.out.println("\t\t\t\t\t\t\tIf however,you meet a ladder your position will be"); System.out.println("\t\t\t\t\t\t\tincreased to the pointed square."); System.out.println("\t\t\t\t\t\t\tThe first one to reach 100 will be the Winner."); System.out.println(); System.out.println(); System.out.println("Coded By

Outcomes of a Dice || Generating Random numbers in Java

import java.util.*; class GenerateRandomNumber { public static void main(String args[]) { int i=0; Random r=new Random(); Scanner sc=new Scanner(System.in); System.out.println("How many random numbers ,do you want to generate?"); int value=sc.nextInt(); System.out.println(); System.out.println("Generating Numbers"); for(i=0;i<value;i++) { int n=r.nextInt(6) + 1; System.out.println(n); } } } /*In the loop it is written: int n=r.nextInt(6) + 1; Here 6 inside the parentesis is the maximum value of random number,since here we are considering a dice so I have set that to 6. And the +1 indicates the minimum possible number generated.You have to include import java.util.*; library to use Random(). */

Java program to remove all whitespaces from entered string.

import java.util.*; class RemoveWhiteSpaceFromString { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your string"); String s=sc.nextLine(); System.out.println(); System.out.println("Removing Whitespaces"); System.out.println(); s=s.replaceAll("\\s+",""); System.out.println(s); } }

Entering a string and printing it in JAVA.

import java.util.*; class InputString { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter your string"); String s=sc.nextLine(); System.out.println(); System.out.print("Printing your String"); System.out.println(); System.out.print(s); } }

Java program on two dimension Matrix of String or Characters.

import java.util.*; class CharArray { public static void main(String args[]) { String a[][]; a=new String[3][3]; int i=0,j=0; Scanner sc=new Scanner(System.in); System.out.println("Enter your characters"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=sc.next(); } } System.out.println(); for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } /* Here I have used 3*3 matrix, you can also form any multi-dimension matrix by providing its size using scanner.*/