Java 调用函数和返回正确字符时出现问题
基本上,我创建的是一个简单的游戏,用户定义网格大小和点击框来收集积分,有三个选项:奖品=200分,强盗=-100分和炸弹=-10000分(这将结束游戏) 我遇到的问题是,当用户选择一个盒子时,炸弹就会出现,它只会减去100点,就像用户选择bandit一样 BlockHop:Java 调用函数和返回正确字符时出现问题,java,class,methods,client,Java,Class,Methods,Client,基本上,我创建的是一个简单的游戏,用户定义网格大小和点击框来收集积分,有三个选项:奖品=200分,强盗=-100分和炸弹=-10000分(这将结束游戏) 我遇到的问题是,当用户选择一个盒子时,炸弹就会出现,它只会减去100点,就像用户选择bandit一样 BlockHop: import java.util.*; public class BlockHop{ Random rand = new Random(); private GameItems[][] board; publi
import java.util.*;
public class BlockHop{
Random rand = new Random();
private GameItems[][] board;
public static int score = 0;
public BlockHop(){
board = new GameItems[1][1];
board[0][0] = new Prize(0, 0, 'P');
}
public BlockHop(int gridSize) {
board = new GameItems[gridSize][gridSize];
int end = 10;
int start = 1;
for (int row = 0; row < board.length; row++){
for (int col = 0; col < board[row].length; col++){
int num = rand.nextInt(end - start + 1) + start;
switch (num){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
board[row][col] = new Prize(row, col, 'P');
break;
case 7:
board[row][col] = new Bomb(row, col, 'X');
break;
case 8:
case 9:
case 10:
board[row][col] = new Bandit(row, col, 'B');
break;
}
}
}
}
public void play(int row, int col){
String newID = getLabel(row, col);
board[row][col].adjustScore(newID.charAt(0));
}
public int getScore(){
return score;
}
public String getLabel(int row, int col){
if ((board[row][col]).equals('P')){
return "PRIZE!";
}
else if ((board[row][col]).equals('X')){
return "Bomb!";
}
else{
return "Bandit!";
}
}
public boolean isGameOver(){
return getScore() < 0;
}
}
BlockHopGUI:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BlockHopGUI extends JFrame{
private BlockHop bh;
private JButton [][] board;
private JLabel scorePoints;
private PlayHandler ph; // listener for buttons
public BlockHopGUI( int gridSize ) {
super( "Click to uncover prizes" );
bh = new BlockHop( gridSize );
System.out.println( "gridsize: " + gridSize );
Container c = getContentPane( );
JPanel p = new JPanel( );
board = new JButton[gridSize][gridSize];
p.setLayout( new GridLayout( gridSize, gridSize ) );
ph = new PlayHandler( );
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board.length; col++ ) {
board[row][col] = new JButton( "" );
board[row][col].addActionListener(ph);
p.add( board[row][col] );
}
c.add( p, BorderLayout.CENTER );
JPanel scorePanel = new JPanel( );
JLabel scoreLabel = new JLabel( "Score: " );
scorePoints = new JLabel( Integer.toString( bh.getScore( ) ) );
scorePanel.add( scoreLabel );
scorePanel.add( scorePoints );
c.add( scorePanel, BorderLayout.SOUTH );
setSize( 500, 500 );
setVisible( true );
}
private class PlayHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[0].length; col++ ) {
if ( ae.getSource( ) == board[row][col] ) {
bh.play( row, col );
board[row][col].setText( bh.getLabel( row, col ) );
board[row][col].removeActionListener( ph );
break;
}
}
scorePoints.setText( Integer.toString( bh.getScore( ) ) );
if ( bh.isGameOver( ) ) {
JOptionPane.showMessageDialog( null, "Game over! Final points: "
+ bh.getScore( ) );
System.exit( 1 );
}
}
}
public static void main( String [] args ) {
int gridSize = Integer.parseInt(
JOptionPane.showInputDialog( null, "Enter the grid size" ) );
BlockHopGUI bhg = new BlockHopGUI( gridSize );
bhg.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
import java.util.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类BlockHopGUI扩展JFrame{
私人BlockHop bh;
私有JButton[]]板;
私人JLabel分数;
专用PlayHandler ph;//按钮的侦听器
公共BlockHopGUI(int gridSize){
超级(“点击打开奖品”);
bh=新的区块链(gridSize);
System.out.println(“gridsize:+gridsize”);
容器c=getContentPane();
JPanel p=newjpanel();
board=新的JButton[gridSize][gridSize];
p、 setLayout(新的GridLayout(gridSize,gridSize));
ph=新的playdhandler();
对于(int row=0;row
你的adjustScore()函数接受一个字符,并期望它是标记对象在地图中的字符,即X代表bomb,B代表bandit,等等。但是如果你看一下你在游戏中调用它的位置(),那不是你实际发送的内容,你将从正方形的标签中获取第一个字符。通常这会完全失败,但因为Bandit和Bomb都是以B开头的,而B是您期望的角色之一,所以它将两者都视为Bandit。您需要修复它,以便调用一个返回所需字符的函数,或者更改adjustScore函数以接受整个字符串标签
调试的一些一般技巧是查看与问题相关的函数,并非常仔细地检查它们是否正在执行预期的操作,尝试一步一步地检查程序将执行的操作,并在变量更改时写下它们
希望这有帮助 您是否调试并找到了无效部分?然后把它贴在这里,你不会得到任何常规的代码检查。坚持住这会花一点时间。当选择一个炸弹并输入
adjustScore
-方法时,任何调试都会说什么?这很有帮助,但我认为我已经解决了这个问题?看起来不像,问题是这两行:String newID=getLabel(row,col);板[row][col].adjustScore(newID.charAt(0));啊,我现在看到了,但是我如何把getLabel放在第二行呢?实际上,您正在对网格正方形内的实际游戏项调用adjustScore()。因此,您根本不需要传入char,将adjustScore()函数更改为不接受任何参数,在打开输入的位置,改为打开该对象ID。只需ID即可获得该对象ID,或者使用this.ID更显式。另一个选项是使adjustScore()接受字符串而不是char,如adjustScore(字符串输入),并让您的切换案例检查完整标签,如案例“Bandit”。然后只需传入newID而不做任何更改。如果这是您想要的,那么adjustScore应该是一个静态方法,您可以使用GameItems调用它。adjustScore(newID);
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BlockHopGUI extends JFrame{
private BlockHop bh;
private JButton [][] board;
private JLabel scorePoints;
private PlayHandler ph; // listener for buttons
public BlockHopGUI( int gridSize ) {
super( "Click to uncover prizes" );
bh = new BlockHop( gridSize );
System.out.println( "gridsize: " + gridSize );
Container c = getContentPane( );
JPanel p = new JPanel( );
board = new JButton[gridSize][gridSize];
p.setLayout( new GridLayout( gridSize, gridSize ) );
ph = new PlayHandler( );
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board.length; col++ ) {
board[row][col] = new JButton( "" );
board[row][col].addActionListener(ph);
p.add( board[row][col] );
}
c.add( p, BorderLayout.CENTER );
JPanel scorePanel = new JPanel( );
JLabel scoreLabel = new JLabel( "Score: " );
scorePoints = new JLabel( Integer.toString( bh.getScore( ) ) );
scorePanel.add( scoreLabel );
scorePanel.add( scorePoints );
c.add( scorePanel, BorderLayout.SOUTH );
setSize( 500, 500 );
setVisible( true );
}
private class PlayHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[0].length; col++ ) {
if ( ae.getSource( ) == board[row][col] ) {
bh.play( row, col );
board[row][col].setText( bh.getLabel( row, col ) );
board[row][col].removeActionListener( ph );
break;
}
}
scorePoints.setText( Integer.toString( bh.getScore( ) ) );
if ( bh.isGameOver( ) ) {
JOptionPane.showMessageDialog( null, "Game over! Final points: "
+ bh.getScore( ) );
System.exit( 1 );
}
}
}
public static void main( String [] args ) {
int gridSize = Integer.parseInt(
JOptionPane.showInputDialog( null, "Enter the grid size" ) );
BlockHopGUI bhg = new BlockHopGUI( gridSize );
bhg.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}