Computer problems,Computer help
*AX SOFT>>>Programming & Design

Java Nested For Loops question?!?!?


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

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • What is the most popular C++ compiler and linker used in the programming industry today?

    Well, the compiler that industries use is of cause Visual Studio from Microsoft. Microsoft offers free Express edition for personal and student use.

    ...
  • Do companies still uses Visual Basic 6.0?

    A lot of companies still used VB6 because of the legacy issues. New windows like Vista still support VB6. For more info on VB6, visit my free tutorial site at ...

  • Where can I get a Turbo C?

    Download it from below link ...

  • Qbasic Character Help?

    Wow! QBasic... haven't used that in ages. I believe the code would be "chr(191)" to convert the ASCII code into a character. if that's not it, try Chr$(191)...

    ...
  • What u say about it ?

    It is very cool. The colour is nice. You should add a few pictures of the place you describe to improve the web site.

    ...
  • I'm learning C++?

    you make what ever you heart desires.. calculators.. math programs.. operating systems,.. videogames... text editors.. accounting software.. etc.. (your problem with the split second viewing is...

  • Setting up path on Myeclipse for a project deployed on Jboss?

    ...

  • AP Computer Science PLZ HELP!!! JAVA?

    May be you can contact a JAVA expert live at website like ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster