-
Make triangle with '*' & print number (School :()
-
Hint for the first, run the loop from 1 to N and print 2N -1 asterisks on each line.
Happiness is good health and a bad memory.
-
hhMMM im new to java (i only used php in the past) so can u be more explainable?
I just dont know what to print and how it print (println or print) in the loop
-
I found the solution for 2nd code, its the following :
Code:
System.out.print("Input number R:");
int R = Integer.parseInt(br.readLine());
while (R<0 || R>99999) {
System.out.println("Error. Wrong Input. Try Again");
System.out.print("Input number R:");
R = Integer.parseInt(br.readLine());
System.out.print("The digit number is :");
}
int countzero = 0; // Apothikeusi Midenikwn Stixeiwn
if (R<10) {
System.out.println("1 Digit");
countzero = 4;
} else if (R<100) {
System.out.println("2 Digits");
countzero = 3;
} else if (R<1000) {
System.out.println("3 Digits");
countzero = 2;
} else if (R<10000) {
System.out.println("4 Digits");
countzero = 1;
} else if (R<100000) {
System.out.println("5 Digits");
countzero = 0;
}
for (int g=0; g<countzero; g++)
System.out.print("0");
System.out.println(R);
I must make the tree only now
-
Code:
for(int i = 1; i <= n; i++) { // n is the number of lines
int numOfSpaces = /* ? */;
int numOfAsterisks = /* ? */;
for(int j = 1; j <= numOfSpaces; j++)
System.out.print(' ');
for(int j = 1; j <= numOfAsterisks; j++)
System.out.print('*');
System.out.print('\n');
}
Try to finish it now. If you don't see it immediatelly, write down on paper the number of spaces and asterixes for each line. Try to see how those number relate to i.
Last edited by prometheuzz; 04-19-2006 at 04:22 PM.
-
Ok, here's how I did the code for the triangle
Code:
import java.util.*;
import javax.swing.JOptionPane;
public class pyramid{
public static void main (String[]Args){
int input = 0;
String temp_string = JOptionPane.showInputDialog("Please enter an integer");
temp_string = temp_string.trim();
input= Integer.parseInt(temp_string);
for (int i=1; i<=input; i++)
{
for (int j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
This just gets you an output like
*
**
***
as opoosed to being lined up like you said
-
Thank you a lot, i really appreciated ur help
-
PHP Code:
import javax.swing.JOptionPane;
public class TriangleWithAsterisk {
public static void main(String[] args)
{
int n;
n = Integer.valueOf(JOptionPane.showInputDialog(null, "Enter the number of lines --> "));
for (int i = 0; i < n; i++)
{
int stars = 1 + 2 * (i);
int space = n - i;
for (int j = 0; j < space; j++)
{
System.out.print(" ");
}
for (int k = 0; k < stars; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
i got an output like this when i entered 6
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
-
Geeeeeeeeee thanx a lot 
Really aprreciated your help!
Similar Threads
-
By Mark Campisi in forum .NET
Replies: 0
Last Post: 07-14-2002, 04:05 PM
-
By Ofir in forum oracle.general
Replies: 0
Last Post: 06-24-2002, 05:28 AM
-
By Kofi Brown in forum Database
Replies: 4
Last Post: 05-09-2000, 09:37 AM
-
Replies: 2
Last Post: 03-24-2000, 01:05 PM
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