Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

help with some basic java

  • 19-11-2012 02:00AM
    #1
    Registered Users, Registered Users 2 Posts: 64
    ✭✭


    anybody tell me why this code will not print out the value for 'i'
    any help greatly appreciated.
    I know its probably the logic in the nested if statements, but i dont understand why.
    for(i = 123 ; i < X ; i++) // output machine
    	   {  //output.println(i + "i");//*******rem latr**********
    
    		   if((x != y) && (x != z) && (y != z))
    		   {
    			   if((x < y) && (y < z))
    			   {
    				   if((x == 0) && (y == 0) && (z == 0))
    				   {
    					   output.println(i); // payload
    				   }
    			   }
    		   }
    
    		   z = z + 1;
    		   if(z == 10)
    		   {
    			   z = 0;
    			   y = y + 1;
    			   if(y == 10)
    			   {
    				   y = 0;
    				   x = x + 1;
    			   }
    		   }
    


Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • Closed Accounts Posts: 2,696 mark renton
    ✭✭✭


    dubdad wrote: »
    anybody tell me why this code will not print out the value for 'i'
    any help greatly appreciared.
    I know its probably the logic in the nested if statements, but i dont understand why.




    for(i = 123 ; i < X ; i++) // output machine
    {

    if((x != y) && (x != z) && (y != z))
    {
    if((x < y) && (y < z))
    {
    if((x == 0) && (y == 0) && (z == 0))
    {
    output.println(i); // payload
    }
    }
    }

    z = z + 1;
    if(z == 10)
    {
    z = 0;
    y = y + 1;
    if(y == 10)
    {
    y = 0;
    x = x + 1;
    }
    }
    Whats the value of x?


  • Registered Users, Registered Users 2 Posts: 64 dubdad
    ✭✭


    sorry
    x =1
    y =2
    z = 3


  • Closed Accounts Posts: 2,696 mark renton
    ✭✭✭


    dubdad wrote: »
    sorry
    x =1
    y =2
    z = 3

    Check your for loop. i is greater than x so loop wont start


  • Closed Accounts Posts: 899 djk1000
    ✭✭✭


    I'm on my phone so it's hard to read, but if i is 123 and x is 1 then the i<x condition isn't met in line 1

    Edit: Looks like I'm too slow!


  • Registered Users, Registered Users 2 Posts: 64 dubdad
    ✭✭


    sorry the X in the for loop counting part is uppercase
    it will be a number between 200 and 1000 depending on the user input


  • Advertisement
  • Closed Accounts Posts: 2,696 mark renton
    ✭✭✭


    dubdad wrote: »
    &#9;   for(i = 123 ; i &#60; X ; i++) 
    
    
    &#9;&#9;   }
    

    i =123 // starts i at 123
    i<x // loop while is less than x - i is greater than x so this is never true


  • Registered Users, Registered Users 2 Posts: 64 dubdad
    ✭✭


    loop runs ok its just not getting through the nested if statements
    to print out values for i

    sorry about the confusion with the two diffrent x and X
    you can tell i am new at this


  • Closed Accounts Posts: 2,696 mark renton
    ✭✭✭


    dubdad wrote: »
    sorry the X in the for loop counting part is uppercase
    it will be a number between 200 and 1000 depending on the user input

    Ok but x,y and z are never equal to 0 so this condition is not met

    Line 8

    Also someone will come in here and wrap ur knuckles for using x and X as variables - programmers hate that kinda stuff!


  • Banned (with Prison Access) Posts: 1,435 areyawell
    ✭✭✭


    What exactly are you trying to do?. The code is all over the place. What are they asking you to do in the assignment?

    if((x == 0) && (y == 0) && (z == 0))
    {
    output.println(i); // payload

    You want all x,y and z to be 0 to print out i?


  • Registered Users, Registered Users 2 Posts: 64 dubdad
    ✭✭


    ok all i got it sorted
    now i'm off to bed : }


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 64 dubdad
    ✭✭


    you are right of course
    i heed to have :
    if((x != 0) && (y != 0) && (z != 0))
    {
    output.println(i); // payload
    }
    on that line

    spotted it and it spun the numbers out for me

    i was trying to find the 'ordered' numbers between 123 and a variable, the large 'X'

    thank you all very much for your time in any event

    dd..


  • Technology & Internet Moderators Posts: 28,850 oscarBravo
    Mod ✭✭✭✭


    Glad you got it sorted, but john47832 is right: using a lowercase and an uppercase X as variables is going to get people swearing at you. Choosing useful variable names is an important part of writing maintainable code, and two variable names that look almost identical is a recipe for subtle bugs.


Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement