import java.util.Random;
import java.util.Scanner;
public class GuessNum {
public GuessNum() {
int numRandom = 0;
int numGuess;
int life = 5;
String want = "";
Random rand = new Random();
Scanner scan = new Scanner(System.in);
do {
int lifeLeft = 5;
numRandom = rand.nextInt(9)+1;
// System.out.print("the number:"+numRandom);
System.out.print("\nGuess the Number [1..10]\n");
System.out.print("===================\n");
System.out.print("You have " + lifeLeft + " chances.\n");
do {
do {
System.out.print("What number do I have in mind: ");
numGuess = scan.nextInt();
if (numGuess < 1 || numGuess > 10)
System.out.println("Invalid input. Range is 1-10.");
} while (numGuess < 1 || numGuess > 10);
if (numGuess != numRandom && lifeLeft != 0)
System.out.println("Wrong! You only have " + --lifeLeft + " chances left.");
} while (numGuess!=numRandom && lifeLeft > 0);
if (numGuess == numRandom)
System.out.println("Correct! -- in " + (life - lifeLeft) + " guess(es).");
if (lifeLeft == 0) {
System.out.println("You have no more lives..");
System.out.println("This is the number: " + numRandom);
}
System.out.print("\nEnter 'y' if you want to play again or any other character to exit: ");
want = scan.nextLine();
} while (want.equals("y") || want.equals("Y"));
}
public static void main(String[] args) {
new GuessNum();
}
}
This is best blog for software engineer can learn c, c++,Java,c# javascript ,vb script and sql query. All are basics for web application and applet
Wednesday, February 5, 2014
Monday, February 3, 2014
Prime number program in Java
Prime number function using Static function
import java.io.*;
public class Prime
{
public static void main (String[] arqs)
{
Console console = System.console();
String sPrimenumber=console.readLine();
int iPrimenumber=Integer.parseInt(sPrimenumber);
if(primeNumber(iPrimenumber))
{
System.out.println("prime");
}
else
System.out.println("not prime");
}
static boolean primeNumber(int number)
{
for(int i=2;i {
if(number%i==0)
{
return false;
}
}
return true;
}
}
====================================================
Prime number using class's function
import java.io.*;
public class Prime
{
public static void main (String[] arqs)
{
Console console = System.console();
String sPrimenumber=console.readLine();
int iPrimenumber=Integer.parseInt(sPrimenumber);
Number objNumber=new Number();
if(objNumber.testNumber(iPrimenumber))
{
System.out.println("prime");
}
else
System.out.println("not prime");
}
}
class Number
{
public boolean testNumber(int number)
{
for(int i=2;i {
if(number%i==0)
{
return false;
}
}
return true;
}
}
============================================
import java.io.*;
public class Prime
{
public static void main (String[] arqs)
{
Console console = System.console();
String sPrimenumber=console.readLine();
int iPrimenumber=Integer.parseInt(sPrimenumber);
if(primeNumber(iPrimenumber))
{
System.out.println("prime");
}
else
System.out.println("not prime");
}
static boolean primeNumber(int number)
{
for(int i=2;i
if(number%i==0)
{
return false;
}
}
return true;
}
}
import java.io.*;
public class Prime
{
public static void main (String[] arqs)
{
Console console = System.console();
String sPrimenumber=console.readLine();
int iPrimenumber=Integer.parseInt(sPrimenumber);
Number objNumber=new Number();
if(objNumber.testNumber(iPrimenumber))
{
System.out.println("prime");
}
else
System.out.println("not prime");
}
}
class Number
{
public boolean testNumber(int number)
{
for(int i=2;i
if(number%i==0)
{
return false;
}
}
return true;
}
}
============================================
Subscribe to:
Posts (Atom)