Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Almost Giving up :((

  1. #1

    Question Almost Giving up :((

    I am about to give up of this one but I want to try my luck here...

    I want to create an applet involving
    (1)successfully increase by 1 25%
    (2)Failed to increase 25%
    (3)Decrease by 1 25%
    (4)Returning to zero 25%

    OPTIONAL
    (5)Managing % of each event


    *******OUTPUT SHOULD BE LOOK LIKE THIS*********

    Level 0

    1-INCREASE 2-EXIT
    Input: 1
    ****************
    Level 1
    SUCCESSFULL!!!
    1-INCREASE 2-EXIT
    Input: 1
    *****************
    Level 2
    SUCCESSFULL!!!
    1-INCREASE 2-EXIT
    Input: 1
    *****************
    Level 1
    FAILED/DECREASE BY 1
    1-INCREASE 2-EXIT
    Input: 1
    *****************

    Level 2
    SUCCESSFULL!!!
    1-INCREASE 2-EXIT
    Input: 1
    *****************
    Level 2
    FAILED
    1-INCREASE 2-EXIT
    Input: 1
    *****************
    [GAME OVER]
    FAILED
    1-TRY AGAIN 2-EXIT
    Input: 2
    *****************
    END OF PROGRAM


    I am not expecting someone will do the code for me, i am just seeking for some additional knowledge coz I don't really finished my study i am just eager to learn more
    Thanks in regards


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    If you have code you are having problems with, please post it with your questions.

    I have not idea what you want the code to do or what the contents of your post means or how it is related to java programming.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3

    Default Re: Almost Giving up :((

    Sorry i know my post is really confusing and maybe annoying, the thruth I am really trying my best to build this code but I can't logically build my own way of doing it so. Maybe I'll check some of my codes and post which I think may give you idea of what I want to do..Thanks for the rep

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Post the console contents for when you execute the program that shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Almost Giving up :((

    Try to better explain what exactly you are trying to achieve. I'm having an awful time trying to figure out what you are trying to do.

  6. #6

    Default Re: Almost Giving up :((

    Hello, here's my code so far again...I am quite figuring it out but i am stuck in a line, hope you guys can figure it out whats wrong ;p . Thank you in advance
    This one runs, I think you can figure it out what this for...
    I have two codes here
    (1)Running, to see what i want to do with this code
    (2)Upgrading my code to next level i am stuck in a line @_@

    RUNNING:It's proceeding to level 7 but i don't think it's the correct way...
    import java.util.*;
    import java.io.*;
     
    public class Proj6
    {
    	public static DataInputStream input=new DataInputStream(System.in);
    	public static void main(String args[])throws Exception
    	{
    		int input2;
    		System.out.println("Level 6");
    		do
    		{
    		System.out.println("**********************\n(1)-INCREASE  (2)-EXIT");
    		System.out.print("Input: ");
    		input2=Integer.parseInt(input.readLine());
     
    		int level=6;
    		Random r = new Random();
    		int rand = r.nextInt(100);
     
    		if(rand<51)
    		level=level+1;
    		else if (rand>50)
    		{
    			Random r2 = new Random();
    			int rand2 = r.nextInt(100);
    			if(rand2<51)
    				System.out.println("FAILED");
    				else
    					System.out.println("GAME OVER");
    		}
    		if(level==7)
    		{
    			System.out.println("Level 7");
    		}
    		}
    		while (input2==1);
    		System.out.println("****END OF PROGRAM****");
     
    	}
    }

    STUCK:I think this will work but i am stuck in a line...Forgot how to get value from a statement...
    import java.util.*;
    import java.io.*;
     
    public class Project7
    {
    	public static DataInputStream input=new DataInputStream(System.in);
    	public static void main(String args[])throws Exception
    	{
    		//int value; if i initialize value here i think its fix and cannot be change.
    		int input2;
    		System.out.println("Level 6");
    		do
    		{
    		System.out.println("**********************\n(1)-INCREASE  (2)-EXIT");
    		System.out.print("Input: ");
    		input2=Integer.parseInt(input.readLine());
     
    		Random r = new Random();
    		int rand = r.nextInt(100);
     
    		if(rand<51)
    		int level=level+1; //here it changes but its value cannot be detected outsite the statement...
    		else if (rand>50)
    		{
    			Random r2 = new Random();
    			int rand2 = r.nextInt(100);
    			if(rand2<51)
    				System.out.println("FAILED");
    				else
    					System.out.println("GAME OVER");
    		}
    		}//This while is to keep doing this statement as long Level is not increased...
    		while ((input2==1)||(level==6));//****I have problem in this line, how to proceed to level 7 if value is increased?
    		if(level==7)
    		{
    			System.out.println("Level 7"); //I made it short but i want it simultanious until Level 12 and so on...
    		}
    		System.out.println("****END OF PROGRAM****");
    	}
    }

    Sorry, the truth is my programming skill is very limited now i am using more on logic to make this work...
    Thank you guys...
    Last edited by amakuzaki; June 11th, 2012 at 02:48 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    i am stuck in a line @_@
    I can not find a line that is marked with @_@.

    Can you explain what you are stuck on?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8

    Default Re: Almost Giving up :((

    On the SECOND code...
    On my while condition...value of level thing, cant figure out how i can get value of level in do {statement}...

    I forgot how to get value inside the do{statement}
    Last edited by amakuzaki; June 11th, 2012 at 03:02 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    You need to define the level variable at the same scope (within the same pair of {}s) for all the places that you want to access it.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10

    Default Re: Almost Giving up :((

    Sorry to ask this but do you have idea how I can manage while((input2)||(level==6)) work in this code...
    Coz i want to happen is

    do{statement} level6 I cant put level outside the do{}
    while((input2)||(level==6))

    do{statement} level7
    while((input2)||(level==7))

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    Sorry, I do not understand what you are asking.

    Please copy and paste here the full text of the error messages you are getting.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12

    Default Re: Almost Giving up :((

    Still in grip of this one
    It's posible to implement IF/IF ELSE statement within DO statement right?
    But i cant figure out how...

    import java.io.*;
    import java.util.*;
    public class Project12
    {
    	public static DataInputStream input=new DataInputStream(System.in);
    	public static void main(String[]args)throws Exception
    	{
    		int level=getLevel();
    		while(level==1)
    			getLevel();
    			System.out.println("Level 2");
     
    	}
    	public static int getLevel()throws Exception
    	{
    		int  input1;
    		System.out.print("Input: ");
    		input1=Integer.parseInt(input.readLine());
     
    		Random r=new Random();
    		int rand=r.nextInt(100);
    		if(rand<51)
    		{
    			int level1=2;
    			return level1;
    		}
    			else if (rand>=51)
    			{
    				int level1=1;
    				return level1;
    			}
    			//It says return statement should be in this line.
                            //Is it posible to have 2 choices which to be return?
    	}
    }
    Last edited by amakuzaki; June 12th, 2012 at 02:56 AM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    Yes, an if statement can be inside of a do/while statement.
    do {
       if(condition) {
         // do this if true
       }else{
         // do this if false
        }
       // do this stuff always
    }while(more condtiions);
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    amakuzaki (June 12th, 2012)

  15. #14

    Default Re: Almost Giving up :((

    import java.io.*;
    import java.util.*;
    public class Project12
    {
    	public static DataInputStream input=new DataInputStream(System.in);
    	public static void main(String[]args)throws Exception
    	{
    		int level=getLevel();
    		while(level==1)
    			getLevel();
    			System.out.println("Level 2");
     
    	}
    	public static int getLevel()throws Exception
    	{
     
    		Random r=new Random();
    		int rand=r.nextInt(100);
    		if(rand<51)
    		{
    			int  input1;
    			System.out.print("Input: ");
    			input1=Integer.parseInt(input.readLine());
    			int level1=2;
    			return level1;
    		}
    			else if (rand>=51)
    			{
    				int  input1;
    				System.out.print("Input: ");
    				input1=Integer.parseInt(input.readLine());
    				int level1=1;
    				return level1;
    			}
    			//It says return statement should be in this line.
    	}
    }

    I tried what you posted, but i can manage to make it working...
    I have i problem where to initialize the variable level and where to put return statement

    Where should i put variable level and where to initialize it, and how can i return the value inside the IF or ELSE IF...
    Last edited by amakuzaki; June 12th, 2012 at 11:22 PM.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    Please post the full text of any error messages you get.

    where to initialize the variable level and where to put return statement
    The variable: level is defined in the main() method which is defined as void and does not return anything.

    You need to have a return statement outside of the if / else statements because the compiler requires the method to always return something and can not evaluate the logic to know what it could do. It wants to be sure something is returned.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16

    Default Re: Almost Giving up :((

    Have diff problem ;p

    public int getValue()
    {
     
    if(condition)
    return 1;
     
    else if(condition)
    return 2;
     
    //it says return statement should be int this line :(
     
    }
    Last edited by amakuzaki; June 13th, 2012 at 08:58 AM.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    You don't need to define a variable. You can do it this way:
       int value2=2;
       return value2;
    vs 
       return 2;    // return the 2 without using a variable
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Almost Giving up :((

    Quote Originally Posted by amakuzaki View Post
    Still in grip of this one...
    	public static void main(String[]args)throws Exception
    	{
    		int level=getLevel();
    		while(level==1)
    			getLevel();
    			System.out.println("Level 2");
     
    	}
    I am new here, and I am having a problem understanding the intent in your main() function.

    I reformatted your code slightly, and I added a couple of comments to what I think your code is doing. Is this what you meant for it to do?
        public static void main(String[]args)throws Exception
        {
            int level = getLevel(); // Call the function and assign return value to the variable "level"
            /*
               Will enter the loop if (and only if) getLevel() returned 1
               Once it is in the loop, it will not leave until the value
               of the "level" variable changes to something else.
            */
            while(level == 1)
            { 
                getLevel(); // Call the function but ignore the return value
            }
            System.out.println("Level not equal to 1 any more.");
        }

    If my comments do not reflect your intent, maybe you can tell us (in a natural language, not Java) what you had in mind for this program.


    Cheers!

    Z
    Last edited by Zaphod_b; June 13th, 2012 at 12:03 PM.

  20. #19
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Almost Giving up :((

    Quote Originally Posted by amakuzaki View Post
    .
    .
    .
    	public static int getLevel()throws Exception
    	{
     
    		Random r=new Random();
    		int rand=r.nextInt(100);
    		if(rand<51)
    		{
    .
    . // Some stuff
    .			return level1;
    		}
    			else if (rand>=51)
    			{
    .
    . // Some stuff
    .
    				return level1;
    			}
    			//It says return statement should be in this line.
    	}
    }
    how can i return the value inside the IF or ELSE IF...
    Hmmm...Perhaps you over-engineered it. Sometimes less is more. (Extra tests sometimes get in the way of compiler semantics) Sometimes compilers seem to be smarter than us, but, there are rather rigid rules for the compilers.

    Why not try something like the following?
        public static int getLevel() throws Exception
        {
    .
    .
    .
            int rand = r.nextInt(100);
            System.out.println("rand = " + rand); // (Optional: For debugging)
            if(rand < 51)
            {
    .
    .  // Some stuff
    . 
               return level1;
            }
            /* Hmmm... if the integer value is not less than 51, it
             * must be greater than or equal to 51, right?
             * So why confuse the compiler (as well as us mere
             * humans) with a superfluous if?
             */
            else //if (rand>=51) <--- just "else" here, not "else if"
            {
    .
    .  // Some stuff
    .  
              return level1;
            }
        }
    }


    Cheers!

    Z
    Last edited by Zaphod_b; June 13th, 2012 at 12:15 PM.

  21. #20
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Almost Giving up :((

    whats the mean of int level=level+1; ?

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Almost Giving up :((

    That is an assignment statement. The expression to the right of the = is evaluated and its value is assigned to the variable to the left of the =


    This may be wrong. I missed seeing the int. Another reason to post code inside of code wrappers.
    Last edited by Norm; June 21st, 2012 at 11:39 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Almost Giving up :((

    I'm a bit confused here, unless there are two level variables, but is that java statement legal? It's declaring a variable and then initializing it to itself plus one. I'm not sure if that will work. (No it won't, just tried it.)

    int level = level + 1; isn't valid. Claims that level might not have been initialized. Unless you have two level variables, but how would it know which level variable you were referring to even if you did have another level variable?

Similar Threads

  1. giving arraylist name
    By iddheepak in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2011, 01:08 PM
  2. Simple interface example giving erors
    By user2205 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 04:11 PM
  3. Class hierarchy... giving me a headache, so I could use some help
    By Kerr in forum Object Oriented Programming
    Replies: 6
    Last Post: May 30th, 2011, 05:03 PM
  4. xml parsing getting all subtags without giving it's name
    By HungryCoder in forum Java SE APIs
    Replies: 1
    Last Post: May 27th, 2011, 03:27 AM
  5. JavaCompilerobject giving an error WITHOUT ide
    By divs1210 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:49 PM

Tags for this Thread