Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2008, 06:23 AM
Member
 
Join Date: Feb 2008
Posts: 1
Alberto is on a distinguished road
Paper,Scissor,Rock If then Statements
Hello everyone im having trouble making the If then statementes for forcing an application of paper,scissor,rock to work
can anyone please be so kind and help me figure it out ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-09-2008, 03:56 PM
geork's Avatar
Member
 
Join Date: Jan 2008
Posts: 14
geork is on a distinguished road
i don't quite know what you mean but here is what i'm guesing you want:
import javax.swing.*;
import java.util.Scanner ;
import java.util.Random;
import java.awt.FlowLayout;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
class rps extends JFrame implements ItemListener, ActionListener {
public static void main(String args[]){
new rps();
}
JLabel l1 = new JLabel("choose rock, paper or scissors");
JLabel l2 = new JLabel();
JLabel l3 = new JLabel("vs");
JLabel l4 = new JLabel();
JLabel l5 = new JLabel();
JComboBox c = new JComboBox();
JButton b = new JButton("go!");
static void Sleep1000(){
try{
Thread.sleep(3000) ;
} catch (InterruptedException e){
System.out.println("program ended") ;
}
}
public rps(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
setLayout(new FlowLayout()) ;
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
l3.setVisible(false);
add(b);
b.addActionListener(this);
b.setEnabled(false);
add(c);
c.addItem("");
c.addItem("rock");
c.addItem("papper");
c.addItem("scissors");
c.addItemListener(this);
pack();
setVisible(true);
}
public void itemStateChanged(ItemEvent e){
b.setEnabled(true);
c.setEnabled(false);
}
public void actionPerformed(ActionEvent e){
int op = new Random().nextInt(2)+1;
int po = op;
b.setEnabled(false);
if(c.getSelectedItem().equals("rock")){
l1.setVisible(false);
l3.setVisible(true);
l2.setText("rock");
if(po == 1){
Sleep1000();
l4.setText("paper");
Sleep1000();
l5.setText("you lose");
}else{
Sleep1000();
l4.setText("scissors");
Sleep1000();
l5.setText("you win!");
}
}
if(c.getSelectedItem().equals("paper")){
l1.setVisible(false);
l3.setVisible(true);
l2.setText("paper");
if(po == 1){
Sleep1000();
l4.setText("scissors");
Sleep1000();
l5.setText("you lose");
}else{
Sleep1000();
l4.setText("rock");
Sleep1000();
l5.setText("you win!");
}
}
if(c.getSelectedItem().equals("scissors")){
l1.setVisible(false);
l3.setVisible(true);
l2.setText("scissors");
if(po == 1){
Sleep1000();
l4.setText("rock");
Sleep1000();
l5.setText("you lose");
}else{
Sleep1000();
l4.setText("paper");
Sleep1000();
l5.setText("you win!");
}
}
}
}
hope that helps
__________________
he who laughs last probabely just got the joke
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-12-2008, 01:18 AM
Member
 
Join Date: Feb 2008
Posts: 16
Deathmonger is on a distinguished road
Hi, I just created a simple Rock, Paper, Scissors program. Here's the code I used. Sorry the comments are limited and sorta crappy.

Code:
import java.io.*; import java.util.*; class RPS { String UserRPS; // The Users choice String CompRPS; // The Computers choice int countWins; int countLoses; public RPS() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("This program is a game of Rock, Paper, Scissors with the computer."); System.out.println("Type exit to end the game."); while(true) { System.out.println("The game is currently " + countWins + " Wins and " + countLoses + " loses."); System.out.println("Type in R for Rock, P for Paper, and S for Scissors."); UserRPS = in.readLine(); UserRPS = UserRPS.toUpperCase(); CompRPS = ComputerRPS(); if(UserRPS.equals("R")) { // The logic used for if the user wins or loses, UserRock(); // depends on what the user entered. Use these } // if statements to choose the correct logic for if(UserRPS.equals("P")) { // the UserRPS. UserPaper(); } if(UserRPS.equals("S")) { UserScissors(); } if(UserRPS.equals("EXIT")) { break; } } } public String ComputerRPS() { // This method determines the int CompChoice; // computer choice for Rock, Paper String CompRPS = ""; // or scissors. This is determined // randomly. CompChoice = (int)((Math.random() * 2) + 1); if(CompChoice == 1) { CompRPS = "R"; } if(CompChoice == 2) { CompRPS = "P"; } if(CompChoice == 3) { CompRPS = "S"; } return CompRPS; } public void UserRock() { // This method is used for if(CompRPS.equals("R")) { // when the user chooses Rock. System.out.println("The computer chooses Rock. The game " + "is a draw."); } if(CompRPS.equals("P")) { System.out.println("The computer chooses Paper. You lose X(."); ++countLoses; } if(CompRPS.equals("S")) { System.out.println("The computer chooses Scissors. You win ;)."); ++countWins; } } public void UserPaper() { // This method is used for if(CompRPS.equals("R")) { // when the user chooses Paper. System.out.println("The computer chooses Rock. You win ;)."); ++countWins; } if(CompRPS.equals("P")) { System.out.println("The computer chooses Paper. The game is a draw."); } if(CompRPS.equals("S")) { System.out.println("The computer chooses Scissors. You lose X(."); ++countLoses; } } public void UserScissors() { // This method is used for if(CompRPS.equals("R")) { // when the user chooses Scissors System.out.println("The computer chooses Rock. You lose X(."); ++countLoses; } if(CompRPS.equals("P")) { System.out.println("The computer chooses Paper. You win ;)."); ++countWins; } if(CompRPS.equals("S")) { System.out.println("The computer chooses Scissors. The game is a draw."); } } } public class TestRPS { public static void main(String[] args) throws IOException { RPS rps = new RPS(); System.out.println("Thanks for playing."); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
avoiding if statements valoyivd New To Java 1 04-02-2008 11:08 AM
Help with actionPerformed Statements wco5002 New To Java 8 03-26-2008 06:02 AM
urgent help needed - paper submission. dirtycash New To Java 2 11-23-2007 01:24 PM
Help with if else statements zoe New To Java 1 07-24-2007 09:56 PM
Problems with packages (import statements) ai_2007 Advanced Java 1 06-29-2007 01:57 PM


All times are GMT +3. The time now is 04:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org