Results 1 to 3 of 3
  1. #1
    andr0id is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Java newbie, a little help please :)

    So basically, this is a rock, paper, scissors program. The main problem i have is getting my array to print all in one JOptionPane line by line.
    The java tutor at my school gave me these tips-
    /*
    Generate a portion of a String
    You can then take that String portion and add it onto a final output String

    String output = "";
    for(){
    portion and add onto output by using the += operator
    }

    JOP.showMessagDialog(null, output);
    */

    but when i do this, i cant initialize "i" because its either missing or it has already been initialized. Also when i print, my it only shows the first element of my arrays. any idea?

    Java Code:
    import javax.swing.JOptionPane;
    import  java.util.Random;
    
    public class proj3
    {
    	public static void main (String[] args)
    	{
    		int[ ] compCArray = new int[100];
    		int[ ] yourCArray = new int[100];
    
    		int computerChoice;
    		int yourChoice;
    		int yourChoices =0;
    		int computerChoices =0;
    		int counterWin = 0;
    		int counterLoss = 0;
    		int counterTie = 0;
    		int totalGames =0;
    		String yourInput;
    		String playAgain = " ";
    		String output="Your Choices"+yourCArray[0] +"\n"+ "   Computer Choices Choices:"+compCArray[0];
    
    		JOptionPane.showMessageDialog(null," [Welcome to Rock Paper Scissor's v2.]");
    		JOptionPane.showMessageDialog(null,"[The rules are as follows]\n[Choose either]\n[Rock, Paper, or Scissors]\n[Paper Beats Rock, But loses to Scissors]\n[Rock beats Scissors, but loses to paper]\n[Scissors beats Paper, but loses to Rock]");
    		JOptionPane.showMessageDialog(null,"[Chose]\n[0 for Rock]\n[1 for Paper]\n[2 For Scissors]");
    
    	  do{
    		//for (int i=0; i<yourChoice.length; i++){
    			yourInput = JOptionPane.showInputDialog("What is your Choice?");
    				yourChoice= Integer.parseInt(yourInput);
    				yourCArray [totalGames] =yourChoice;
    
    		//	}
    			//for (int j=0; j<computerChoice.length; j++){
    			computerChoice= (int) (Math.random() * 3);
    			JOptionPane.showMessageDialog(null,"The Choices Were:\nComputer Choice"+computerChoice+ "\nYour Choice:"+yourChoice);
    				compCArray [totalGames] =computerChoice;
    			//}
    
    				if(yourChoice == computerChoice){
    					JOptionPane.showMessageDialog(null,"It is a tie, you both picked the same thing.  YOU LOSE!!");counterTie++;
    				}
    				else if(yourChoice == 0 & computerChoice == 2){
    					JOptionPane.showMessageDialog(null,"Congratulations, you chose Rock and the Computer Chose Scissors.  YOU LOSE!!");counterWin++;
    				}
    				else if(yourChoice == 1 & computerChoice == 0){
    					JOptionPane.showMessageDialog(null,"Congratulations, you chose Paper and the Computer Chose Rock.  YOU LOSE!!");counterWin++;
    				}
    				else if(yourChoice == 2 & computerChoice == 1){
    					JOptionPane.showMessageDialog(null,"Congratulations, you chose Scissors and the Computer Chose Paper.  YOU LOSE!!");counterWin++;
    				}
    				else if(yourChoice == 0 & computerChoice == 1){
    					JOptionPane.showMessageDialog(null,"Im sorry, you chose Rock, and the computer Chose Paper.  YOU LOSE!!");counterLoss++;
    				}
    				else if(yourChoice == 2 & computerChoice == 0){
    					JOptionPane.showMessageDialog(null,"Im sorry, you chose Scissors, and the computer Chose Rock.  YOU LOSE!!");counterLoss++;
    				}
    				else if(yourChoice == 1 & computerChoice == 2){
    					JOptionPane.showMessageDialog(null,"Im sorry, you chose Paper, and the computer Chose Scissors. YOU LOSE!!");counterLoss++;
    				}
    				totalGames++;
    
    	 	playAgain = JOptionPane.showInputDialog("Do you want to Play again? 'yes/no'");
            }while (playAgain.equals("yes"));
    
    		JOptionPane.showMessageDialog(null,"Total Games Played:  "+totalGames+"\n Total Wins:  "+counterWin+"\nTotal Losses:  "+counterLoss+"\nTotal Tie:  "+counterTie);
    
    		for(int i=0; i<totalGames; i++){
    		output+=output;
    
    		}
    		JOptionPane.showMessageDialog(null,output+=output);
    	}
    }
    Last edited by andr0id; 12-05-2011 at 11:56 PM.

  2. #2
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java newbie, a little help please :)

    Your forloop only runs while i is less than yourchoice.length, but you initialize yourchoice to 0, so it never enters the loop. Same with your second forloop and computerchoice.length.

  3. #3
    Vooda is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Java newbie, a little help please :)

    You actually don't need them for loops to achive what i think your aiming for. The problem is the way you are trying to build the output string.

    If the idea is to iterate over the arrays and create a string, then you should create a string for each array and use your last for loop to append each string using += then display both these strings in a JOptionPane.

    Java Code:
    String playerOut="You choises ";
    String compOut="Com choises";
    ...
    for(int i=0; i<totalGames; i++){
          playerOut+=yourCArray[i]+(" ");
          compOut+=compCArray[i]+(" ");    
    }
    JOptionPane.showMessageDialog(null,playerOut+"\n"+compOut);
    I think this is kinda what your aiming for

Similar Threads

  1. java newbie help
    By dasu908 in forum New To Java
    Replies: 20
    Last Post: 06-06-2011, 08:19 AM
  2. (Newbie) Help with Java and XML
    By sharktopus in forum AWT / Swing
    Replies: 0
    Last Post: 05-15-2011, 06:54 PM
  3. Newbie to Java.
    By avinash87 in forum New To Java
    Replies: 2
    Last Post: 04-30-2011, 09:36 AM
  4. Newbie iN java and need some help indeed!!
    By davexc in forum Java Applets
    Replies: 1
    Last Post: 07-13-2009, 06:34 PM
  5. 'm a newbie of java
    By ilysony in forum Introductions
    Replies: 7
    Last Post: 07-21-2008, 04:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •