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

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

如何將 Scanner.in 值從一個(gè)方法返回到另一個(gè)方法

如何將 Scanner.in 值從一個(gè)方法返回到另一個(gè)方法

臨摹微笑 2023-11-01 20:56:49
我想制作一些簡(jiǎn)單的程序來計(jì)算產(chǎn)品的月費(fèi)率。有兩個(gè)輸入:產(chǎn)品成本 - 100-10000 之間和費(fèi)率數(shù)量 - 6-48 之間。我想像下面的代碼那樣做到這一點(diǎn):import java.util.Scanner;public class Calculator {Scanner sc = new Scanner (System.in);double productCost;int numberOfRates;double loanInterestRate;double monthlyRate;Double print () {Calculator c = new Calculator();System.out.println ("Enter the value of your product from 100 to 10 000 : ");productCost=sc.nextDouble();if (productCost < 100){    System.out.println ("You have to choose price between 100 to 10000. Try again: ");    c.print();} else if (productCost >10000){    System.out.println ("You have to choose price between 100 to 10000. Try again: ");    c.print();} else if (productCost >= 100 || productCost <=10000){    c.print1();    return = productCost;   // how to return productCost to be used in next method print1()?}else return null;   }void print1(){Calculator c = new Calculator(); System.out.println ("Now enter how many rates do you want to pay from 6 to 48: ");numberOfRates=sc.nextInt();if (numberOfRates<6){    System.out.println ("You can't choose this number of rates. Choose between 6-48: ");    c.print1();} else if (numberOfRates>48){    System.out.println ("You can't choose this number of rates. Choose between 6-48: ");    c.print1();} else if (numberOfRates>=6 || numberOfRates<=12) {    loanInterestRate=1.025;    monthlyRate = (productCost*loanInterestRate)/numberOfRates;    System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);} else if (numberOfRates>=13 || numberOfRates <=24 ) {    loanInterestRate=1.05;    monthlyRate = (productCost*loanInterestRate)/numberOfRates;    System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);} else if (numberOfRates >=25|| numberOfRates<=48){    loanInterestRate=1.1;    monthlyRate = (productCost*loanInterestRate)/numberOfRates;    System.out.printf("Your monthly rate is: "+ "%.2f%n",monthlyRate);}}}問題是什么,我不知道如何從方法“print()”返回“雙倍productCost”。ProductCost 是從輸入中獲取的,這是雙倍的,但 NetBeans 向我表明它不是正確的類型。有人能幫我理解問題出在哪里嗎?
查看完整描述

2 回答

?
收到一只叮咚

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

簡(jiǎn)單地做


    return productCost;

return是關(guān)鍵字,而不是變量。它“返回”給定值并退出函數(shù),以便調(diào)用該函數(shù)的實(shí)體可以執(zhí)行以下操作:


public static void main(String[] args) {

    ...

    double cost = calc.print();  // note calc.print() PRODUCES a value, which we assign to `cost`

    ...

}

然后,您可以執(zhí)行任何您想要的操作cost(或任何您選擇的變量命名方式),包括將其傳遞給另一個(gè)函數(shù)。


查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
滄海一幻覺

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

您的程序需要在一些地方進(jìn)行更改。我已經(jīng)完成了這些更改并寫在更新后的程序下面:


import java.util.Scanner;


class Calculator {

    Scanner sc = new Scanner(System.in);

    double productCost;

    int numberOfRates;

    double loanInterestRate;

    double monthlyRate;


    void print() {

        Calculator c = new Calculator();

        System.out.println("Enter the value of your product from 100 to 10 000 : ");

        productCost = sc.nextDouble();

        if (productCost < 100) {

            System.out.println("You have to choose price between 100 to 10000. Try again: ");

            c.print();

        } else if (productCost > 10000) {

            System.out.println("You have to choose price between 100 to 10000. Try again: ");

            c.print();

        } else if (productCost >= 100 || productCost <= 10000) {

            print1(productCost);            

        }

    }


    void print1(double productCost) {

        Calculator c = new Calculator();

        System.out.println("Now enter how many rates do you want to pay from 6 to 48: ");

        numberOfRates = sc.nextInt();

        if (numberOfRates < 6) {

            System.out.println("You can't choose this number of rates. Choose between 6-48: ");

            c.print1(productCost);

        } else if (numberOfRates > 48) {

            System.out.println("You can't choose this number of rates. Choose between 6-48: ");

            c.print1(productCost);

        } else if (numberOfRates >= 6 || numberOfRates <= 12) {

            loanInterestRate = 1.025;

            monthlyRate = (productCost * loanInterestRate) / numberOfRates;

            System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);

        } else if (numberOfRates >= 13 || numberOfRates <= 24) {

            loanInterestRate = 1.05;

            monthlyRate = (productCost * loanInterestRate) / numberOfRates;

            System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);

        } else if (numberOfRates >= 25 || numberOfRates <= 48) {

            loanInterestRate = 1.1;

            monthlyRate = (productCost * loanInterestRate) / numberOfRates;

            System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);

        }

    }

}


public class MonthlyRate {

    public static void main(String[] args) {

        Calculator calc = new Calculator();

        calc.print();

        // TODO code application logic here

    }


}

將您的程序與此更新的程序進(jìn)行比較后,很容易理解這些變化。盡管如此,如果您需要任何進(jìn)一步的幫助,請(qǐng)隨時(shí)告訴我。


查看完整回答
反對(duì) 回復(fù) 2023-11-01
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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