Hey, so my assignment is to make a pattern like this(- = space):
-*-
***
-*-
Based on the number that the use inputs (ie if the input is 2 the output should look like that). If the input would be 3 the output should look like this:
--*--
-***-
*****
-***-
--*--
ECT.
So I have set my code up like follows, and I wonder if this would be the easiest way to do this, because I have a feeling that there is a much easier way then creating 2 for loops. Anyways here is what I have:
(included in the additional details...) public static void main (String [] args){
int input = Integer.parseInt(JOptionPane.showInputDi... a number: "));
String s1 = "";
String s2 = "*";
int space = input/2;
int stars = input/2;
for (int rows = 1; rows <= input; rows++){
for (int columns=0; columns<= space; columns++){
s1+=" ";
}
System.out.print ( s1+ s2+s1);
System.out.println("");
s2+="**";
s1="";
space-=1;
}
s1=" ";
s2="*";
for (int rows = 1; rows <= input-1; rows++){
for (int columns=1; columns<= stars; columns++){
s2+="**";
}
System.out.print ( s1+ s2+s1);
System.out.println("");
s2="*";
s1+=" ";
stars-=1;
}
}
} Here's my solution, it must have taken me 2 or 3 hours. This is a tough one, I think. When my mind got boggled with nested loops and complex logic, I broke things apart into separate methods and tried to name everything to keep me straight.
package com.kaydell.answers;
import javax.swing.JOptionPane;
public class Puzzle1 {
// define constants for this class.
final static char UNDER_SCORE = '_';
final static char ASTERISK = '*';
private final static int FORWARD = 1;
private final static int BACKWARD = -1;
private final static int MIN_ASTERISKS = 1;
public static void main (String [] args) {
// get an integer from the user that is 2 or more
int n = 0;
boolean good = false;
do {
String input = JOptionPane.showInputDialog("Enter a whole number of 2 or greater: ");
if (input == null) return;
try {
n = Integer.parseInt(input);
good = (n >= 2);
} catch (Exception e) {
good = false;
}
} while (!good);
// output pattern based on the number, n
int maxUnderscores = n - 1;
int underscores = maxUnderscores;
int asterisks = MIN_ASTERISKS;
int increment = FORWARD;
// repeately output the pattern specified.
while (true) {
// output one line
outputOneLine(underscores,asterisks...
// if the last line was just printed, then exit this loop
if ((underscores == maxUnderscores) && (increment == BACKWARD)) {
break;
}
// if we just printed all asterisks, then count down the other way
if ((underscores == 0) && (increment == FORWARD)) {
increment = BACKWARD;
}
// calculate the number of underscores and asterisks for the next loop
underscores -= increment;
asterisks += (2*increment);
} // end while
}
public static void outputOneLine(int underscores, int asterisks) {
outputChar(UNDER_SCORE,underscores);
outputChar(ASTERISK,asterisks);
outputChar(UNDER_SCORE,underscores);
System.out.println();
}
public static void outputChar(char ch, int i) {
for (int j = 1; j <= i; j++) {
System.out.print(ch);
}
}
} I tried to solve the problem in general with numbers 2 and above.
try the numbers 2,3,4,...10
and my solution will still work Report It
|