I'm having java troubles

Off-topic discussion, where anyone can talk about almost anything.

Moderator: Executive

Post Reply
Tea-Assault
Supporting Member
Supporting Member
Posts: 501
Joined: Wed Apr 03, 2013 3:02 pm
Location: Tiber, waiting for Matsif

I'm having java troubles

Post 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
Image
the end is really fµcking nigh
Image
User avatar
A Docile Sloth
Executive
Executive
Posts: 2323
Joined: Sun Jun 24, 2012 4:32 pm
Location: Somewhere where you aren't.

Re: I'm having java troubles

Post 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("*");
}
Image
Tea-Assault
Supporting Member
Supporting Member
Posts: 501
Joined: Wed Apr 03, 2013 3:02 pm
Location: Tiber, waiting for Matsif

Re: I'm having java troubles

Post by Tea-Assault »

Ah, thanks sloth! I added the other condition, now to find out what that condition is haha :thumbup:
Image
the end is really fµcking nigh
Image
CrookedBarrel
Posts: 308
Joined: Wed Aug 20, 2014 4:07 pm

Re: I'm having java troubles

Post by CrookedBarrel »

Code: Select all

System.out.print("--***++++-----******+++++++");
Image
User avatar
Necromancer
Supporting Member
Supporting Member
Posts: 3315
Joined: Sat Jul 28, 2012 4:20 pm

Re: I'm having java troubles

Post 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("-");
Image
-“Regret your helplessness…and feel despair.”
Achievement Unlocked: Battlefield 4 Uninstalled!!
Tea-Assault
Supporting Member
Supporting Member
Posts: 501
Joined: Wed Apr 03, 2013 3:02 pm
Location: Tiber, waiting for Matsif

Re: I'm having java troubles

Post 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..
Image
the end is really fµcking nigh
Image
Tea-Assault
Supporting Member
Supporting Member
Posts: 501
Joined: Wed Apr 03, 2013 3:02 pm
Location: Tiber, waiting for Matsif

Re: I'm having java troubles

Post 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
Image
the end is really fµcking nigh
Image
Post Reply