Month: July 2015

Screenshot of the Switch Statement in Eclipse

Text Games – Switch Statements

The thing about text games it could be fun if the story is interesting and the players has lots of choices. But here I just tried something real quick as I was learning about switch statements. Apparently advanced programmers don’t really make use of switch statements but it’s a nice to know. Well I gave it a go and this is what I was able to come up with:

import java.util.Scanner;

public class SwitchStatements {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out
.println(“You are at home. Your father walks into your room and asks you to choose a flavour of pop fizz. What do you choose?”);
String text = input.nextLine();

switch (text) {

case “orange”:
System.out
.println(“Your father says there is no orange flavoured pop fizz. Run to the store and get some.”);
break;
case “bubblegum”:
System.out
.println(“Your dad hands you some bubblegum flavoured pop fizz. Your dad leaves the room.When you open the can of pop fizz, it sprays in your face.”);
break;
case “strawberry”:
System.out
.println(“Your dad tells you that your sister wants to have the strawberry. Do you give her the strawberry and take the bubblegum pop fizz?”);
String giveFizzToSister = input.nextLine();
switch (giveFizzToSister) {
case “yes”:
System.out
.println(“Your dad hands you some bubblegum flavoured pop fizz. Your dad leaves the room.When you open the can of pop fizz, it sprays in your face.”);
break;
case “no”:
System.out
.println(“Your dad calls you selfish and throws the strawberry pop fizz in your face. Your face starts bleeding and you faint.”);
break;
default:
System.out
.println(“Your father couldn’t understand what you said. He leaves the room.”);
}
}
}
}

I faced a couple of challenges with this especially the switch (text) part. I had to google a solution. Found it on stackover. It involved changing my java compiler properties to 1.7 because the way it was set up it wouldn’t allow a string to be defined as a switch statement.

The “/n” new line command doesn’t seem to work. It doesn’t move the text to a new line. Eg. I would enter the text like this:

System.out.println(“Your dad enters the room./nYour dad asks if you want some pop fizz”);

Actual output:

Your dad enters the room./nYour dad asks if you want some pop fizz

Desired output:

Your dad enters the room.

Your dad asks if you want some pop fizz

I will try to find a solution for my problem. Hope whoever is reading finds this useful. Thanks for reading.

Advertisements