Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Help! how to construct program to run in dos
|
Posted: Feb 10, 2003 12:51 PM
|
|
I don't know what you mean about rows and columns, but how's this?
/**
* Nummy.java
*
* Simple demo of printing meaningless rows of numbers.
*
* Copyright 2003, Matt Gerrans.
*
* Turning in this code as homework is a violation of ethical principals,
* and punishable by severe penalties, even if you change the variable
* names.
*/
public class Nummy
{
/**
* Gets the number of colums (and lines) to display from the command
* line. Returns 10, by default (in case of error, or no parameters).
*
* (You can ignore this part, if you are getting the number of columns
* some other way.)
*/
public static int getColumnCountFromCommandLine( String args[] )
{
int n = 10;
if( args.length > 0 )
try
{
n = Integer.parseInt(args[0]);
}
catch( NumberFormatException nfe )
{
String msg =
nfe.getMessage() + ", integer conversion is not impossible!\n" +
"You should specify the number of lines you'd like to see as\n" +
"a plain old integer value, like 100.\n" +
"Here's the results with " + n + ":\n";
System.err.println( msg );
}
return n;
}
/**
* Simple method to print out a silly bunch of lines.
* This one more closely mimics this Python code:
* <pre>
%
* for i in range(howManyColumns):
* print ' '*i + str(i)[-1]*(howManyColumns-i)
*
* </pre>
*
* Note that it is probably a bad idea to have this method write
* directly to the screen, but this is after all, a very simple
* method which demonstrates for-loops, if not good program design.
*/
public static void simpleMethod( int howManyColumns )
{
for( int i = 0; i < howManyColumns; i++ )
{
for( int j = 0; j < i; j++ )
System.out.print( " " );
// "Integer.toString(i%10)" gives you the single-character string for the last digit of i.
for( int j = i; j < howManyColumns; j++ )
System.out.print( Integer.toString(i%10) );
System.out.println();
}
}
public static void main(String args[])
{
int howManyColumns = getColumnCountFromCommandLine(args);
simpleMethod(howManyColumns);
}
}
|
|