Cant Get It To Work

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 482 Views - Last Post: 05 November 2011 - 04:55 PM Rate Topic: -----

#1 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Cant Get It To Work

Posted 02 November 2011 - 10:53 PM

long NUM = 2000;

for (int x = 0; x < NUM; x++){
NUM = (long) Math.pow(2, x);
}

for (int y = 0; y < 35; y++) {
for (int z = 0; z < 7; z++) {
System.out.print(y++ + " ");
}
y--;
System.out.println();


So I keep running this and I just 0 1 2 3 4 5 6 7 and the next line 8 9 10 11 12 13 14...and Im trying to get it to return the powers of 2. Im doing something wrong but I cant figure out what.

And sorry about the ambiguous title. Should have been more clearer.

Is This A Good Question/Topic? 0
  • +

Replies To: Cant Get It To Work

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 02 November 2011 - 10:55 PM

What do you really want to do? Your code looks illogical to me, could you please paste the whole method?
Was This Post Helpful? 0
  • +
  • -

#3 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 02 November 2011 - 10:58 PM


public static void main (String[] args){
		new Scanner(System.in);
		long NUM = 2000;
		
		for (int x = 0; x < NUM; x++){
	    	  NUM = (long) Math.pow(2, x);
	       }
	      
	      for (int y = 0; y < 35; y++) {
	          for (int z = 0; z < 7; z++) {
	             System.out.print(y++ + " ");
	          }
	          y--;
	          System.out.println();
	      
	      }

		}
}


Im trying to get the code to print the power of 2.
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 02 November 2011 - 11:32 PM

Eh, my observations:

1. Scanner is not properly formed.
2. Scanner import is probably missing.
3. Why are you using long?
4. What are the for loops for?
6. Why are you decrementing y?

A simple power of 2 program looks like this:
public static void main(String[] args)	{
	Scanner sc = new Scanner(System.in);
	int input = sc.nextInt();
	int powered = (int)Math.pow(input,2);
	System.out.println("The power of 2 for "+ input + " is "+ powered);
}


Was This Post Helpful? 2
  • +
  • -

#5 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 02 November 2011 - 11:33 PM

Dude that makes more sense. Thank you so much.
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 02 November 2011 - 11:49 PM

That solves it? What? Oh well. Ask again if you get stuck somewhere, next time show proper code and ask clearer questions or else your topic will be closed. Good luck. ^^
Was This Post Helpful? 0
  • +
  • -

#7 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 03 November 2011 - 12:03 AM

Wait never mind its still not working. So I guess I should be more clear Im trying to calculate all the powers of 2 up to number 2000. The reason why there's a y is because one each line 7 numbers are supposed to be there. Im still trying to work the top part.
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 03 November 2011 - 12:12 AM

Shouldn't a simple for loop do that work for you?

int powered = 0;
for(int x = 1; x < 2000; x++){
powered = (int)Math.pow(x,2);
	System.out.println("The power of 2 for "+ x + " is "+ powered);
}


Was This Post Helpful? 0
  • +
  • -

#9 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 03 November 2011 - 01:01 AM

Nope. I think theres a formula to this but I cant remember what it was.
Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 03 November 2011 - 01:15 AM

Oh, I see, so the limit is 2000?
Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 03 November 2011 - 01:30 AM

Might be you want to check up to what number it is possible before 2000 comes up when powered by 2? That could be achieved by a while loop. Could you try making a sample input/output?
Was This Post Helpful? 0
  • +
  • -

#12 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 03 November 2011 - 01:53 AM

YEah its up to 20000. Gah I know how to do this but I forgot how to structure it. I know you cant use long because it goes beyond it.
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Cant Get It To Work

Posted 03 November 2011 - 05:31 PM

Now I'm confused. You say now that the threshold is 20000, 2000+0, which actually makes sense if you want to use long if and only if you plan to power up every number up to that threshold. But if you're looking for all the numbers that will be powered by 2 and not cross that threshold, you really don't need long. So which is it?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Cant Get It To Work

Posted 03 November 2011 - 07:31 PM

This code does not make sense
		long NUM = 2000;
		 
		for (int x = 0; x < NUM; x++){
		 NUM = (long) Math.pow(2, x);
		 }


You start with x = 0
set NUM to Math.pow(2,0) which is 1
execute x++ which set x to 1
the test x < NUM is performed... as x = 1 is not < NUM which is 1 the for loop exits
Was This Post Helpful? 0
  • +
  • -

#15 GennyBK75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-October 11

Re: Cant Get It To Work

Posted 05 November 2011 - 03:52 PM

Sorry out of town. Just came back So here's the question since Im an idiot for not posting this up earlier. Im supposed to make java print out the power of 2 up to 20,000. (note: not 2000). Thats why I was using long. Other wise I'd have this figured out by now. I think Im supposed to use a for loop ( power = power + 1) but I could be wrong. This is confusing as crap.

make java calculate all power of 2 up to number 20000. ( Not 2000)

^The above is the correct wording not the one in the previous post.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2