-
Applet Question: Printing out a special string
Hey guys, ive been working on this applet for awhile and I can't get it.
I need to an applet that reads five numbers (each between 1 and 30). For each number read, it should draw a line containing that number of adjacent asterisks. For example, if input the number seven, it should print *******.
I have this but im not sure what kind of control structure to use that will check to see if the number is in 1-30 parameter and to loop to print out the "*"'s.
Code:
import javax.swing.*;
import java.awt.Graphics;
public class L5a1 extends JApplet{
int num1,
a=1;
public void init()
{
String one,
one = JOptionPane.showInputDialog ( "Enter first number, 1-30 please:" );
//converts string to integer
num1 = Integer.parseInt(one);
num1=a;
num1=1;
}
public void paint ( Graphics g )
{
for (num1=1; num1 > a; num1++)
{
g.drawString ( "*", num1, 10);
}
}
}
Theres only 1 structure in for simplicty. Im not sure what to use for the the check on the 1-30 either. I need some real help on this one, i know what i want to do but don't know how to do it...
thanks
JW
-
Assuming I've followed your question properly this should build up the string that you want to be printed.
Code:
int n; /* Assuming this is going to have been set to the number */
String output = "";
if (n > 30 || n < 0 ) {
/* Do some error stuff it it's too big here (or too small) */
}
for (int count = 0; count < n; count++) {
/* Build up string of *s here */
output += "*";
}
Hope that helps,
Alan
-
you dont specify whether you want these lines of *** to be drawn on top of each other.. alongside each other etc...
so you need to give more info
-
oh yeah, preferably on top of each other stacked.
-
Code:
import javax.swing.*;
import java.awt.Graphics;
public class L5a1 extends JApplet{
int num1,
count;
public void init()
{
String one;
one = JOptionPane.showInputDialog ( "Enter first number, 1-30 please:" );
//converts string to integer
num1 = Integer.parseInt(one);
}
public void paint ( Graphics g )
{
String output = "";
if (num1 > 30 || num1 < 0 )
{
g.drawString("The number does not meet specs!", 10, 10);
}
for (int count = 0; count < num1; count++)
{
g.drawString("*", count, 10);
output += "*";
}
}
}
this is what i have now, but im not sure why the spacing of the astericks are so small, they are bunching up...
-
because youre putting them at single pixel spacing, when you call drawString() in the loop it draws:
* at x=0, y=10
* at x=1, y=10
* at x=2, y=10
do you see?
you may find it easier to make the string before you draw it, or you can multiply the x offset.. its up to you
Code:
str = ""
for(int i=0i<num1;i++){
str = str + "*";
}
drawString(str, 0, 10);
or
drawString("*", count*10, 10)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks