簡體   English   中英

println 被打印兩次 - Java

[英]println being printed twice - Java

嗨,所以我在 java 為 uni 制作游戲,但我還是很新

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() {}}

當我運行代碼時它工作正常,直到我到達 IF 語句,我可以選擇輸入“1”或“2”並且結果顯示下一個 println 兩次? 我還是 java 的新手,所以我們將不勝感激。

使用 choice = 1 調用 oldBridge 並在 main() 中再次調用 oldBridge。 是那個

結果顯示下一個 println 兩次

你在說什么?

這是一個緩沖區錯誤。 choice = scanner.nextInt(); ,輸入一個空格被遺漏了,所以在上面的代碼之后簡單地輸入choice = scanner.nextLine(); . 它會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM