如何将 Scanner.in 值从一个方法返回到另一个方法

我想制作一些简单的程序来计算产品的月费率。有两个输入:产品成本 - 100-10000 之间和费率数量 - 6-48 之间。我想像下面的代码那样做到这一点:


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 向我表明它不是正确的类型。有人能帮我理解问题出在哪里吗?


临摹微笑
浏览 114回答 2
2回答

收到一只叮咚

简单地做&nbsp; &nbsp; return productCost;return是关键字,而不是变量。它“返回”给定值并退出函数,以便调用该函数的实体可以执行以下操作:public static void main(String[] args) {&nbsp; &nbsp; ...&nbsp; &nbsp; double cost = calc.print();&nbsp; // note calc.print() PRODUCES a value, which we assign to `cost`&nbsp; &nbsp; ...}然后,您可以执行任何您想要的操作cost(或任何您选择的变量命名方式),包括将其传递给另一个函数。

沧海一幻觉

您的程序需要在一些地方进行更改。我已经完成了这些更改并写在更新后的程序下面:import java.util.Scanner;class Calculator {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; double productCost;&nbsp; &nbsp; int numberOfRates;&nbsp; &nbsp; double loanInterestRate;&nbsp; &nbsp; double monthlyRate;&nbsp; &nbsp; void print() {&nbsp; &nbsp; &nbsp; &nbsp; Calculator c = new Calculator();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the value of your product from 100 to 10 000 : ");&nbsp; &nbsp; &nbsp; &nbsp; productCost = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; if (productCost < 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You have to choose price between 100 to 10000. Try again: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.print();&nbsp; &nbsp; &nbsp; &nbsp; } else if (productCost > 10000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You have to choose price between 100 to 10000. Try again: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.print();&nbsp; &nbsp; &nbsp; &nbsp; } else if (productCost >= 100 || productCost <= 10000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print1(productCost);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; void print1(double productCost) {&nbsp; &nbsp; &nbsp; &nbsp; Calculator c = new Calculator();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Now enter how many rates do you want to pay from 6 to 48: ");&nbsp; &nbsp; &nbsp; &nbsp; numberOfRates = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (numberOfRates < 6) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You can't choose this number of rates. Choose between 6-48: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.print1(productCost);&nbsp; &nbsp; &nbsp; &nbsp; } else if (numberOfRates > 48) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You can't choose this number of rates. Choose between 6-48: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.print1(productCost);&nbsp; &nbsp; &nbsp; &nbsp; } else if (numberOfRates >= 6 || numberOfRates <= 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loanInterestRate = 1.025;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monthlyRate = (productCost * loanInterestRate) / numberOfRates;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);&nbsp; &nbsp; &nbsp; &nbsp; } else if (numberOfRates >= 13 || numberOfRates <= 24) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loanInterestRate = 1.05;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monthlyRate = (productCost * loanInterestRate) / numberOfRates;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);&nbsp; &nbsp; &nbsp; &nbsp; } else if (numberOfRates >= 25 || numberOfRates <= 48) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loanInterestRate = 1.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monthlyRate = (productCost * loanInterestRate) / numberOfRates;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Your monthly rate is: " + "%.2f%n", monthlyRate);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class MonthlyRate {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Calculator calc = new Calculator();&nbsp; &nbsp; &nbsp; &nbsp; calc.print();&nbsp; &nbsp; &nbsp; &nbsp; // TODO code application logic here&nbsp; &nbsp; }}将您的程序与此更新的程序进行比较后,很容易理解这些变化。尽管如此,如果您需要任何进一步的帮助,请随时告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java