Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Lab10Lopez {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- boolean run = false;
- while (!run) {
- int input = 0;
- int answer1 = 0;
- int answer2 = 0;
- int increment = 0;
- boolean process = false;
- while (!process) {
- System.out.print("Please enter a number (q to quit): ");
- if (scan.hasNextInt()) {
- input = scan.nextInt();
- scan.nextLine();
- if (input < 0) {
- System.out.println("You have entered an invalid option, please enter a valid number...\n");
- } else {
- process = true;
- }
- } else {
- String whatsEntered = scan.nextLine();
- if (whatsEntered.equalsIgnoreCase("q")) {
- System.out.println("+-------------------------------+");
- System.out.println("| Thanks for useing my program! |");
- System.out.println("+-------------------------------+");
- System.exit(1);
- } else {
- System.out.println("You have entered an invalid option, please enter a valid number...\n");
- }
- }
- }
- for (int x = input; x > 0; x--) {
- increment++;
- answer1 += increment;
- }
- // ReInitializing the variable to 0
- increment = 0;
- while (input > 0) {
- increment++;
- answer2 += increment;
- input--;
- }
- System.out.println("For While");
- System.out.printf("%-10d%-10d\n", answer1, answer2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement