7 Replies - 1319 Views - Last Post: 02 June 2009 - 06:13 PM Rate Topic: -----

#1 mikem678  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:18 PM

Ok so im making a tic tac toe game and now im trying to tell it that when the 3 buttons say either X or O to say Player one or player two wins now. i think i have to put this into an else if statement if im wrong then what do i put it in and then this is my code:

	private void btnSpotOneActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotOne.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotOne.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotOne.setText("O");
   txtMessage.setText("Player Ones Turn");
}



Thats just one of the buttons now i need to make the else if statement

else if(btnSpotOne.getText().equals("X")); {

}
	}



that only says if the one button equals X but it doesnt say what to do now the problem is i need to put lets say the first three buttons i would need to put if all three buttons equal X then display in the text box Player One Wins! so how would i put all three of the buttons in there properly?

Thanks for your help

Is This A Good Question/Topic? 0
  • +

Replies To: How to put more than 1 thing in else if statement

#2 xCraftyx  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 258
  • Joined: 13-September 08

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:24 PM

You would use Java's logic operators(&& (and) and || (or)) in between statements. For example:
else if(btnSpotOne.getText().equals("X") && btnSpotTwo.getText().equals("X")){}
would only execute if both of the buttons' text was set to X. The || (or) operator works the same way. You can put as many tests as you need inside of the if statement and they do not all have to be the same type

This post has been edited by xCraftyx: 02 June 2009 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 mikem678  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:30 PM

ok thanks that did help but i dont know where to put to say player one wins! do i put it between the two { } i did that and it didnt work. i click on the first box it puts the x in the box then the text box says player one wins!
Was This Post Helpful? 0
  • +
  • -

#4 xCraftyx  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 258
  • Joined: 13-September 08

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:35 PM

Sounds like a problem elsewhere in your code; what I changed for you ought to work based on your description of the problem. If you post the rest of your code i'll be able to help you more
Was This Post Helpful? 0
  • +
  • -

#5 mikem678  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:37 PM

ok heres the full code.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TicTacToe.java
 *
 * Created on 2-Jun-2009, 5:54:53 PM
 */

/**
 *
 * @author Home
 */
public class TicTacToe extends javax.swing.JFrame {

	/** Creates new form TicTacToe */
	public TicTacToe() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		btnSpotOne = new javax.swing.JButton();
		btnSpotTwo = new javax.swing.JButton();
		btnSpotThree = new javax.swing.JButton();
		btnSpotFour = new javax.swing.JButton();
		btnSpotFive = new javax.swing.JButton();
		btnSpotSix = new javax.swing.JButton();
		btnSpotSeven = new javax.swing.JButton();
		btnSpotEight = new javax.swing.JButton();
		btnSpotNine = new javax.swing.JButton();
		txtMessage = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);

		jLabel1.setText("Tic Tac Toe");
		getContentPane().add(jLabel1);
		jLabel1.setBounds(130, 20, 70, 14);

		btnSpotOne.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotOneActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotOne);
		btnSpotOne.setBounds(60, 90, 50, 40);

		btnSpotTwo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotTwoActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotTwo);
		btnSpotTwo.setBounds(130, 90, 50, 40);

		btnSpotThree.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotThreeActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotThree);
		btnSpotThree.setBounds(200, 90, 50, 40);

		btnSpotFour.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotFourActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotFour);
		btnSpotFour.setBounds(60, 150, 50, 40);

		btnSpotFive.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotFiveActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotFive);
		btnSpotFive.setBounds(130, 150, 50, 40);

		btnSpotSix.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotSixActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotSix);
		btnSpotSix.setBounds(200, 150, 50, 40);

		btnSpotSeven.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotSevenActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotSeven);
		btnSpotSeven.setBounds(60, 210, 50, 40);

		btnSpotEight.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotEightActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotEight);
		btnSpotEight.setBounds(130, 210, 50, 40);

		btnSpotNine.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnSpotNineActionPerformed(evt);
			}
		});
		getContentPane().add(btnSpotNine);
		btnSpotNine.setBounds(200, 210, 50, 40);

		txtMessage.setText("Player Ones Turn");
		txtMessage.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				txtMessageActionPerformed(evt);
			}
		});
		getContentPane().add(txtMessage);
		txtMessage.setBounds(70, 50, 160, 20);

		pack();
	}// </editor-fold>

	private void btnSpotOneActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotOne.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotOne.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotOne.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotTwoActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotTwo.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotTwo.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotTwo.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotThreeActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotThree.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotThree.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotThree.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotFourActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotFour.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotFour.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotFour.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotFiveActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotFive.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotFive.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotFive.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotSixActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotSix.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotSix.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotSix.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotSevenActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotSeven.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotSeven.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotSeven.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotEightActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotEight.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotEight.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotEight.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void btnSpotNineActionPerformed(java.awt.event.ActionEvent evt) {
btnSpotNine.setEnabled(false);
if (txtMessage.getText().equals("Player Ones Turn")) {
   btnSpotNine.setText("X");
   txtMessage.setText("Player Twos Turn");
}
else if(txtMessage.getText().equals("Player Twos Turn")) {
   btnSpotNine.setText("O");
   txtMessage.setText("Player Ones Turn");
}
	}

	private void txtMessageActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
}

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new TicTacToe().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton btnSpotEight;
	private javax.swing.JButton btnSpotFive;
	private javax.swing.JButton btnSpotFour;
	private javax.swing.JButton btnSpotNine;
	private javax.swing.JButton btnSpotOne;
	private javax.swing.JButton btnSpotSeven;
	private javax.swing.JButton btnSpotSix;
	private javax.swing.JButton btnSpotThree;
	private javax.swing.JButton btnSpotTwo;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JTextField txtMessage;
	// End of variables declaration

}


Was This Post Helpful? 0
  • +
  • -

#6 xCraftyx  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 258
  • Joined: 13-September 08

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 04:46 PM

Ah, what you ought to do is put a checkForWins() method of some sort at the end of your button click events that will check all of the buttons in sequence to see if player 1 or player 2 won. That way you could put the if statements you mentioned to check the buttons inside of the checkForWins() method :)

This post has been edited by xCraftyx: 02 June 2009 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 mikem678  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 05:15 PM

ok so i talked to my teacher and he told me to use this but he cant give me the full code and he cant help me alot more because its an exam -.- all i need to know now is how to make it output to the text box he gave me this:

if (btnSpotOne.getText().equals("X") && btnSpotTwo.getText().equals("X") && btnSpotThree.getText().equals("X")) {
	txtMessage.setText("Player One Wins!");
}



he says its slightly wrong but now i dont know where to put it to make it proper. please help...
Was This Post Helpful? 0
  • +
  • -

#8 xCraftyx  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 258
  • Joined: 13-September 08

Re: How to put more than 1 thing in else if statement

Posted 02 June 2009 - 06:13 PM

Put it inside the checkForWins() method that i recommended you write. The code itself will work if that's what he implied was wrong
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1