第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從字符串中刪除字符串重復(fù)出現(xiàn)字符中的字符串偶數(shù)對(duì)字符 - java

從字符串中刪除字符串重復(fù)出現(xiàn)字符中的字符串偶數(shù)對(duì)字符 - java

慕桂英546537 2021-10-28 16:58:36
我是java初學(xué)者,我有這個(gè)問題:**Q01 [ 7 分] 編寫一個(gè)java 程序,輸入字符串,使用EvenPairs(str) 方法檢查每個(gè)字符(即字母表)是否存在偶數(shù)對(duì)。示例測(cè)試用例輸入:“3gy41d21y363”輸出:3 – 錯(cuò)誤g – 假y – 真4 – 假1 – 真d – 假正如您在輸出中看到的,即使重復(fù)出現(xiàn)的每個(gè)字符也只打印一次,我解決了這個(gè)問題,直到這一步我找不到只打印字符 1 次的解決方案,結(jié)果是真還是假這是我的代碼:    package evenpairornot;import java.util.Scanner;public class EvenPairOrNot {static Scanner input = new Scanner(System.in);public static void main(String[] args) {    System.out.print("Enter a string: ");    String s1=input.nextLine();    EvenPairs(s1);}public static void EvenPairs(String s){    char [] chs=s.toCharArray();    int count=0;    for (int i = 0; i <chs.length; i++) {        for (int j = 0; j <chs.length; j++) {            if (chs[i]==chs[j]){                count++;             }            }         if(count%2==0)            System.out.println(s.charAt(i)+"- true");            else            System.out.println(s.charAt(i)+"- False");        count=0;     }}}這是輸出:輸入一個(gè)字符串:3gy41d21y3633- 錯(cuò)誤g- 錯(cuò)誤y-真4- 錯(cuò)誤1-真d-錯(cuò)誤2- 錯(cuò)誤1-真y-真3- 錯(cuò)誤6- 錯(cuò)誤3- 錯(cuò)誤等待你的幫助?。≈x謝你
查看完整描述

1 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是代碼?;旧显谒凶址麛?shù)完之后。在打印之前,我向后看以確保它不是重復(fù)的。這里有很大的優(yōu)化空間,比如在計(jì)數(shù)之前進(jìn)行檢查,或者可以只計(jì)算 i 之后的字符而不是計(jì)算所有字符。


public static void EvenPairs(String s) {


    char[] chs = s.toCharArray();

    int count = 0;


    for (int i = 0; i < chs.length; i++) {

        for (int j = 0; j < chs.length; j++) {

            if (chs[i] == chs[j]) {

                count++;

            }

        }


        boolean shouldPrint = true;  

        for (int k = i - 1; k >= 0; k--) {  //loop though each character before the current one to check if it was already printed. 

            if (chs[k] == chs[i]) {         //if we it was already printed don't print.

                shouldPrint = false;

                break;

            }

        }


        if (shouldPrint) {

            if (count % 2 == 0)

                System.out.println(s.charAt(i) + "- true");

            else

                System.out.println(s.charAt(i) + "- False");

        }


        count = 0;

    }


}


查看完整回答
反對(duì) 回復(fù) 2021-10-28
  • 1 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)