Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package class170629;
- import java.util.Scanner;
- public class arrays09 {
- public static void main(String[] args) {
- /*
- * Exercise 9, slide 29
- * input : 5 chars to an array
- * output: are the values in the array is the same character
- */
- final int ARRAY_SIZE = 5;
- // create a scanner
- Scanner s = new Scanner(System.in);
- // define an integer array
- char[] chars = new char[ARRAY_SIZE];
- System.out.printf("Enter %d chars:\n", chars.length);
- // get data from user and save it in the array
- for (int i = 0; i < chars.length; i += 1) {
- chars[i] = s.next().charAt(0);
- }
- s.close();
- // scan the array
- int i;
- for (i = 1; i < chars.length && chars[i] == chars[0]; i += 1) {
- }
- // i==chars.length --> all charactars are the same
- System.out.printf("All characters are %sthe same\n",
- i == chars.length ? "" : "not ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement