Loading [MathJax]/extensions/MathMenu.js

Sunday, 8 March 2015

Two simple Java programs

So far I’m still a newbie Java programmer and I am mostly familiar with the basics (the syntax and basic concepts) therefore I thought I could practice my little knowledge by writing two simple programs. In particular, this task helped me to get more familiar with user input, random number generation and arrays. I took the idea from a website, however I lost the bookmark therefore I can not post it.

The first program is quite simple. It asks you to guess a number within 0 and 99. Many improvements could be done to this simple program, perhaps in the future I’ll work on it.

Here it is:

import java.util.Scanner;
import java.util.Random;
public class GuessNumberMain
{
public static void main(String[] args)
{
System.out.println("Guess the number beween 0 and 99");
Scanner reader = new Scanner(System.in);
int number = reader.nextInt();
Random randomGenerator = new Random();
int toGuess = randomGenerator.nextInt(100);
while(number != toGuess)
{
if(number < toGuess)
{
System.out.println("Higher");
}else if(number > toGuess)
{
System.out.println("Lower");
}
System.out.println("Try again!");
number = reader.nextInt();
}
System.out.println("Congrats! You guessed it! It was: "+number);
reader.close();
}
}

The second program is a little bit more complex, it asks for an input sentence from the user and then it outputs a sentence of the same length using random part of the entered sentence.

import java.util.Scanner;
import java.util.Random;
public class SentenceGeneratorMain
{
public static void main(String[] args)
{
//Variables declaration and initialization
Scanner reader = new Scanner(System.in);
String input = new String();
String reply = new String();
Random rGenerator = new Random();
boolean again = true;
boolean first = true;
//Program description
String description = "This simple program takes a sentence as input and then\n"
+ "it prints another sentence of the same length placing the words\n"
+ "in a random place. Enter a sentence to start. After it has run\n"
+ "the program asks you if you'd like to play again, enter 'no' to\n"
+ "terminate the program otherwise enter a random character.\n"
+ "------------------------------------------------------------------\n"
+ "Let's start:\n\n";
//Initial prints
System.out.println(description);
System.out.println("Enter a sentence: ");
//Main loop
while(again)
{
if(!first){System.out.println("Enter a sentence: ");}
else{first = false;}
//Reading the input and storing the string as an array
input = reader.nextLine();
String[] array = new String[input.split(" ").length];
for(int i=0; i< array.length; i++)
{
array[i] = input.split(" ")[i];
}
//Sentence generation
for(int i=0; i < array.length; i++)
{
int k = rGenerator.nextInt(array.length);
System.out.print(array[k]);
System.out.print(" ");
}
//Play again?
System.out.println("\nPlay again?");
reply = reader.nextLine();
if("no".equals(reply) || "No".equals(reply)){again = false;}
}
reader.close();
}
}

Hope this was interesting.

No comments:

Post a Comment