Edited: Thank you all for your help. I was able to get it working using the the skills I learned in the previous chapters and your advice. Thank you so much!
I decided to try cementing the things I have learned from Java: A Beginner's Guide by creating a simple text adventure. I am about to start Chapter 4 which involves classes and methods. The first three chapters have dealt with, if, for, while, do-while, switch, simple keyboard interaction, and break/continue.
I plan on going back after every chapter and editing it to use the new skills that I have learned. I have barely scratched the surface and I am running into a problem.
// A basic, but hopefully, lengthy text adventure.
class TextAdventure
{
public static void main(String args[])
throws java.io.IOException
{
System.out.println("\t\t BASIC TEXT ADVENTURE");
// variables I need, attributes, classes, character name, player's choice, gold
int str = 0, inte = 0, chr = 0, con = 0, dex = 0, gold;
char charName, choice;
System.out.println("Welcome player! You are about to embark upon a quest in the form of a text adventure.");
System.out.println("You will make choices, fight monsters, and seek treasure. Come back victorious and you");
System.out.println("could quite possibly buy your way into nobility!");
System.out.println();
caseChoice: {
System.out.println("Please select your class:");
System.out.println("1. Warrior");
System.out.println("2. Mage");
System.out.println("3. Rogue");
System.out.println("4. Archer");
choice = (char) System.in.read(); // Get players choice of class
switch(choice)
{
case '1':
System.out.println("You have chosen the Warrior class!");
System.out.println("You're stats are as followed:");
System.out.println("Str: 16");
System.out.println("Int: 11");
System.out.println("Chr: 14");
System.out.println("Con: 15");
System.out.println("Dex: 9");
str = 16;
inte = 11;
chr = 14;
con = 15;
dex = 9;
break;
case '2':
System.out.println("You have chosen the Mage class!");
System.out.println("You're stats are as followed:");
System.out.println("Str: 16");
System.out.println("Int: 11");
System.out.println("Chr: 14");
System.out.println("Con: 15");
System.out.println("Dex: 9");
str = 9;
inte = 16;
chr = 14;
con = 15;
dex = 11;
break;
case '3':
System.out.println("You have chosen the Rogue class!");
System.out.println("You're stats are as followed:");
System.out.println("Str: 16");
System.out.println("Int: 11");
System.out.println("Chr: 14");
System.out.println("Con: 15");
System.out.println("Dex: 9");
str = 15;
inte = 11;
chr = 14;
con = 9;
dex = 16;
break;
case '4':
System.out.println("You have chosen the Archer class!");
System.out.println("You're stats are as followed:");
System.out.println("Str: 16");
System.out.println("Int: 11");
System.out.println("Chr: 14");
System.out.println("Con: 15");
System.out.println("Dex: 9");
str = 9;
inte = 11;
chr = 14;
con = 15;
dex = 16;
break;
default:
System.out.println("Not a valid choice, please enter a digit 1-4");
break caseChoice;
}
}
}
}
The intent of the default statement in the switch is to bring the flow of code back to the class choice. I DO NOT receive a compile error or run time error. When you select anything besides 1, 2, 3, or 4. It says " Not a valid choice, please enter a digit 1-4" like it's suppose to, but the program ends.
Am I not allowed to use a label like that in a switch? Or does it not work because it's technically outside the block of code?