Tuesday, December 13, 2016

Improved version of Guessing Game

import java.util.Random;
import java.util.Scanner;

public class GuessingNumber {
public static void main(String[]args){
//Generate a random number between 1 to 20
Random generator = new Random();
//store randomly generated number into variable comNum of type int
int comNum = generator.nextInt(20)+1;
//Create an object for Scanner class. 
Scanner scan = new Scanner(System.in);
boolean gameOn = true;
//create a variable to count the number of guesses. 
int guess =1;
//store user input into the variable num
int num;
System.out.printf("I am thinking of a number between 1 and 20.\n");
System.out.printf("Can you guess it?\n");
//start the while loop 
while(gameOn){
    if(guess<=4){
    System.out.printf("Enter a number to try.\n");
    num = scan.nextInt();
    if(num==comNum){
    System.out.printf("You guessed it right on %d tries\n",guess );
    break;
    }else{
    if(num<comNum){
    System.out.printf("Your guess # is lower than the one I am thinking off.\n");
   
    }else{
    System.out.printf("Your number is greater than mine.\n");
   
    }
   
    }
    }else{
    System.out.printf("You missed all your chances.The no was %d", comNum);
    break;
    }
    guess = guess+1;
}
scan.close();
}


}



Q. How to implement a loop to keep playing the game instead of restarting the program every time?

Monday, December 12, 2016

Number Guessing Game in Java

/*Description: This program implements a famous number guessing game in programming.
* There is a serious error in this code. Spot it.
*@blankbug.
*/


import java.util.Random;
import java.util.Scanner;

public class GuessingNumber {
public static void main(String[]args){
//Generate a random number between 1 to 20
Random generator = new Random();
//store randomly generated number into variable comNum of type int
int comNum = generator.nextInt(20)+1;
//Create an object for Scanner class. 
Scanner scan = new Scanner(System.in);
boolean gameOn = true;
//create a variable to count the number of guesses. 
int guess =1;
//store user input into the variable num
int num;
System.out.printf("I am thinking of a number between 1 and 20\n");
                System.out.printf("Can you guess it?");
//start the while loop 
while(gameOn){
   
    num = scan.nextInt();
    if(num==comNum){
    System.out.printf("You guessed it right on %d tries\n",guess );
    break;
    }else{
    if(num<comNum){
    System.out.printf("Your guess # is lower than the one I am thinking off.\n");
    System.out.printf("Enter it again.\n");
    }else{
    System.out.printf("Your number is greater than mine.\n");
    System.out.printf("Try it again.");
    }
   
    }
    guess++;
if(guess>4){
System.out.printf("You missed all your chances.The no was %d", comNum);
break;
}
}
}


}

----------------------------------------------------------------------------------------------------------------------
REMEMBER: There is a serious error in this program.
HINT:  Check if.. else statement inside a while loop.

Sunday, December 11, 2016

How to read a file token to token?

/*Description: this program reads a file and displays it into tokens.This program uses Scanner
class to do the job. There is no comment and explanation. If you know how to read a file then this should be obvious. This is not a good programming habit btw. We must explain or comment on the code. 
 * @author: blankbug
 * 
*/


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class StringTokenizer{
public static void main(String[]args)throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.printf("Please enter the name of the file to read.\n");
File nameOfTheFile = new File(scan.nextLine());
Scanner fileReader = new Scanner(nameOfTheFile);
String fileContent ="";
while(fileReader.hasNextLine()){
fileContent = fileReader.nextLine();
Scanner scan1 = new Scanner(fileContent);
String scan2 ="";
while(scan1.hasNext()){
scan2 = scan1.next();
System.out.println(scan2);
}//end of inner while loop
scan1.close();
}//end of outer while loop
fileReader.close();
scan.close();
}//end of the main method
}//end of the class