基本上,我希望用户选择Dwarf
或Human
。
问题是,如果做出了另一个选择,例如Cyclops
,则流程结束。
以下是我的代码。
//Race.java
public class Race{
private String race;
private int might;
private int reflex;
private int durability;
private int smarts;
private int bravado;
public Race(){
}
public String getRace(){
return race;
}
public void setRace(String race){
this.race = race;
}
public int getMight(){
return might;
}
public void setMight(int might){
this.might = might;
}
public int getReflex(){
return reflex;
}
public void setReflex(int reflex){
this.reflex = reflex;
}
public int getDurability(){
return durability;
}
public void setDurability(int durability){
this.durability = durability;
}
public int getSmarts(){
return smarts;
}
public void setSmarts(int smarts){
this.smarts = smarts;
}
public int getBravado(){
return bravado;
}
public void setBravado(int bravado){
this.bravado = bravado;
}
}
//RaceCreator.java
import java.util.Scanner;
public class RaceCreator{
public static void main(String[] args) {
Race dwarf = new Race();
dwarf.setRace("Dwarf");
dwarf.setMight(4);
dwarf.setReflex(2);
dwarf.setDurability(4);
dwarf.setSmarts(3);
dwarf.setBravado(2);
Race human = new Race();
human.setRace("Human");
human.setMight(3);
human.setReflex(3);
human.setDurability(3);
human.setSmarts(3);
human.setBravado(3);
System.out.println("Are you Human or Dwarf?");
Scanner scanRace = new Scanner(System.in);
String inputRace = scanRace.next();
//System.out.println("Hello down there " + inputRace + "!");
if (inputRace.equalsIgnoreCase("Dwarf")) {
System.out.println("Greetings " + dwarf.getRace() + "!");
System.out.println("Your Might is " + dwarf.getMight() + " ... Impressive!");
System.out.println("Your Reflex is " + dwarf.getReflex() + " ... Below Average.");
System.out.println("Your Durability is " + dwarf.getDurability() + " ... Impressive!");
System.out.println("Your Smarts are " + dwarf.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + dwarf.getBravado() + " ... Below Average.");
} else if (inputRace.equalsIgnoreCase("Human")) {
System.out.println("Greetings " + human.getRace() + "!");
System.out.println("Your Might is " + human.getMight() + " ... Average.");
System.out.println("Your Reflex is " + human.getReflex() + " ... Average.");
System.out.println("Your Durability is " + human.getDurability() + " ... Average.");
System.out.println("Your Smarts are " + human.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + human.getBravado() + " ... Average.");
}
}
}
我查了很多关于while
,do
和break
的教程,但他们都使用整数,或只使用1个选项,而不是2(Dwarf
或{ {1}})。
答案 0 :(得分:1)
原因是在if-else-if语句之后没有更多代码,因此,main方法将返回,因此程序结束(程序在主方法结束时结束)。
if (inputRace.equalsIgnoreCase("Dwarf")) {
// ...
} else if (inputRace.equalsIgnoreCase("Human")) {
// ...
}
// There is no code after this, so the main method will return and the program will end.
要让程序继续,直到用户选择了有效选项,请使用以下代码检索用户的输入。
String inputRace = null;
System.out.print("Please choose either a dwarf or human: ");
while(scanRace.hasNextLine()){
inputRace = scanRace.nextLine(); // Get the next line of input
// Exit the loop if the choice is valid
if(inputRace.equalsIgnoreCase("dwarf") || inputRace.equalsIgnoreCase("human")) break;
else System.pout.print("Invalid choice, please enter the choice again: ");
}
// "inputRace" now holds the valid choice (either dwarf or human)
// After this should be your current if-else block.
而不是
String inputRace = scanRace.next();
答案 1 :(得分:0)
这将提示你直到不会插入矮人或人类
取而代之的是:
System.out.println("Are you Human or Dwarf?");
Scanner scanRace = new Scanner(System.in);
String inputRace = scanRace.next();
使用此:
Scanner scanRace = new Scanner(System.in);
String inputRace;
while(scanRace.hasNext()){ // or while(true)
System.out.println("Are you Human or Dwarf?");
inputRace = scanRace.next();
if(inputRace.equalsIgnoreCase("Dwarf") || inputRace.equalsIgnoreCase("Human"))
break;
}
答案 2 :(得分:0)
您为自己的程序创建的内容是A或B,如果未选择Dwarf或human,则不会退回。您可以使用 While循环。
// for a single person
String race = racescan.next(); //you create a scanner class and retrieve it
int loopCounter =0;
while(loopCounter < 1)
{
if (race.equalsIgnoreCase("dwarf") {
System.out.println("Greetings " + dwarf.getRace() + "!");
System.out.println("Your Might is " + dwarf.getMight() + " ... Impressive!");
System.out.println("Your Reflex is " + dwarf.getReflex() + " ... Below Average.");
System.out.println("Your Durability is " + dwarf.getDurability() + " ... Impressive!");
System.out.println("Your Smarts are " + dwarf.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + dwarf.getBravado() + " ... Below Average.");
loopCounter++;
}
else if (race.equalsIgnoreCase("Human")) {
System.out.println("Greetings " + human.getRace() + "!");
System.out.println("Your Might is " + human.getMight() + " ... Average.");
System.out.println("Your Reflex is " + human.getReflex() + " ... Average.");
System.out.println("Your Durability is " + human.getDurability() + " ... Average.");
System.out.println("Your Smarts are " + human.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + human.getBravado() + " ... Average.");
loopCounter++;
}
} // end of while loop
如果您想为双人游戏创建它,那么您可以在退出之前获得您需要实现的循环次数。
答案 3 :(得分:0)
精彩回应大家! (特别是 SamTebbs33 )我非常感谢他们所有人,谢谢!此外,如果此论坛上的任何其他初学者Java成员感兴趣,我将继续致力于这个简单的“选择你自己的冒险/角色扮演”风格的游戏,如果你给我发私信,我们可以慢慢地将这个基础基础建立在更丰富的东西未来几个月情况会很复杂。
现在这是代码的样子,它运行得很好!
Race.java
public class Race{
private String race;
private int might;
private int reflex;
private int durability;
private int smarts;
private int bravado;
private int fortune;
private int sense;
public Race(){
}
public String getRace(){
return race;
}
public void setRace(String race){
this.race = race;
}
public int getMight(){
return might;
}
public void setMight(int might){
this.might = might;
}
public int getReflex(){
return reflex;
}
public void setReflex(int reflex){
this.reflex = reflex;
}
public int getDurability(){
return durability;
}
public void setDurability(int durability){
this.durability = durability;
}
public int getSmarts(){
return smarts;
}
public void setSmarts(int smarts){
this.smarts = smarts;
}
public int getFortune(){
return fortune;
}
public void setFortune(int fortune){
this.fortune = fortune;
}
public int getBravado(){
return bravado;
}
public void setBravado(int bravado){
this.bravado = bravado;
}
public int getSense(){
return sense;
}
public void setSense(int sense){
this.sense = sense;
}
}
RaceCreator.java
import java.util.Scanner;
public class RaceCreator {
public static void main(String[] args) {
Race dwarf = new Race();
dwarf.setRace("Dwarf");
dwarf.setMight(7);
dwarf.setReflex(5);
dwarf.setDurability(7);
dwarf.setSmarts(6);
dwarf.setBravado(5);
dwarf.setFortune(7);
dwarf.setSense(5);
Race human = new Race();
human.setRace("Human");
human.setMight(6);
human.setReflex(6);
human.setDurability(6);
human.setSmarts(6);
human.setBravado(6);
human.setFortune(6);
human.setSense(6);
Profession gambler = new Profession();
gambler.setProfession("Gambler");
Profession bard = new Profession();
bard.setProfession("Bard");
Profession gladiator = new Profession();
gladiator.setProfession("Gladiator");
Profession pickpocket = new Profession();
pickpocket.setProfession("Pickpocket");
Profession wanderer = new Profession();
wanderer.setProfession("Wanderer");
Profession scholar = new Profession();
scholar.setProfession("Scholar");
System.out.println();
System.out.println("Welcome to Choice!");
System.out.println();
Scanner scanRace = new Scanner(System.in);
String inputRace = null;
System.out.println("Are you Human or Dwarf?");
while (scanRace.hasNextLine()) {
inputRace = scanRace.nextLine();
if (inputRace.equalsIgnoreCase("Dwarf") || inputRace.equalsIgnoreCase("Human"))
break;
else System.out.println("Please choose a valid race.");
}
System.out.println();
if (inputRace.equalsIgnoreCase("Dwarf")) {
System.out.println("Greetings " + dwarf.getRace() + "!");
System.out.println("Your Might is " + dwarf.getMight() + " ... Impressive!");
//if(dwarf.getMight() < 6 ){ System.out.println("Average");} else if (dwarf.getMight() > 6 ){ System.out.println("Great!");}
System.out.println("Your Reflex is " + dwarf.getReflex() + " ... Below Average.");
System.out.println("Your Durability is " + dwarf.getDurability() + " ... Impressive!");
System.out.println("Your Smarts are " + dwarf.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + dwarf.getBravado() + " ... Below Average.");
System.out.println("Your Fortune is " + dwarf.getFortune() + " ... Impressive!");
System.out.println("Your Sense is " + dwarf.getSense() + " ... Below Average.");
} else if (inputRace.equalsIgnoreCase("Human")) {
System.out.println("Greetings " + human.getRace() + "!");
System.out.println("Your Might is " + human.getMight() + " ... Average.");
System.out.println("Your Reflex is " + human.getReflex() + " ... Average.");
System.out.println("Your Durability is " + human.getDurability() + " ... Average.");
System.out.println("Your Smarts are " + human.getSmarts() + " ... Average.");
System.out.println("Your Bravado is " + human.getBravado() + " ... Average.");
System.out.println("Your Fortune is " + human.getFortune() + " ... Average.");
System.out.println("Your Sense is " + human.getSense() + " ... Average.");
}
System.out.println();
Scanner scanProfession = new Scanner(System.in);
String inputProfession = null;
System.out.println("What was your previous Profession? Gambler, Bard, Gladiator, Pickpocket, Wanderer or Scholar?");
while (scanProfession.hasNextLine()) {
inputProfession = scanProfession.nextLine();
System.out.println();
if (inputProfession.equalsIgnoreCase("Gambler") ||
inputProfession.equalsIgnoreCase("Bard") ||
inputProfession.equalsIgnoreCase("Gladiator") ||
inputProfession.equalsIgnoreCase("Pickpocket") ||
inputProfession.equalsIgnoreCase("Wanderer") ||
inputProfession.equalsIgnoreCase("Scholar"))
break;
else System.out.println("Please choose a valid profession.");
}
if (inputProfession.equalsIgnoreCase("Gambler")) {
System.out.println("Luck be with you ... " + gambler.getProfession() + ".");
System.out.println(" *+1 Bravado, +2 Sense, +3 Fortune*");
dwarf.setBravado(dwarf.getBravado() + 1);
human.setBravado(human.getBravado() + 1);
dwarf.setSense(dwarf.getSense() + 2);
human.setSense(human.getSense() + 2);
dwarf.setFortune(dwarf.getFortune() + 3);
human.setFortune(human.getFortune()+3);
} else if (inputProfession.equalsIgnoreCase("Bard")) {
System.out.println("Music to my ears ... " + bard.getProfession() + ".");
System.out.println(" *+1 Fortune, +2 Smarts, +3 Bravado*");
dwarf.setFortune(dwarf.getFortune()+1);
human.setFortune(human.getFortune()+1);
dwarf.setSmarts(dwarf.getSmarts()+2);
human.setSmarts(human.getSmarts()+2);
dwarf.setBravado(dwarf.getBravado()+3);
human.setBravado(human.getBravado()+3);
} else if (inputProfession.equalsIgnoreCase("Gladiator")) {
System.out.println("Freedom from savagery ... " + gladiator.getProfession() + ".");
System.out.println(" *+1 Reflex, +2 Might, +3 Durability*");
dwarf.setReflex(dwarf.getReflex()+1);
human.setReflex(human.getReflex()+1);
dwarf.setMight(dwarf.getMight()+2);
human.setMight(human.getMight()+2);
dwarf.setDurability(dwarf.getDurability()+3);
human.setDurability(human.getDurability()+3);
} else if (inputProfession.equalsIgnoreCase("Pickpocket")) {
System.out.println("A cunning ... " + pickpocket.getProfession() + ".");
System.out.println(" *+1 Fortune, +2 Sense, +3 Reflex*");
dwarf.setFortune(dwarf.getFortune() + 1);
human.setFortune(human.getFortune() + 1);
dwarf.setSense(dwarf.getSense() + 2);
human.setSense(human.getSense() + 2);
dwarf.setReflex(dwarf.getReflex() + 3);
human.setReflex(human.getReflex() + 3);
} else if (inputProfession.equalsIgnoreCase("Wanderer")) {
System.out.println("To the roads less traveled ... " + wanderer.getProfession() + ".");
System.out.println(" *+1 Durability, +2 Smarts, +3 Sense*");
dwarf.setDurability(dwarf.getDurability() + 1);
human.setDurability(human.getDurability() + 1);
dwarf.setSmarts(dwarf.getSmarts() + 2);
human.setSmarts(human.getSmarts() + 2);
dwarf.setSense(dwarf.getSense() + 3);
human.setSense(human.getSense() + 3);
} else if (inputProfession.equalsIgnoreCase("Scholar")) {
System.out.println("An ever curious ... " + scholar.getProfession() + ".");
System.out.println(" *+1 Bravado, +2 Sense, +3 Smarts*");
dwarf.setBravado(dwarf.getBravado()+1);
human.setBravado(human.getBravado()+1);
dwarf.setSense(dwarf.getSense()+2);
human.setSense(human.getSense()+2);
dwarf.setSmarts(dwarf.getSmarts()+3);
human.setSmarts(human.getSmarts()+3);
}
System.out.println();
System.out.println("Press \"ENTER\" to continue...");
Scanner pressEnter = new Scanner(System.in);
pressEnter.nextLine();
System.out.println("You are a *" + inputRace + " " + inputProfession + "* and your final statistics are:");
if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Gambler")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Bard")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Gladiator")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Pickpocket")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Wanderer")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Dwarf") && inputProfession.equalsIgnoreCase("Scholar")) {
System.out.println("Might " + (dwarf.getMight()));
System.out.println("Reflex " + (dwarf.getReflex()));
System.out.println("Durability " + (dwarf.getDurability()));
System.out.println("Smarts " + (dwarf.getSmarts()));
System.out.println("Bravado " + (dwarf.getBravado()));
System.out.println("Fortune " + (dwarf.getFortune()));
System.out.println("Sense " + (dwarf.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Gambler")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Bard")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Gladiator")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Pickpocket")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Wanderer")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
} else if (inputRace.equalsIgnoreCase("Human") && inputProfession.equalsIgnoreCase("Scholar")) {
System.out.println("Might " + (human.getMight()));
System.out.println("Reflex " + (human.getReflex()));
System.out.println("Durability " + (human.getDurability()));
System.out.println("Smarts " + (human.getSmarts()));
System.out.println("Bravado " + (human.getBravado()));
System.out.println("Fortune " + (human.getFortune()));
System.out.println("Sense " + (human.getSense()));
}
}
}
Profession.java
public class Profession{
private String profession;
private String gambler;
private String bard;
private String gladiator;
private String pickpocket;
private String wanderer;
private String scholar;
public Profession(){
}
public String getProfession(){
return profession;
}
public void setProfession(String profession){
this.profession = profession;
}
public String getGambler(){
return gambler;
}
public void setGambler(String gambler){
this.gambler = gambler;
}
public String getBard(){
return bard;
}
public void setBard(String bard){
this.bard = bard;
}
public String getGladiator(){
return gladiator;
}
public void setGladiator(String gladiator){
this.gladiator = gladiator;
}
public String getPickpocket(){
return pickpocket;
}
public void setPickpocket(String pickpocket){
this.pickpocket = pickpocket;
}
public String getWanderer(){
return wanderer;
}
public void setWanderer(String wanderer){
this.wanderer = wanderer;
}
public String getScholar(){
return scholar;
}
public void setScholar(String scholar){
this.scholar = scholar;
}
}