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

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

如何修復(fù)計(jì)算java中子字符串的出現(xiàn)次數(shù)

如何修復(fù)計(jì)算java中子字符串的出現(xiàn)次數(shù)

藍(lán)山帝景 2023-08-04 19:01:08
我正在嘗試使用substring獲取男性和女性的數(shù)量。但在本例中,我創(chuàng)建了 2 個(gè)類,并使用掃描儀輸入我想要的記錄數(shù)。如何組合for 循環(huán)中的計(jì)數(shù)?我已經(jīng)得到了M和F的計(jì)數(shù),但它沒(méi)有得到我的預(yù)期輸出。我嘗試刪除 for 循環(huán),僅刪除 for 循環(huán),但它需要處于循環(huán)中,否則可能會(huì)出錯(cuò)。如果我再次放置 for 循環(huán),它會(huì)起作用,但它被分開(kāi)“它在輸出框上”。如果我做錯(cuò)了請(qǐng)告訴我。A[i] = Outputpublic class Input {    String info, gender;    int resultf;    int resultm;    static Scanner in = new Scanner (System.in);    public void inputted() {        System.out.print("Enter a Gender: ");        info = in.nextLine();        gender = info.substring(0,1);        }    public void Output() {        resultm = info.length() - info.replaceAll("M", "").length();        resultf = info.length() - info.replaceAll("F", "").length();        System.out.println("Male: "+resultm);        System.out.println("Female: "+resultf);        }}public class Output {    static Input A[] = new Input [100];    static Scanner in = new Scanner (System.in);    public static void main (String args []) {        int i, size;        //Input        System.out.print("Enter how many record: ");        size = in.nextInt();        for (i = 0; i < size; i++) {            A[i] = new Input();            A[i].inputted();        }           System.out.println();        //Print        for (i = 0; i < size; i++) {            System.out.println("Gender: "+A[i].gender);        }        System.out.println();        //Output Section        for (i = 0; i < size; i++) {            A[i].Output();        }    }}輸出:Enter how many record: 2Enter a Gender: MEnter a Gender: FGender: MGender: FMale: 1Female: 0Male: 0Female: 1預(yù)期輸出:Enter how many record: 2Enter a Gender: MEnter a Gender: FGender: MGender: FMale: 1Female: 1
查看完整描述

2 回答

?
海綿寶寶撒

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

您必須在主函數(shù)中對(duì)其進(jìn)行計(jì)數(shù),而不是在輸入類中進(jìn)行計(jì)數(shù)。


public class Input {

  //add functions below

  public boolean IsMale(){return resultm == 1;}

  public boolean IsFemale(){return resultf == 1;}


  //another way without boolean

  public int GetMaleCount(){return resultm;}

  public int GetFemaleCount(){return resultf;}

}

將 main 中的輸出部分更改為以下內(nèi)容:


int male = 0;

int female = 0;

for (i = 0; i < size; i++) {

  //if(A[i].IsMale()){male ++;}

  //if(A[i].IsFemale()){female ++;}

  //another way without boolean

  male += A[i].GetMaleCount();

  female += A[i].GetFemaleCount();

}

System.out.println("Male: "+male);

System.out.println("Female: "+female);


查看完整回答
反對(duì) 回復(fù) 2023-08-04
?
哈士奇WWW

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

在最后一個(gè)循環(huán)中,mainInput.Output根據(jù)數(shù)組的大小調(diào)用了基數(shù),并在該方法上打印了性別。更改Input.Output打印數(shù)據(jù)的方式。嘗試添加檢查if以檢查您的打印是男性還是女性?;蛘咧皇歉淖兇蛴〗Y(jié)果的邏輯。



查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 2 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報(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)