|
-
Need help with this Code, please.
I have this program. The output of the program is “OH”.
How to shorten the program but with sticking to the same idea? Meaning, not making major changes to the program. Just having lesser codes
import javax.swing.JOptionPane;
public class HouseB
{
public static void main(String args[])
{
int no_rows;
String str_no_rows;
str_no_rows = JOptionPane.showInputDialog("Enter row numbers (3 or 5):");
no_rows = Integer.parseInt(str_no_rows);
switch (no_rows)
{
case 3: {
print_x(0);
print_x(4);
print_x(0);
break;
}
case 5:
{
print_x(0);
print_x(4);
print_x(4);
print_x(4);
print_x(4);
print_x(4);
print_x(0);
break;
}
default:
JOptionPane.showMessageDialog(null, "Invalid data, Try again!");
break;
}
System.out.println(" ");
switch (no_rows)
{
case 3: {
print_x(4);
print_x(0);
print_x(4);
break;
}
case 5:
{
print_x(4);
print_x(4);
print_x(4);
print_x(0);
print_x(4);
print_x(4);
print_x(4);
break;
}
default:
JOptionPane.showMessageDialog(null, "Invalid data, Try again!");
break;
}
}
public static void print_x(int no_spaces)
{
if (no_spaces==0)
{
System.out.println("******");
}
else
System.out.print("*");
for (int i=0; i<no_spaces; i++)
{
System.out.print(" ");
}
System.out.println("*");
}
}
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