1. 程式人生 > >java語言在某個數組中查找某個字符出現的次數

java語言在某個數組中查找某個字符出現的次數

com clas else 技術分享 span java語言 false sca util

package com.llh.demo;

import java.util.Scanner;

/**
 * 
 * @author llh
 *
 */
public class Test {
    /*
     * 在某個字符數組中查找某個字符出現的次數
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入一個字符:");
        char a = sc.next().charAt(0);
        
int count = 0; boolean b = true; char[] letter = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘c‘ }; for (int i = 0; i < letter.length; i++) { if (a == letter[i]) { count++; b = false; } } if (b == false) { System.out.println(
"這個字符在字符數組中出現過" + count + "次!"); } else { System.out.println("這個字符不在字符數組中!"); } } }

技術分享

java語言在某個數組中查找某個字符出現的次數