{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week7Chap4-v

# Week7Chap4-v - Chapter 4 Selection and Repetition...

This preview shows pages 1–7. Sign up to view the full content.

Janaka Balasooriya CSE 110, Arizona State University Chapter 4 Selection and Repetition Applications

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely
PalindromeTester.java //******************************************************************** // PalindromeTester.java Java Foundations // // Demonstrates the use of nested while loops. //******************************************************************** import java.util.Scanner; public class PalindromeTester { //----------------------------------------------------------------- // Tests strings to see if they are palindromes. //----------------------------------------------------------------- public static void main (String[] args) { String str, another = "y"; int left, right; Scanner scan = new Scanner (System.in); while (another.equalsIgnoreCase("y")) // allows y or Y { System.out.println ("Enter a potential palindrome:"); str = scan.nextLine();

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
PalindromeTester.java left = 0; right = str.length() - 1; while (str.charAt(left) == str.charAt(right) && left < right) { left++; right--; } System.out.println(); if (left < right) System.out.println ("That string is NOT a palindrome."); else System.out.println ("That string IS a palindrome."); System.out.println(); System.out.print ("Test another palindrome (y/n)? "); another = scan.nextLine(); } } }
Chapter 4: More Practice Questions • 4.4, 4.5, 4.7, 4.8, 4.22, 4.21 • Quiz 4 solutions • In-class activity 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Janaka Balasooriya CSE 110, Arizona State University Chapter 5 Writing Classes
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

Week7Chap4-v - Chapter 4 Selection and Repetition...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online