Hi all,
I have a test tomorrow morning and I'm stuck on this one question on my review and here it is:
1. Write a method that takes an integer n, and returns a String. Assume the integer n is an odd integer between 1 and 9. If the returned String is printed, it should display a box like the one below, when n = 9. The integers in the last row will be n.
-----1-----
----333----
---55555---
--7777777--
-999999999-
What I have so far has some problems that I can't figure out how to fix:
import java.util.*;
public class NumberFigure
{
public static String dashes(int num)
{
String theDashes = "";
for (int i = 0; i < num; i++)
{
theDashes += "-";
}
return theDashes;
}
public static String numbers(int number)
{
String theNumbers = "";
for (int i = 0; i <= number; i++)
{
theNumbers += number;
}
return theNumbers;
}
public static void main(String[] args)
{
System.out.println("Enter 1, 3, 5, 7, or 9");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
String display = box (num);
}
public static String box(int n)
{
String theBox = "";
for (int i = 1; i <= n; i+=2)
{
theBox += dashes((11-i) / 2);
theBox += numbers(i);
theBox += dashes((11-i) / 2);
}
return theBox;
}
}