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

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

使用依賴于其他方法的方法

使用依賴于其他方法的方法

精慕HU 2022-01-06 17:14:40
我正在嘗試制作一個(gè)程序,告訴您進(jìn)行零錢所需的最少硬幣數(shù)量,并且它必須是模塊化的。目前它要求輸入三次并打印所有三個(gè)輸入的總和所需的硬幣數(shù)量。我相信這是因?yàn)?maxDimes 依賴于依賴于輸入的 maxQuarters。有什么辦法可以讓這些方法只運(yùn)行一次?我是編程和這個(gè)網(wǎng)站的新手,所以如果這個(gè)問(wèn)題不清楚或已經(jīng)被問(wèn)到,我深表歉意。     public static int input(){    System.out.println("Please enter amount of change under 100 cents: ");    Scanner input = new Scanner(System.in);    int change = input.nextInt();    if (change%5<2)    {        change = change+(change%5);    }    else if(change%5>2)    {        change = change-(change%5);    }    return change;}public static int[] maxQuarters(){    int change = input();    int quarters = change/25;    int leftover = change-quarters;    return new int[] {quarters, leftover};}public static int[] maxDimes(){    int first[] = maxQuarters();    int change = first[1];    int dimes = change/10;    int leftover = change-dimes;    return new int[] {dimes, leftover};  }public static int maxNickels(){    int second[] = maxDimes();    int change = second[1];    int nickels = change/5;    return nickels;}public static void makeChange(){    int first[] = maxQuarters();    int quarters = first[0];    int second[] = maxDimes();    int dimes = second[0];    int nickels = maxNickels();    System.out.println("The minimum number of coins you could use is: "+quarters+"quarters, "+dimes+" dimes and "+nickels+" nickels");}public static void main(String[] args) {    makeChange();}
查看完整描述

1 回答

?
HUX布斯

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

您被要求輸入三遍,因?yàn)槟鷌nput()在maxQuarters(). 在maxDimes()和maxNickels()你叫maxQuarters()等也input()叫。


解決此問(wèn)題的一種可能方法是調(diào)用input()您的main-method 并將輸入作為參數(shù)傳遞給您的其他方法,如下所示:


public static int input() {

    System.out.println("Please enter amount of change under 100 cents: ");

    Scanner input = new Scanner(System.in);

    int change = input.nextInt();

    if (change % 5 < 2) {

        change = change + (change % 5);

    } else if (change % 5 > 2) {

        change = change - (change % 5);

    }

    return change;


}


public static int[] maxQuarters(int input) {


    int quarters = input / 25;

    int leftover = input - 25*quarters;

    return new int[]{quarters, leftover};

}


public static int[] maxDimes(int input) {

    int first[] = maxQuarters(input);

    int change = first[1];

    int dimes = change / 10;

    int leftover = change - 10*dimes;

    return new int[]{dimes, leftover};

}


public static int maxNickels(int input) {

    int second[] = maxDimes(input);

    int change = second[1];

    int nickels = change / 5;

    return nickels;

}


public static void makeChange(int input) {

    int first[] = maxQuarters(input);

    int quarters = first[0];

    int second[] = maxDimes(input);

    int dimes = second[0];

    int nickels = maxNickels(input);

    System.out.println("The minimum number of coins you could use is: " + quarters + " quarters, " + dimes + " dimes and " + nickels + " nickels");


}


public static void main(String[] args) {

    int input = input();

    makeChange(input);

}


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

添加回答

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