public class GameLogic {
Scanner input = new Scanner(System.in);
public void buyHouse(Player player, int propertyIndex) {
boolean boughtHouse = false;
GameboardSquare property = Board.getMonopolyBoard()[propertyIndex];
int a = 0;
if (property.getNumberHouse() <= 3) {
System.out.println("How many houses you want to buy? it must be between 1-4: ");
a = input.nextInt();
System.out.println(a);
}
}
}
有人可以告訴我我做錯了什麼。每次運行代碼時,它都不會讓我輸入任何內容到控制檯中,它只會在線路a = input.nextInt();
上變得糟糕。我試圖調試它在調試器,但仍然不知道在哪裏崩斷,這是我的測試案例也Java掃描儀沒有從用戶輸入
public class GameLogicTest extends TestCase {
public void testBuyHouse() throws Exception {
Player player = new Player(1000,0);
GameboardSquare property = new GameboardSquare("Seb", 100,50,50,100,150,200,250,300,200,0,3,0,player,false);
GameboardSquare[] gameboard = new GameboardSquare[1];
gameboard[0] = property;
GameLogic game = new GameLogic();
Board board = new Board(gameboard);
board.setMonopolyBoard(gameboard);
game.buyHouse(player,0);
}
}
https://i.stack.imgur.com/Dovlc.png
也打印輸出
你在哪裏運行你的代碼?在IDE或控制檯?當你在'a = input.nextInt();'行中說'被卡住'時,你究竟是什麼意思? – Nurjan
看起來它並沒有卡住,而是等待着你在這個地方的輸入,你正在運行你的代碼。輸入你輸入的內容,然後按回車。它應該工作! –
考慮使用System.console()從控制檯讀取輸入。 而且,根據您的啓動方式,控制檯可能無法使用或System.in無法使用。 –