Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 14

CSE110 Lecture 14

Principles of Programming with Java
Loops and Conditional statements
(202005)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110 Principles of Programming with Java Lecture 14: Loops and

    Conditional statements Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 break

    Statement We can use a break statement to get out of the loop for (int i=1; i<=100; i=i+2) { System.out.println(i); if (i % 15 == 0) { break; } }
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 continue

    Statement • After executing a continue statement, the rest of the statements within the loop will be skipped, then the loop condition will be evaluated again. for (int i=1; i<=4; i+=1) { System.out.println(i); System.out.println("Before"); if (i % 2 == 0) { continue; } System.out.println("After"); }
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 TicTacToe.java

    import javax.swing.*; public class TicTacToe { public static void main(String [] args) { // 1. initialize do { // 2. user move // 3. continue? // 4. computer move // 5. winner or tie? // 6. print } while (true); } }
  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 TicTacToe.java

    // 1. initialize char a = ' ', b = ' ', c = ' '; char d = ' ', e = ' ', f = ' '; char g = ' ', h = ' ', i = ' '; System.out.println("Game: "); System.out.println(" " + a + " | " + b + " | " + c); System.out.println("----------"); System.out.println(" " + d + " | " + e + " | " + f); System.out.println("----------"); System.out.println(" " + g + " | " + h + " | " + i);
  6. // 2. user move boolean userFail; do { String option

    = JOptionPane.showInputDialog("Player:"); int move = Integer.parseInt(option); userFail = true; switch (move) { case 1: if (a == ' ') {a = 'X'; userFail = false;} break; case 2: if (b == ' ') {b = 'X'; userFail = false;} break; case 3: if (c == ' ') {c = 'X'; userFail = false;} break; case 4: if (d == ' ') {d = 'X'; userFail = false;} break; case 5: if (e == ' ') {e = 'X'; userFail = false;} break; case 6: if (f == ' ') {f = 'X'; userFail = false;} break; case 7: if (g == ' ') {g = 'X'; userFail = false;} break; case 8: if (h == ' ') {h = 'X'; userFail = false;} break; case 9: if (i == ' ') {i = 'X'; userFail = false;} break; default: userFail = true; } } while (userFail == true);
  7. // 3. continue? if (a!=' ' && b!=' ' &&

    c!=' ' && d!=' ' && e!=' ' && f!=' ' && g!=' ' && h!=' ' && i!=' ') break;
  8. // 4. computer move boolean computerFail = false; do {

    double x = Math.random(); double y = Math.random(); if (x <= 0.33) { if (y <= 0.33) { if (a == ' '){a = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (b == ' ') {b = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (c == ' ') {c = 'O'; computerFail = false;} else {computerFail = true;} } } else if (x > 0.33 && x < 0.66) { if (y <= 0.33) { if (d == ' ') {d = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (e == ' ') {e = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (f == ' ') {f = 'O'; computerFail = false;} else {computerFail = true;} } } else if (x >= 0.66) { if (y <= 0.33) { if (g == ' ') {g = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (h == ' ') {h = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (i == ' ') {i = 'O'; computerFail = false;} else {computerFail = true;} } } } while (computerFail == true);
  9. // 5. a winner or tie? if (a=='O' && b=='O'

    && c=='O' || d=='O' && e=='O' && f=='O' || g=='O' && h=='O' && i=='O' || a=='O' && d=='O' && g=='O' || b=='O' && e=='O' && h=='O' || c=='O' && f=='O' && i=='O' || a=='O' && e=='O' && i=='O' || c=='O' && e=='O' && g=='O' ) { System.out.println("YOU LOST!"); break; } else if (a=='X' && b=='X' && c=='X' || d=='X' && e=='X' && f=='X' || g=='X' && h=='X' && i=='X' || a=='X' && d=='X' && g=='X' || b=='X' && e=='X' && h=='X' || c=='X' && f=='X' && i=='X' || a=='X' && e=='X' && i=='X' || c=='X' && e=='X' && g=='X' ) { System.out.println("You Win!"); break; } else if (a!=' ' && b!=' ' && c!=' ' && d!=' ' && e!=' ' && f!=' ' && g!=' ' && h!=' ' && i!=' ') { System.out.println("It is a tie"); break; }
  10. // 6. print System.out.println("Game: "); System.out.println(" " + a +

    " | " + b + " | " + c); System.out.println("----------"); System.out.println(" " + d + " | " + e + " | " + f); System.out.println("----------"); System.out.println(" " + g + " | " + h + " | " + i);
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 TicTacToe.java

    import javax.swing.*; public class TicTacToe.java { public static void main(String [] args) { // 1. initialize do { // 2. user move // 3. continue? // 4. computer move // 5. winner or tie? // 6. print } while (true); } }
  12. CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020

    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.