質問: 3 番目のオプションの後に続行できるようにプログラムを修正するにはどうすればよいですか?
短いアドベンチャー ゲームを作成していますが、いくつか問題があるようです。私はこれにまったく慣れていないので、アドバイスは素晴らしいです。
主な問題は、プレーヤーが「はい」または「いいえ」をクリックしたときです。
<i>Go into the abandon house?</i>
<i>1.I don't see why not.</i>
<i>2.I rather stay outside,</i>(user_name)<i> snickers.</i>
プレーヤーが選択を行った後、プログラムは終了します。プログラムは、プレイヤーが選択した後も戦いを続けることになっていますが、違います!
これが私のコードです:
import java.util.Scanner;
public class NewAdventureProject {
public static void main(String[] args)
throws java.io.IOException{
int MonsterHealth;
int PlayerHealth ;
int GhostHealth;
char input='a';//The attack key for battles
PlayerHealth=100;//Health of the player
MonsterHealth=100;//Health of the Monster
GhostHealth=120;//Health of the ghost
Scanner user_input=new Scanner(System.in);
String user_name;
System.out.println("Enter Your name or prefered nick name:");//player name option
user_name=user_input.next();
System.out.println(user_name+" you stepped into the light.");
System.out.println("You see a house");
System.out.println("It looks abandon. Do you want to investigate it?");
System.out.println("1.Yes.");
System.out.println("2.No.");
Scanner scan = new Scanner(System.in);//Declares new scanner
int a = scan.nextInt();//Helps me navigate around long if and while statements
switch(a){
case 1://outcome one
System.out.println("As you get closer to the abandon house, you "
+ "hear a random roar.");
break;
case 2://outcome two
System.out.println("The clouds black out the sun.\nYour "
+ "face is sprinkled with water"+". It's raining now.\nY"
+ "ou shiver.");
System.out.println("You hear a faint scream from the abandon "
+ "house. Do you want to investigate?");
break;
}
System.out.println("Go into the abandon house?");
System.out.println("1.I don't see why not.");
System.out.println("2.I rather stay outside,"+user_name+" snickers.");
Scanner sc = new Scanner(System.in);//New scanner
int b = sc.nextInt();//New int scanner
switch(b){
case 1://if play goes into the abandon house
for(MonsterHealth=100; MonsterHealth<0;){//monster's health loop
if(PlayerHealth>0){
System.out.println("The house is dark and quiet.");
System.out.println("A roar gets closer to you.");
System.out.println("You get knocked back!\nYou're hurt!");
PlayerHealth=PlayerHealth-6;
System.out.println(user_name+"'s health is now"+PlayerHealth);
System.out.println("You were attacked by a Green Monster!"+user_name
+" take this!\n"+user_name+" obtained a shiny sword!");
System.out.println("Press a to attack the Green Monster!");
if(input=='a'){
System.out.println("The Green Monster flinches.\nYou did 10 dam"
+ "age to the monster!");
MonsterHealth=MonsterHealth-15;
System.out.println("The Green Monster lunges at you!\nYou lost"
+ "5 health points!");
System.out.println("your health is now"+PlayerHealth+"!");
}else{
PlayerHealth=PlayerHealth-5;
System.out.println("The Green Monster lunges at you!\nYou lost"
+ "5 health points!");
input=(char)System.in.read();
}
}else{
}
if(MonsterHealth==0){
System.out.println("The Green Monster makes an ear piercing growl."
+ "The Green monster falls!\nYou win!");
}else if(PlayerHealth==0){//if the player looses all of their health
System.out.println("You see the light again"+user_name+"you have "
+ "fallen.\nYou're dead.\nGameover");
break;
}
}
case 2://second option for switch
for(GhostHealth=120; GhostHealth<0;){
System.out.println("You get chills.\n The sky gets ominously dark.");
System.out.println("You hear a screeching howl.\n You're pushed onto"
+ " the ground!\nYou're hurt");
PlayerHealth=PlayerHealth-5;
System.out.println(user_name+"'s health is now"+PlayerHealth);
System.out.println("You were attacked by a Ghost!");
System.out.println("Press a ,followed by enter ,to attack the Ghost!");
if(input=='a'){
System.out.println("The Ghost howls!.\nYou did 15 damage to the"
+ " Ghost!");
GhostHealth=GhostHealth-15;
System.out.println("The Ghost scratches you!\nYou lost"
+ "10 health points!");
PlayerHealth=PlayerHealth-10;
System.out.println("your health is now"+PlayerHealth+"!");
}else{
PlayerHealth=PlayerHealth-5;
System.out.println("The Ghost scratches you!\nYou lost"
+ "10 health points!");
input=(char)System.in.read();
}
}
if(GhostHealth==0){
System.out.println("The Ghost makes an ear piercing squeal."
+ "The Ghost dissolves!The skies are now clear"
+ "\nYou win!");
}else if(PlayerHealth==0){
System.out.println("You see the light again"+user_name+"you have "
+ "fallen.\nYou're dead.\nGameover");
}
}
}
}