Thank you! That actually means a lot. I'll certainly keep your comments in mind. I didn't even think of holding off on declaring variables until it came time to use them. When I first chalked it up in pseudocode, I wrote through the entire function to itemize the variables and types I would need and carelessly declared them at the beginning of the code.
Yeah, I noticed the inconsistency yesterday upon completion and went back to fix it. I come from a background in HTML, CSS, and some Javascript and a little experience in PHP, so I'm not sure why I didn't do it from the beginning. Haste, I suppose.
I plan on returning to this particular project in the future to compare how I do with a bit more familiarity with Java.
- Viewing Profile: Posts: Adderly
Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics
Community Stats
- Group Members
- Active Posts 25
- Profile Views 736
- Member Title Member
- Age 21 years old
- Birthday April 1, 1992
-
Gender
Male
-
Location
Texas
Posts I've Made
In Topic: If/Else Statements in Swtich Statements?
20 May 2011 - 08:00 AM
In Topic: If/Else Statements in Swtich Statements?
20 May 2011 - 05:57 AM
Excellent, rip-off! I can understand most of your code, but there are a still few parts that look new to me. The book that I'm reading now is just getting into exceptions and 'try..catch'. Haven't seen anything on 'throw new' yet, but I'm sure I will. Also, thank you for informing me of the difference between the "==" operator and 'equals()' method. I'll certainly consider that for future application. Like I said earlier though, this was the first program I'd written from scratch to suit my desires and I thought it turned out wonderfully, considering I've only been really reading heavily for about a week now. I'm still proud of it.
I've finally stepped over that boundary of writing a functional (however arbitrary it may be) and interactive program. For the first time since I started reading, I feel like I might actually have a shot at this once I have some experience behind me.
Thank you both for you contributions! Everyone here has helped immensely and I greatly appreciate that. I'm glad I've joined the community.
Thank you both for you contributions! Everyone here has helped immensely and I greatly appreciate that. I'm glad I've joined the community.
In Topic: If/Else Statements in Swtich Statements?
19 May 2011 - 08:13 PM
challenge accepted :-)
public static void main(String[] args) { String[] choices = new String[]{"Rock", "Paper", "Scissors"}; System.out.println("What's your move?"); for (int i = 0; i < 3; ++i) System.out.println("\t" + (1 + i) + ". " + choices[i]); Scanner in = new Scanner(System.in); int human = in.nextInt() - 1; //Get myMove int pc = (int) (3. * Math.random());//Get computerMove System.out.println("Computer chooses: " + choices[pc]); System.out.println("You choosed: " + choices[human]); int i = human - pc; if (pc == human) System.out.println("Tie!"); else if (i == 1 || i == -2) System.out.println("You win!"); else System.out.println("Computer wins!"); }
for such a little programm a while loop will do, break out if the player don'T want to play another game.
lol! Show off
As a beginner, pieces of your solution looks foreign to me. Would you care to elaborate more with comments to specify what you're doing and when? If not, you're not obligated to. I'm just in Learning Mode, today because I'm happy that I achieved the results that I wanted to. This is the first interactive program I've started from scratch. And no matter how newbish my solution may be, I'm proud of it!
edit: By the way, you forgot to include 'import java.util.Scanner;'
In Topic: If/Else Statements in Swtich Statements?
19 May 2011 - 06:48 PM
So here's the final product (with a few changes from my initial post):
Is there anything anyone would've done differently? Also, rather than breaking from the case statement and ending the program, how can I go back to the beginning of the main class to start the game again?
edit: Nevermind. I'm pretty sure I can implement a boolean expression to determine whether or not the user would like to play again. (Although I'm not sure how to return to the beginning of the main class). Still, though. What would you have done differently?
public class RockPaperScissors {
public static void main(String[] args) {
String win = "You win!";
String lose = "Sorry. You lose.";
String computerMove;
String selection;
int myMove;
int rand;
//Get myMove
TextIO.putln("What's your move?");
TextIO.putln();
TextIO.putln(" 1. Rock");
TextIO.putln(" 2. Paper");
TextIO.putln(" 3. Scissors");
myMove = TextIO.getlnInt();
//Get computerMove
rand = (int)(3*Math.random());
if ( rand == 0)
computerMove = "Rock";
else if (rand == 1)
computerMove = "Paper";
else
computerMove = "Scissors";
//Compare moves
switch (myMove) {
case 1:
selection = "Rock";
if (computerMove == "Paper")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection);}
else if (computerMove == "Rock")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
case 2:
selection = "Paper";
if (computerMove == "Scissors")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection );}
else if (computerMove == "Paper")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
case 3:
selection = "Scissors";
if (computerMove == "Rock")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection);}
else if (computerMove == "Scissors")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
default:
TextIO.putln("You can't choose that! I quit!");
System.exit(1);
}
}
}
Is there anything anyone would've done differently? Also, rather than breaking from the case statement and ending the program, how can I go back to the beginning of the main class to start the game again?
edit: Nevermind. I'm pretty sure I can implement a boolean expression to determine whether or not the user would like to play again. (Although I'm not sure how to return to the beginning of the main class). Still, though. What would you have done differently?
In Topic: If/Else Statements in Swtich Statements?
19 May 2011 - 06:34 PM
Your if and your else inside switch (the ones it's complaining about) don't have any braces ({ }) on them!
If an if/else has more than one statement inside it, those statements need to be contained within braces.
This is what Java is seeing:if (computerMove == "Paper") TextIO.putln(lose); //the 'if' just ended here TextIO.putln(computerMove);//this is outside of the if TextIO.putln(" beats "); TextIO.putln(selection); else //wtf, why is there an 'else' here that isn't connected to an 'if'?? ZOMG I'll report an error telling the user to delete it. TextIO.putln(win); TextIO.putln(selection); TextIO.putln(" beats "); TextIO.putln(computerMove);
Thank you for explaining in detail! Detail is always helpful
- Home
- » Viewing Profile: Posts: Adderly

Find content