2 Replies - 8721 Views - Last Post: 26 September 2010 - 07:13 PM Rate Topic: -----

#1 mzkitten38  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-September 10

rock paper scissors using switch and do while statements

Posted 24 September 2010 - 05:45 PM

Hi
I'm new to Java, and my teacher keeps changing up the directions for creating the game rock, paper scissors.
she wants us to include a do while loop today. she wants us to use the switch statement as well, but I am not sure if I have the do while loop in the correct place, I keep getting errors. I would be so grate ful if someone would help me figure out what I am doing wrong.

 import javax.swing.JOptionPane;
public class Option{
	public static void main(String [] args){

int userChoice;
int compChoice;
int win,loses,draws;
int games;

do{

computer =(int)Math.random ()*3)+1;

  user = JOptionPane.showInputDialog(null, "enter a number 0 to 2");
  
  }
  

switch (option){
case 1: JoptionPane.showInputDialog(null, "Play paper,rock, scissors,against the computer");
	
if ( rock == 0 && rock == 0){
lose--;
JOptionPane.showMessageDialog(null, "The game is a draw");
}
else if (rock ==0 && scisssors ==2){
lose--;
JOptionPane.showMessageDialog(null," Rock win + lose");
}
if (paper == 1 && rock == 0){
win++;
JOptionPane.showMessageDialog(null,"Paper wint + win");
}
else if (scissors ==2 && paper ==1){
win++;
JOptionPane.showMessageDialog(null,"scissors win + win");
}
if (rock == 0 && paper ==1){
lose--;
JOptionPane.showMessageDialog(null ,"paper wins + lose");
}
else if (scissors==2 && rock ==0){
JOptionPane.showMessageDialog(null, "Rock win + lose");
lose--;
{
if (paper ==1 && scissors ==2){
win++;
JOptionPane.showMessageDialog(null, "scissors win +win");
}
else if (paper ==1 && paper ==1){
JOptionPane.showMessageDialog(null,"The game is a draw");
}               
                      break;
 case2: JOptionPane.showMessageDialog(null,"Print out the statistics versus the computer");
	                    break;
 case3: JOptionPane.showMessageDialog(null,"Quit the program");
	                    break;
 default:   JOptionPane.showMessageDialog (null,"Errors: invalid status");
				System.exit (0);
}
				while {(choice>1 && choice<3)
				
}


Is This A Good Question/Topic? 0
  • +

Replies To: rock paper scissors using switch and do while statements

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: rock paper scissors using switch and do while statements

Posted 24 September 2010 - 07:59 PM

Well, there's a reason that it's called a do-while loop. You have simply done the do part. How do expect this code to stop looping?

        do {
            computer = (int) Math.random() * 3
                    )+1;
            user = JOptionPane.showInputDialog(null, "enter a number 0 to 2");
        }



do-while loops are "supposed" to look like this:
do {
   //...stuff
} while (condition_is_true); // Notice the semicolon!



Right...In the switch section you need to do two things. Make sure ALL your variables are defined. Few of them currently are. Another thing that you need to do is get your braces ( { and } ) working properly. Right now, your code is all over the place because these are not properly placed. I suggest using an editor that handles indention and highlights braces for you. I suggest Notepad++
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: rock paper scissors using switch and do while statements

Posted 26 September 2010 - 07:13 PM

That does not really make sense

if ( rock == 0 && rock == 0){

if I am male AND I am male ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1