I am looking for help with my code, new to programming/development and looking for advice on how to improve.
I’ve been having issues with objects made in do
loops, unable to access them outside of the loop.
I’m not sure how to continue and I am looking for advice and suggestions or example programs for reference.
import java.util.Scanner;
public class Dungeon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to The Dungeon" +
"\nTo create a new character enter 1" +
"\nTo load a old character enter 2");
boolean check = false;
int i = 0;
do {
try {
i = Integer.parseInt(sc.nextLine());
}
catch (Exception e) {
check = true;
System.out.println("Please input a valid input");
}
switch (i) {
case 1: {
Character player = new Character();
player.factory();
check = false;
break;
}
case 2: {
System.out.println("Working");
Character player = new Character();
player.factory();
check = false;
break;
}
default: {
System.out.println("Please input a '1' or '2'");
check = true;
break;
}
}
} while(check);
System.out.println("end");
}
}
import java.util.Scanner;
public class Character {
private String name;
private String profession;
private int endurance;
private int intellect;
private int strength;
private int agility;
public Character () {
}
public void factory() {
boolean check;
int i = 0;
do {
createName();
createProfession();
displayStats();
System.out.println("Enter '1' to confirm character"
+ "\nEnter '2' to restart character creation");
i = intGet();
switch (i) {
case 1: {
check = false;
break;
}
case 2: {
check = true;
break;
}
default: {
System.out.println("Please input a '1' or '2'");
check = true;
break;
}
}
} while (check);
}
public void createName() {
String pName = "";
boolean check;
do {
System.out.println("What is your name?");
pName = stringGet();
System.out.println("Is this your name? \n" +
" '" + pName + "'" +
"\nIf 'Yes' press '1' if 'No' press '2'");
int i = 0;
i = intGet();
switch(i) {
case 1: {
check = false;
break;
}
case 2: {
check = true;
break;
}
default: {
System.out.println("Please input a '1' or '2'");
check = true;
break;
}
}
} while (check);
name = pName;
}
public void createProfession() {
System.out.println("What is your profession?"
+ "\nPress 1 for Fighter"
+ "\nPress 2 for Mage"
+ "\nPress 3 for Archer");
int i = 0;
boolean check;
do {
i = intGet();
switch(i) {
case 1: {
profession = "Fighter";
endurance = 10;
intellect = 2;
strength = 10;
agility = 0;
check = false;
break;
}
case 2: {
profession = "Mage";
endurance = 3;
intellect = 10;
strength = 2;
agility = 7;
check = false;
break;
}
case 3: {
profession = "Archer";
endurance = 5;
intellect = 2;
strength = 5;
agility = 10;
check = false;
break;
}
default: {
System.out.println("Please input a '1', '2' or '3'");
check = true;
break;
}
}
} while (check);
}
public int intGet() {
Scanner sc = new Scanner(System.in);
int i = 0;
try {
i = Integer.parseInt(sc.nextLine());
}
catch(Exception e) {
System.out.println("Please input a valid input");
}
return i;
}
public String stringGet() {
Scanner sc = new Scanner(System.in);
String i = "";
try {
i = sc.nextLine();
}
catch(Exception e) {
System.out.println("Please input a valid input");
}
return i;
}
public void displayStats()
{
System.out.println(name + " the " + profession + "'s Stats" );
System.out.println("-----------------------------");
System.out.println("Endurance:\t\t" + endurance);
System.out.println("Intellect:\t\t" + intellect);
System.out.println("Strength:\t\t" + strength);
System.out.println("Agility:\t\t" + agility);
}
}