Skip to content

Instantly share code, notes, and snippets.

@bahodge
Created February 28, 2018 16:57
Show Gist options
  • Save bahodge/97cabb631c770e2065c9956c4ed89719 to your computer and use it in GitHub Desktop.
Save bahodge/97cabb631c770e2065c9956c4ed89719 to your computer and use it in GitHub Desktop.
A loop that builds a table in the console between a low and high user input number
Scanner scan = new Scanner(System.in);
boolean userCont;
String userAns;
do {
System.out.print("Enter your min value: ");
int userLow = scan.nextInt();
System.out.print("Enter your high value: ");
int userHigh = scan.nextInt();
System.out.println("number | squared | cubed");
System.out.println("------ | ------- | -----");
for (int i = userLow; i <= userHigh; i++){
System.out.printf("%-7d" + '|' +"%-9d" + '|' + "%d", i, (int)Math.pow(i, 2), (int)Math.pow(i, 3));
System.out.println();
}
System.out.print("Would you like to continue? [y/n]");
userAns = scan.next();
userCont = userAns.equalsIgnoreCase("y");
} while (userCont);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment