繁体   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