Length of a right Triangle (Java Eclipse): User input chart -


import java.util.scanner;  public class triangledriver {   public static void main(string[] args) {     system.out.println("please enter value of n: ");      scanner scan = new scanner(system.in);     int num = scan.nextint();      (int = 1; <= num; i++) {         system.out.printf("%d\t", i);     }     system.out.println("");     system.out.println("----    ----");     (int = 0; < num; i++) {         system.out.print((i + 1) + "    ");         (int j = 1; j <= num; j++) {             system.out.printf("%7.2f", math.sqrt(i * + j * j));         }         system.out.println();     } } } 

this useful others new coding. need on though. when input number, 8,

-it not spaced properly

-the first actual row of data should not include 1.00, 2.00, 3.00

-personal preference: include ---- on top after numbers , | on side j numbers

what guys think?

this close how code should look

public class triangledriver {     private static final string request = "please enter value of n: ";     private static final string spacer3 = "   ";     private static final string spacer4 = "    ";     private static final string divider = "----";     private static final string format_one = "  %d    ";     private static final string format_first_row = "%7.0f";     private static final string format_additional_rows = "%7.2f";     private static final int header_lines = 2;      public static void main(string[] args) {         int num = getintinput(request);         stringbuilder[] result = solution(num);         printsbarray(result);     }      static private int getintinput(string request){         system.out.print(request);         return new scanner(system.in).nextint();     }      static private void printsbarray(stringbuilder[] sbarray){         (stringbuilder sb : sbarray)             system.out.println(sb.tostring());     }      static private stringbuilder[] solution(int num){         stringbuilder output[] = new stringbuilder[num + header_lines];         for(int = 0; < output.length; i++)             output[i] = new stringbuilder("");          // build line 0         output[0].append(spacer4 + spacer4);         (int = 1; <= num; i++) {             output[0].append(string.format(format_one, i));         }          // build line 1         output[1].append(divider + spacer4);         (int = 0; < num; i++)             output[1].append(string.format(divider + spacer3));          // build line 2         int = 0;         output[2].append((i + 1) + spacer4);         (int j = 1; j <= num; j++) {             output[2].append(string.format(format_first_row, math.sqrt(i * + j * j)));         }          // build line 3+         (i = 1; < num; i++) {             output[i + 2].append((i + 1) + spacer4);             (int j = 1; j <= num; j++) {                 output[i + 2].append(string.format(format_additional_rows, math.sqrt(i * + j * j)));             }         }         return output;     } } 

this gives separation of functionality, keeps strings out of logic code, , minimizes main essential calls. uses stringbuilder build result. allows maintain separation of functionality. code should 1 thing, ie calculate results not print them. technically, should have been separated it's own class.

on side note, not turn in homework teacher might bit suspicious.


Comments

Popular posts from this blog

c# - ItextSharp font color issue in ver 5.5.4+ -

jquery - Multiple issues with pushstate: history, loading, calling functions -

ios - retrievePeripherals deprecated in IOS7 how to substitude it with retrievePeripheralsWithIdentifiers -