Navarro Loop

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CASE 7

public class Main {

public static void main(String[] args) {

int rows = 5;

for (int i = rows; i >= 1; --i) {

for (int j = 1; j <= i; ++j) {

System.out.print(j + " ");

System.out.println();

}
CASE 8

public class Main {

public static void main(String[] args) {

int rows = 5;

for (int i = 1; i <= rows; ++i) {

for (int j = 1; j <= i; ++j) {

System.out.print(j + " ");

System.out.println();

}
CASE 9

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// Creates scanner object

Scanner scanner = new Scanner(System.in);

// Controls the start and stop of the program

Character ans = 'Y';

char controller = 'Y';

// Sets and resets the operand and answer variables

int fnum = 0;

int snum = 0;

int answer = 0;

// While loop that keeps the program running until user exits

while ((ans.equals('Y')) || (ans.equals('y'))) {

// Calculator interface

System.out.println("=====================================");

System.out.println(" Calculator V1.0 ");

System.out.println("=====================================");

System.out.println("A. Addition");

System.out.println("S. Subtraction");

System.out.println("M. Multiplication");

System.out.println("D. Division");
System.out.printf("Please pick an operation(A/S/M/D): ");

// Prompts user to choose an operation

String operation = scanner.nextLine();

System.out.print("");

// Does stuff depending on what integer the user enters

switch (operation) {

case "A":

System.out.printf("Enter first number: ");

fnum = scanner.nextInt();

System.out.printf("Enter second number: ");

snum = scanner.nextInt();

answer = fnum + snum;

System.out.println("The answer is: " + answer);

System.out.println("Do you want to try again (Y/N):");

controller = scanner.next().charAt(0);

break;

case "S":

System.out.printf("Enter first number: ");

fnum = scanner.nextInt();

System.out.printf("Enter second number: ");

snum = scanner.nextInt();

answer = fnum - snum;

System.out.println("The answer is: " + answer);

System.out.println("Do you want to try again (Y/N):");

controller = scanner.next().charAt(0);

break;
case "M":

System.out.printf("Enter first number: ");

fnum= scanner.nextInt();

System.out.printf("Enter second number: ");

snum = scanner.nextInt();

answer = fnum * snum;

System.out.println("The answer is: " + answer);

System.out.println("Do you want to try again (Y/N):");

controller = scanner.next().charAt(0);

break;

case "D":

System.out.printf("Enter first number: ");

fnum = scanner.nextInt();

System.out.printf("Enter second number: ");

snum = scanner.nextInt();

answer = fnum / snum;

System.out.println("The answer is: " + answer);

System.out.println("Do you want to try again (Y/N):");

controller = scanner.next().charAt(0);

break;

default:

System.out.println("Invalid operation!");

break;

if (controller == 'N' || controller == 'n'){


ans = 'N';

if ((ans.equals('N')) || (ans.equals('n'))){

System.out.println("Thank You!");

You might also like