The Big Page o'Examples


On this page:


Nested.java

class Nested {
	public static void main(String[] args) {
		int i, j;
		for(i=0;i<10;i++) {
			for(j=i; j<10;j++) {
				System.out.print(".");
			}
			System.out.println();
		}
	}
}

output:

..........
.........
........
.......
......
.....
....
...
..
.

 


BreakLoop.java

class BreakLoop {
	public static void main(String[] args) {
		int i=0;
		while(i<100) {
			if(i==10) 
				break;
			System.out.println("i: " + i);
			i++;
		}
		System.out.println("Loop complete.");
	}
}

Output:

i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.

 


SquareRoot.java

//Compute the square root "by hand"
import tio.*;
public class SquareRoot
{  
	public static void main(String[] args) {  
		System.out.println("Please enter a number:");
		double a = Console.in.readDouble();
		double xnew = a / 2;
		double xold;
		do {
			xold = xnew;
			xnew = (xold + a / xold) / 2;
			System.out.println(xnew);
		//Test for equality between two doubles
		} while (Math.abs(xnew - xold) > 1E-4);
		System.out.println("\nSqrt is: " + xnew);
	}
}

Output:

Please enter a number:
81
21.25
12.530882352941177
9.497456198181656
9.01302783945225
9.000009415515176
9.000000000004924
Sqrt is: 9.000000000004924

 


LotteryOdds.java

// Computes the odds of winning the lottery
// in games where you draw 5 balls 1...69 or 
// somethings alongs those lines.
import tio.*;
public class LotteryOdds { 
	public static void main(String[] args) {  
		System.out.println("How many numbers do you need to draw?");
		int numbers = Console.in.readInt();
		System.out.println("What is the highest number you can draw?");
		int topNumber = Console.in.readInt();
		
		long oddsAre = 1;
		for (int i = 1; i <= numbers; i++) {  
			oddsAre = oddsAre * topNumber / i;
			topNumber--;
		}
		System.out.println("Your odds are 1 in " + oddsAre + ". Good luck!");
   }
}

Output:

How many numbers do you need to draw?
5
What is the highest number you can draw?
69
Your odds are 1 in 11238513. Good luck!

 


MultiplicationTables.java

class MultiplicationTables 
{
	public static void main(String[] args) 
	{
		for(int i=0;i<10; i++) {
			for(int j=0;j<10;j++) {
				System.out.print(i * j + "\t");
			}
		
			System.out.println();
		}
	}
}

Output:

0       0       0       0       0       0       0       0       0       0
0       1       2       3       4       5       6       7       8       9
0       2       4       6       8       10      12      14      16      18
0       3       6       9       12      15      18      21      24      27
0       4       8       12      16      20      24      28      32      36
0       5       10      15      20      25      30      35      40      45
0       6       12      18      24      30      36      42      48      54
0       7       14      21      28      35      42      49      56      63
0       8       16      24      32      40      48      56      64      72
0       9       18      27      36      45      54      63      72      81