Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Look, next time you start writing something, do it in pseudo code. That is, don't write java, but just plain language and detail the steps you need to take. In your case, it looks like this:
    Java Code:
    prompt user to input word
    if word is stop
        exit the program
    if word contains secret letter
        print out Success
    else
        print out Fail
    repeat
    Once you have the steps laid out, it's much easier to write up your program.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  2. #22
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    the problem is am a begginer i cant implement this thing into the program
    sorry for that guyz

  3. #23
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    No, the thing is you don't want to try. The steps I laid out in the above post make it painfully obvious what needs to be done, and the code you posted a while back has almost all the necessary elements in it. Look, here take it:
    Java Code:
    [COLOR="White"]import java.util.Scanner;
    public class Guess {
    	public static void main(String[] args) {
    		char guess = 'r';
    		String input;
    		Scanner sc = new Scanner(System.in);
    		while(true) {
    			System.out.println("Input word");
    			input = sc.next().toLowerCase();
    			if(input.equalsIgnoreCase("stop"))
    				break;
    			if(input.indexOf(guess) != -1)
    				System.out.println("Your guess contains secret");
    			else
    				System.out.println("Your guess doesn't contain secret");
    		} 
    	}
    }[/COLOR]
    I put the code in white color in the slim chance you'll try and write the few lines of code required by yourself. Copy/paste the code above, and you're only harming yourself in the long run.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #24
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    thank you very much .. i've tried it this way by the way " just because i want to tell you that i've tried"

    Java Code:
    public static void main(String[] args)  
            {  
                    Scanner sc;  
                    char hidden = 'R';  
                    String guess;
    	String ab="stop";
                    sc = new Scanner(System.in);  
                    do  
                    {  
                            System.out.println("enter the word");  
                            guess = sc.nextLine().toUpperCase();     
    	if (guess==ab)
    	{
    		System.exit(0)
    	}                       
                    }  
                    while (guess.indexOf(hidden) == -1);  
                      
                    System.out.println("Well done");                  
            }   
    }
    Last edited by zohdi; 05-30-2010 at 01:33 AM.

  5. #25
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Java Code:
    if (guess==ab)
    Don't use == to compare Strings. (This was probably covered in your class, so check your notes and your text -- there is a method in the String class that enables you to check if a String has the same text as another String.) And when you're posting to the forum, please put your code in CODE tags, and explain your problem clearly. "Here is my code, and the program doesn't stop when I enter 'stop'" is a much better question to post than "I don't know how to do it."

    There is no need to apologize for being a beginner -- we were all beginners once, and in many, many areas, we still are. But try to clearly identify exactly what help you need, and ask a clear question. Presumably you are taking this course because you want to become a Java programmer. As a Java programmer, there will always be things you don't know, and you will need to research to find the answer, or ask for help. Knowing how to ask for help is an important skill. Asking other people to do your work for you is rarely a good technique (unless you are paying them to work for you).

    -Gary-

  6. #26
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    thank you garry

  7. #27
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    could i use this command to exit

    " System.exit(0) "
    ?

  8. #28
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by zohdi View Post
    could i use this command to exit

    " System.exit(0) "
    ?
    Yes, you can, but of course your "Well done" message won't be printed if you do that. You can also use "break" to exit the loop you're in.

    -Gary-

    EDIT: Actually, that's probably what you want, so System.exit(0) is fine.

  9. #29
    zohdi is offline Member
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. A new thing arrives...
    By ewomack in forum Introductions
    Replies: 3
    Last Post: 10-09-2009, 09:16 PM
  2. [SOLVED] Simple Trig Thing
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 05-16-2009, 06:45 AM
  3. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  4. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 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
  •