I need someone to help fix the following code and re-write into IF-THEN logic?

public class Mystery4 extends Applet{
	int clickCount=0;
	public boolean mouseDown(Event evt,int x,int y) {
		clickCount++;
		repaint();
		return true;
	}
 
	public void paint(graphics g) {
		g.drawString("Click 10 times!",80,30);
		g.drawString("Clicks: "+clickCount, 120,50);
 
		switch(clickCount) {
			case 0: g.drawString("You haven't clicked yet!",70,100); break;
			case 1:	g.drawString("You clicked once!",90,100); break;
			case 2:	g.drawString("You clicked 2 times!",80.100); break;
			case 10: g.drawString("You click 10 times!",90,100); break;
			default: g.dawString("You click more than 3 times,",50,80);
				 g.drawString("but not 10 times!",90,100);
		}
	}
}

Recommended Answers

All 4 Replies

This should be your main switch() convertered to if statements...

if(clickcount==0)
 
{
	g.drawString("You haven't clicked yet!",70,100);
}
else if(clickcount==0)
{
	g.drawString("You clicked once!",90,100);
}
else if(clickcount==2)
{
	g.drawString("You clicked 2 times!",80.100);
}
else if(clickcount==10)
{
	g.drawString("You click 10 times!",90,100);
}
else
{
	g.dawString("You click more than 3 times,",50,80);
	g.drawString("but not 10 times!",90,100);
}

Also, when posting code, please wrap them in [/code ] tags (since I didn't want it to parse the tags there, remove the space between the e and the ]).[code ] [/code ] tags (since I didn't want it to parse the tags there, remove the space between the e and the ]).

This should be your main switch() convertered to if statements...

if(clickcount==0)

{
    g.drawString("You haven't clicked yet!",70,100);
}
else if(clickcount==0)
{
    g.drawString("You clicked once!",90,100);
}
else if(clickcount==2)
{
    g.drawString("You clicked 2 times!",80.100);
}
else if(clickcount==10)
{
    g.drawString("You click 10 times!",90,100);
}
else
{
    g.dawString("You click more than 3 times,",50,80);
    g.drawString("but not 10 times!",90,100);
}

Thank you very much for your help. I am still learning JAVA.

So, you learned switch before if? That's interesting; I always thought if would come before switch ;-).

I am in the middle of the a class. It seems to be going quickly for a beginner java class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.