APCSP创建任务问题-方法在被调用后结束程序



我正在为我的AP CSP项目创建一个简短的RPG游戏,出于某种原因,当我在第310-313行调用方法Element时,它只是结束了Main中的其余代码(这是程序中所有剩余的代码(。用户需要按x键才能继续游戏,但它会跳过所有这些并自动正确填充用户输入。简而言之,一旦你在代码中选择了元素,程序就会自己完成游戏,这是不应该发生的,因为用户需要输入才能继续对话。

如上所述,该代码的预期输出是仅使用用户输入和用户信息来完成对话。请帮忙,因为这很快就要到期了!

import java.util.*;
import java.lang.*;
/**
This class is the "command and return center" for the program. Essentialy,
the main outputs and some inputs occur here.
*/
public class Main
{
private static String elemental;
private static void Element(String e)
{
Scanner scan = new Scanner (System.in);
System.out.println("Which one do you pick? Type the following letters to obtain your element.");
System.out.println("f - Fire");
System.out.println("w - Water");
System.out.println("a - Air");
System.out.println("e - Earth");
System.out.println("Type your letter now!");
e = scan.nextLine();
elemental = e;
if (e.charAt(0) == 'f')
{
e = "Fire";
}
if (e.charAt(0) == 'w')
{
e = "Water";
}
if (e.charAt(0) == 'a')
{
e = "Air";
}
if (e.charAt(0) == 'e')
{
e = "Earth";
}
System.out.println(e + ", nice pick!");
}   
public static void main (String args[]) 
{
Scanner scan = new Scanner (System.in);
int c = 0;
int trainc = 0;
ArrayList<String> hits = new ArrayList<>();
hits.add("5");
hits.add("4");
hits.add("3");
hits.add("2");
hits.add("1");
int lhits = 0;
System.out.println("What is your name?");
String name = scan.nextLine();
System.out.println("Press x to start");
String play = scan.nextLine();
String e = "";
if (play.charAt(0) == 'x' )
{
System.out.println("*HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING* This drag-a-agon is to powerful for m-e.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*Dragon Laughs*");
System.out.println("");
System.out.println("Press x to attack the dragon");
} 
hits.remove("5");
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("I have dealt no damage, even with my elemental buffs. Even with all the rare potions, I cannot deal the damage I want to the dragon."); 
System.out.println("");
System.out.println("Press x to attack the dragon");
}  
hits.remove("4");
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("I have to retreat, it looks like I am on the blind spot of the dragon. I have to go. NOW OR NEVER!!!");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*Gets out of Bed*");
System.out.println("*HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING*");
System.out.println("I-I'm back.");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("");
System.out.println("");
System.out.println("--*RESTING ARC*--");
System.out.println("");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("I forgot that I came home and rested, and...lost to that dragon.");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Huh? What is this lying in front of my door? A letter? From the-THE KING?!?!?!?!");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Why couldn't I have a nice morning *Sigh*");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Well, I guess I have to open it and found out what it says.");
System.out.println("");
System.out.println("Press x to interact with the letter");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Dear " + name + ", I have recently gotten news on your unsuccessful mission on defeating the dragon.");
System.out.println("I am dissapointed, and you will be prompted to confront my prescence this afternoon, for your journey could not have been more upsetting and shameful to this nation.");
System.out.println("");
System.out.println("Press x to continue");
} 
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Another day, another quarell with the King. Why can't we just get along. *Sigh*");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Wait...the- THE AFTERNOON!?! I have to get ready NOW!");
System.out.println("What time is it...11 pm, 11 PM!?!?!? oh brother...");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*1 Hour Passes and the Noble Knight is at the King's Door Guarded by Knights*"); 
System.out.println("Heya Guards! Wanna open this giant red door for your ol' pal " + name +"?");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*Knock**Knock*");
System.out.println("*Large Cushioned Door Opens*");
System.out.println("");
System.out.println("");
System.out.println("--*King's Arc*--");
System.out.println("");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*Mrmmr's Across The Room Became Silent*");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: " + name + ", come in and walk forth in front of my prescence.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println(name + ": Kk, whatever you say Jim.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("*Mrmmr's Across The Room Get Louder.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: YOUR MAJESTY!! CALL ME YOUR MAJESTY!! You should know your place...");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println(name + ": Okayyyy... calm down... Your Majesty...");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: DONT TELL THE KING WHAT TO DO!!! ONLY THE KING CAN TELL WHAT THE KING NEEDS TO DO!!! NOT A KNIGHT!");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x')
{
System.out.println(name + ":You just threw Democracy out the window, huh.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: You lost to the dragon, how will I know that you are ready to fight again? I can only trust you since you have the most experience fighting alone.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println(name + ": I will train, just, just give me time, I will defeat this dragon.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: Okay, come back to me when you are ready.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("");
System.out.println("");
System.out.println("--*Training Arc*--");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("I have to train, no matter how long or hard it takes, I will defeat this dragon.");
System.out.println("");
}
System.out.println("Press x as many times you want, this number will equal your years spent training.");
System.out.println("To stop training, type s");
System.out.println("Go! Your training has already begun!");
String stop = "x";
while(stop.charAt(0) != 's')
{
trainc++;
System.out.println("You have trained for " + trainc + " years.");
System.out.println("Press x to continue training or s to stop.");
stop = scan.nextLine();
}
System.out.println("King: How long has it been?");
System.out.println("");
System.out.println("Press x to continue");
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println(name + ": " + trainc + " years, boss man.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("");
System.out.println("");
System.out.println("--*Merchant Arc*--");
System.out.println("");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("King: Hey, someone call that merchant, we are getting you some armor.");
System.out.println("");
System.out.println("Press x to continue");
}
play = scan.nextLine();
if (play.charAt(0) == 'x' )
{
System.out.println("Merchant: Howdy! I have 4 elemental swords: Fire, Water, Air, and Earth. Pick one, and your super finisher ability will be based off of the element of your choice! Note that it takes 5 attacks to charge the ability!");
System.out.println("");
Element(e);
}

System.out.println("Press x to continue");
if (play.charAt(0) == 'x' )
{
System.out.println("Merchant: Since you attacked the dragon before, your charge would be less than 5, how many times did you hit the dragon?");
System.out.println("");
System.out.println("Press x to continue");
}
lhits = hits.size();
if (play.charAt(0) == 'x' )
{
System.out.println(name + ": 2 times, meaning that my charge count is " + lhits + ".");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Merchant: Great, off you go!");
System.out.println("King: Good luck out there, " + name);
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("");
System.out.println("");
System.out.println("--*Final Battle Arc*--");
System.out.println("");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("I am back at where it started.");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: *HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING*");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Oyy, wake up lazy head, I'M BAAAAAAAACK!!!");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: *ROAAAAAAAAAAAAAR*");
System.out.println("");
System.out.println("Press x to land the first blow!");
}
hits.remove("3");
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: *AHHHHHH*");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
lhits = hits.size();
System.out.println("Looks like the dragon took some damage, and my charge count is now " + lhits);
System.out.println("");
System.out.println("Press x to land the second blow!");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: *AHHHHHH*");
System.out.println("");
System.out.println("Press x to continue");
}
hits.remove("2");
if (play.charAt(0) == 'x' )
{
lhits = hits.size();
System.out.println(lhits + " more hit and I can finish the dragon!!!");
System.out.println("");
System.out.println("Press x to land the third blow!");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: *AHHHHHH*");
System.out.println("");
System.out.println("Press x to continue");
}
hits.remove("1");
if (play.charAt(0) == 'x' )
{
lhits = hits.size();
System.out.println("NOWWWWWWWWWWWWWWWWWWWW!");
System.out.println("");
System.out.println("Press x to land the final blow!");
}
if (play.charAt(0) == 'x' )
{
if(elemental.charAt(0) == 'f')
{
System.out.println("Fire element, use PHENOIX");
}
else if (elemental.charAt(0) == 'w')
{
System.out.println("Water element, use TSUNAMI");
}
else if (elemental.charAt(0) == 'a')
{
System.out.println("Air element, use TORNADO");
}
else
{
System.out.println("Earth element, use EARTHQUAKE");
}
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Dragon: AHHHHHHHHHHHHHHHHHHHHHHHhhhh-h-h..h..h...");
System.out.println("");
System.out.println("Press x to continue");
}
if (play.charAt(0) == 'x' )
{
System.out.println("Man, that training paid off, time to return home and celebrate with Jim and everyone else!");
System.out.println("");
System.out.println("Press x to end game");
}
}
}

看起来您停止了遵循一开始应用的模式。如您所见,在第310行之前,您已使用

play = scan.nextLine();
if (play.charAt(0) == 'x' )

在检查字符之前,调用扫描仪读取新行。在第310行之后,您已经停止使用play = scan.nextLine();,这意味着程序不会停止等待用户输入,而是将继续,因为player.charAt(0)将始终等于x(或之前的输入(。

查找所有检查字符输入的实例,并确保已调用nextLine()

编辑:

作为一个不得不标记大学一年级项目的人,这只是一句建议。

  • 考虑将对话存储在数组中(尤其是在对话非常线性的情况下(,然后使用while循环对其进行迭代。查找Java的行分隔符
  • 考虑当用户不按";x〃;以及如何正确处理
  • 添加一些开发人员对你为什么以某种方式做某事的评论
  • 查找DRY的概念(不要重复(,并寻找以这种方式改进代码的方法

最新更新