Results 1 to 3 of 3
- 02-09-2008, 04:23 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
- 02-09-2008, 01:56 PM #2
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
- 02-11-2008, 11:18 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 16
- Rep Power
- 0
Hi, I just created a simple Rock, Paper, Scissors program. Here's the code I used. Sorry the comments are limited and sorta crappy.
Java 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."); } }
Similar Threads
-
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM -
Help with actionPerformed Statements
By wco5002 in forum New To JavaReplies: 8Last Post: 03-26-2008, 04:02 AM -
urgent help needed - paper submission.
By dirtycash in forum New To JavaReplies: 2Last Post: 11-23-2007, 11:24 AM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM -
Problems with packages (import statements)
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 06-29-2007, 11:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks