Page 1 of 1

I'm having java troubles

Posted: Sat Dec 06, 2014 11:17 am
by Tea-Assault
Hey guys, I'm trying to learn java at the moment at university, and I have run into a slight issue with using for loops.

Basically I have to create this sequence "--***++++-----******+++++++" and this is what I have come up with so far:

I can get the sequence to read out this: 223334444555556666667777777
with this snippet of code

Code: Select all

int a, b;
		for (a=2;a<8;++a)
		{
			for (b=1;b<a+1;++b)
			{
				System.out.print(a);
			}
		}
But I am struggling with trying to replace the numbers with the symbols. I think it might involve including conditional statements. I tried this but I think I may have gone wrong with the way I have decided to do it as it reads out this: ++***+++++++++******+++++++

Code: Select all

int a, b;
		for (a=2;a<8;++a)
		{
			for (b=1;b<a+1;++b)
			{
				if (a%3==0)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print("+");
				}
			}
		}
any help would be great :D

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 11:47 am
by A Docile Sloth
You just need to add in a condition for printing "-"

You have three different output so you need three parts of the if statement:

Code: Select all

if(a%3==0)
{
   System.out.print("*");
}
else if(condition for printing -)
{
   System.out.print("-");
}
else
{
   System.out.print("*");
}

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 12:03 pm
by Tea-Assault
Ah, thanks sloth! I added the other condition, now to find out what that condition is haha :thumbup:

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 12:07 pm
by CrookedBarrel

Code: Select all

System.out.print("--***++++-----******+++++++");
Image

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 12:08 pm
by Necromancer
not sure what the problem is, seems like you got the idea.

Code: Select all

if ((a==2) || (a==5)) System.out.print("-");
and so on.

you can try to make it more "elegant" using different methods and/or mathematical properties of the numbers.

Code: Select all

if (a%3 == 0) System.out.print("*");
if (a%3 == 1) System.out.print("+");
if (a%3 == 2) System.out.print("-");

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 12:40 pm
by Tea-Assault
So yeah I don't know if this is cheating exactly, but it gave me the right readout in the screen haha

Code: Select all

int a, b;
		for (a=2;a<8;++a)
		{
			for (b=1;b<a+1;++b)
			{
				if (a%3==0)
				{
					System.out.print("*");
				}
				else if (((a%2==0)&&(a<4))||(a%5==0))
				{
					System.out.print("-");
				}
				else
				{
					System.out.print("+");
				}
			}
		}
Could be on the same level as going "System.out.print("--***++++-----******+++++++") to try to answer the question..

Re: I'm having java troubles

Posted: Sat Dec 06, 2014 12:42 pm
by Tea-Assault
Oh necromancer you glorious bastard! I forgot that you could have more than one remainder! :lol: I spent 6 hours in the library today trying to work out how to do this hahaha

EDIT: I am genuinely so happy right now