java - Adding integers to Multiplication Table -
java - Adding integers to Multiplication Table -
using command line command:
java class -table 7
is supposed print table:
7 7 7 7 7 7 7 7 7 7 7 8 9 10 11 12 13 14 15 16 7 9 11 13 15 17 19 21 23 25 7 10 13 16 19 22 25 28 31 34 7 11 15 19 23 27 31 35 39 43 7 12 17 22 27 32 37 42 47 52 7 13 19 25 31 37 43 49 55 61 7 14 21 28 35 42 49 56 63 70 7 15 23 31 39 47 55 63 71 79 7 16 25 34 43 52 61 70 79 88
which 10 x 10 multiplication table "7" added each number. in other words, adds 7 each number in next table:
0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81
the next code have far. prints out original table starting @ 0. not understand how suppose add together args[1]
each value. (args[1]
beingness 7 in case above). how add together args[1]
each value in table?
i supposed print "argument count mismatch" if user not set 1 int
after -table
in command line.
for code have, prints "argument count mismatch" @ appropriate times, prints numbers, prints line 100 times. understand should set part of code outside of loops, because want print once. how implement that?
private static void table(string[] args) { int[][] table = new int[10][10]; (int = 0; < 10; i++) { (int j = 0; j < 10; j++) { if (args.length != 2) system.out.println("argument count mismatch"); else system.out.printf("%6d", * j); } system.out.println(); } }
okay, parse sec command line argument int , add together each value:
private static void table(string[] args) { int numtoadd = integer.parseint(args[1]); int[][] table = new int[10][10]; (int = 0; < 10; i++) { (int j = 0; j < 10; j++) { system.out.printf("%6d", * j + numtoadd); } system.out.println(); } }
not sure why created table double array, since never utilize it.
and output:
java test -test 7 7 7 7 7 7 7 7 7 7 7 7 8 9 10 11 12 13 14 15 16 7 9 11 13 15 17 19 21 23 25 7 10 13 16 19 22 25 28 31 34 7 11 15 19 23 27 31 35 39 43 7 12 17 22 27 32 37 42 47 52 7 13 19 25 31 37 43 49 55 61 7 14 21 28 35 42 49 56 63 70 7 15 23 31 39 47 55 63 71 79 7 16 25 34 43 52 61 70 79 88
i create different method:
public static boolean validateargs(string[]args){ //all code validate args here. //print validation specific errors here }
then main:
public static void main(string[]args){ if(validateargs(args)){ table(args); } }
java arrays
Comments
Post a Comment