Giter Club home page Giter Club logo

99-bottles-of-beer-challenge's Introduction

pasting the code in the readme

public class Runner {
	
	// How many bottles of beer do we start with? Set it when this is called!
	public static void singSong(int bottlesOfBeer) {
		
		//quick check to make sure number is not negative
		if (bottlesOfBeer < 0) {
			System.out.println("Enter a number greater than or equal to zero if you want to sing the song!");
		}
		
		// i variable to loop
		int i;
		
		// loop through all bottles of beer (going to increment rather than decrement)
		for ( i = 0 ; i <= bottlesOfBeer; i++ ) {
			
			// current bottle count
			int curBottles = bottlesOfBeer - i;
			
			// take one down
			int downBottles = curBottles - 1;
			
			// bottles of beer string
			String repString = "bottles of beer";
			
			// non plural 'bottle of beer' string
			String oneBottle = repString.substring(0,6) + repString.substring(7,15);

			// default first line
			String firstLine = curBottles + " " + repString + " on the wall, " + curBottles + " " + repString + ".\r\n";
			
			// default second line
			String secondLine = "Take one down and pass it around, " + downBottles + " " + repString + " on the wall.\r\n";
			
			// logic
			
			if ( curBottles == 0 && bottlesOfBeer == 1 ) { // for the rare case when someone enters only one bottle to start singing from
				// change the first line
				firstLine = "No more " + repString + " on the wall, no more " + repString + ".\r\n";
				
				// change the second line
				secondLine = "Go to the store and buy some more, " + bottlesOfBeer + " " + oneBottle + " on the wall.\r\n";
			} else { // using a switch as it's more readable than a lot of if and else if statements!
				switch(curBottles) {
					case 2: // logic for 2 bottles, second line only has one bottle and needs changing
						// change the second line
						secondLine = "Take one down and pass it around, " + downBottles + " " + oneBottle + " on the wall.\r\n";
						break;
					case 1: // logic for 1 bottle, first line only has one and second line has 'no more'
						// change the first line
						firstLine = curBottles + " " + oneBottle + " on the wall, " + curBottles + " " + oneBottle + ".\r\n";
						// change the second line
						secondLine = "Take one down and pass it around, no more " + repString + " on the wall.\r\n";
						break;
					case 0: // logic for 0 bottles, both lines need alterations from the default
						// change the first line
						firstLine = "No more " + repString + " on the wall, no more " + repString + ".\r\n";
						// change the second line
						secondLine = "Go to the store and buy some more, " + bottlesOfBeer + " " + repString + " on the wall.\r\n";	
						break;
				}
			}
			
			// print first and second line
			System.out.println(firstLine + secondLine);
			
		}
	}
	

	public static void main(String[] args) {
		
		// sing the song starting at whatever number you want!  
		singSong(99);

	}

}

99-bottles-of-beer-challenge's People

Contributors

cameronguthrie avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.