hi so im making a game for uni in java but im still pretty new
import java.util.Scanner;
public class Assignment{
public static void main ( String[] args ) {
Assignment game;
game = new Assignment();
game.darkJungle();
game.oldBridge();
game.abandonedShack();
}
public void darkJungle() {
Scanner scanner = new Scanner(System.in);
System.out.println("Hello adventurer. You awake from your slumber
in a dark jungle, with no memory of your past life.");
System.out.println("");
System.out.println("Before you set off exploring for answers, what
do you wish to call yourself?");
String playerName = scanner.next();
System.out.println("hello " + playerName);
System.out.println();
System.out.println("Inventory");
System.out.println("-------------");
String weapon;
weapon = "glock";
boolean rustyKey = true;
System.out.println(weapon);
System.out.println("rustyKey");
System.out.println();
System.out.println("You look to the east and west and notice
they're the safest looking paths. Which path do you set off on?");
System.out.println("1: west");
System.out.println("2: east");
int choice;
choice = scanner.nextInt();
if(choice==1){
System.out.println("You have wandered onto a creaky old
bridge.");
scanner.nextLine();
oldBridge();}
if(choice==2)
{System.out.println("You find an abandoned shack.");
scanner.nextLine();
abandonedShack();}
}
public void oldBridge() {
System.out.println("");
System.out.println("hi");
}
public void abandonedShack() {}}
when i run the code it works fine until i get to the IF statements where i have the choice to enter "1" or "2" and the result displays the next println twice? Im still new to java so any help would be appreciated.
With the choice = 1 you invoke oldBridge and in the main() you invoke oldBridge again. Is that the
result displays the next println twice
You are talking about?
It's a buffer error. After you take choice = scanner.nextInt();
, input a space gets left out, so after the above code simply put choice = scanner.nextLine();
. it will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.