Results 1 to 3 of 3

Thread: how can i cut this code size down

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    how can i cut this code size down

    Code:
    		if (result == "Congratulations! Your guess was correct.") {
    		    String end = "Game over. You guessed correctly.";
    		    
    		    out.writeUTF(end);
    		    out.flush();
    		    
    		    client.close();
    		    server.close();
    		    break;
    		}
    		
    		else if (mnumb.getFinished()) {
    		    String end = "Game over. You exceeded maximum number of guesses.";
    		    
    		    out.writeUTF(end);
    		    out.flush();
    		    
    		    client.close();
    		    server.close();
    		    break;
    		}
    		else {
    		    out.writeUTF(result);
    		    out.flush();
    		}
    the 2 first if statements perfrom the same action, except the string message is different.. im sure i can cut duplicating code out?

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: how can i cut this code size down

    you can use the or opperator || to conect the two if statements

    Code:
    if (result == "Congratulations! Your guess was correct." || mnumb.getFinished())

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: how can i cut this code size down

    Quote Originally Posted by TBeck
    you can use the or opperator || to conect the two if statements

    Code:
    if (result == "Congratulations! Your guess was correct." || mnumb.getFinished())
    That's not right, the end String is different in each statement.

    and you cannot compare strings like that, it'll never return true, you should use
    Code:
    if(result.equals("Congratulations! Your guess was correct."))
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width