Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab10Lopez {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. boolean run = false;
  7.  
  8. while (!run) {
  9. int input = 0;
  10. int answer1 = 0;
  11. int answer2 = 0;
  12. int increment = 0;
  13. boolean process = false;
  14. while (!process) {
  15. System.out.print("Please enter a number (q to quit): ");
  16. if (scan.hasNextInt()) {
  17. input = scan.nextInt();
  18. scan.nextLine();
  19. if (input < 0) {
  20. System.out.println("You have entered an invalid option, please enter a valid number...\n");
  21. } else {
  22. process = true;
  23. }
  24. } else {
  25. String whatsEntered = scan.nextLine();
  26. if (whatsEntered.equalsIgnoreCase("q")) {
  27. System.out.println("+-------------------------------+");
  28. System.out.println("| Thanks for useing my program! |");
  29. System.out.println("+-------------------------------+");
  30. System.exit(1);
  31. } else {
  32. System.out.println("You have entered an invalid option, please enter a valid number...\n");
  33. }
  34. }
  35. }
  36.  
  37. for (int x = input; x > 0; x--) {
  38. increment++;
  39. answer1 += increment;
  40. }
  41.  
  42. // ReInitializing the variable to 0
  43. increment = 0;
  44.  
  45. while (input > 0) {
  46. increment++;
  47. answer2 += increment;
  48. input--;
  49.  
  50. }
  51.  
  52. System.out.println("For While");
  53. System.out.printf("%-10d%-10d\n", answer1, answer2);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement