如何添加返回结果的方法



我一直在研究这些程序。他们没有任何错误,但我需要让他们返回一个结果,以便他们正常工作。更具体地说,添加一个返回结果的方法。

说明如下:编写一个程序,将其拆分为至少一个返回结果的方法

这是第一个程序:

import java.util.Scanner; // Needed to make Scanner available
public class onlineCalculator {


public static void main(String[] args) {
Calculator();
} //END of main method

// Inserting your loan at the start of the year and the amount paid off
// and calculates the amount yet to pay with interest
//
public static void Calculator(){
int a;
int b;
Scanner scanner = new Scanner(System.in);

System.out.print("Amount of loan at start of year? ");
a = scanner.nextInt();
System.out.print("Amount paid off this year? ");
b = scanner.nextInt();
int c;
c= a - b;
double d;
final double e;
d = c * 1.07 * 10.0; 
e = (int)d / 10.0; 

System.out.println("The new amount owed is (in pounds): " + e);

} //END of Calculator
}

这是第二个程序:

import java.util.Scanner; // Needed to make Scanner available
public class BodyAge {
public static void main(String[] args) {
CalculateAge();

} // END of main method
// Inserting age and heart rate and stretch distance 
//and calculates the body age based on conditions
public static void CalculateAge() {

int age;
int heartRate;
int stretch;
Scanner input = new Scanner(System.in);
System.out.print("What is your age? "); 
age = input.nextInt();

System.out.print("What is your heart rate? "); 
heartRate = input.nextInt();
if (heartRate <= 62) {
age -= 5;          // block of code to be executed if condition1 is true
} else if (62 <= heartRate && heartRate <= 64) {
age--;            // block of code to be executed if the condition1 is false and condition2 is
// true
} else if (65 <= heartRate && heartRate <= 70) {
age++;           // block of code to be executed if the condition1 and condition2 are false and 
// condition3 is true
} else {
age += 2;       // block of code to be executed if the condition1 and condition2 and condition3
// are false and condition4 is true
}
System.out.print("How far can you stretch? "); 
stretch = input.nextInt();

if (stretch <= 20) {
age += 4;      // block of code to be executed if condition1 is true
} else if (20 <= stretch && stretch <= 32) {
age++;         // block of code to be executed if the condition1 is false and condition2 is
// true
} else if (33 <= stretch && stretch <= 37) {
age = age + 0; // block of code to be executed if the condition1 and condition2 are false and
// condition3 is true
} else {
age = age + 3; // block of code to be executed if the condition1 and condition2 and condition3
// are false and condition4 is true
}
System.out.println("Your body's age is " + age);
}  //END of CalculateAge
}

这里有一个示例,说明了您可以考虑将类分解为使用一些带有返回的方法的一种可能方法。让我们以你的第一堂课为例。您经常接受用户的输入。

Scanner scanner = new Scanner(System.in);
System.out.print("Amount of loan at start of year? ");
a = scanner.nextInt();
System.out.print("Amount paid off this year? ");
b = scanner.nextInt();

这可能会分解为另一种用于精简可重用性的方法。

public static int askForInt(Scanner scanner, String message) {
System.out.print(message);
return scanner.nextInt();
}

从那里,您可以用此方法替换对信息的调用。完整示例:

import java.util.Scanner; // Needed to make Scanner available
public class OnlineCalculator {
public static void main(String[] args) {
calculator();
} //END of main method
// Inserting your loan at the start of the year and the amount paid off
// and calculates the amount yet to pay with interest
//
public static void calculator(){
int a;
int b;
Scanner scanner = new Scanner(System.in);

a = askForInt(scanner, "Amount of loan at start of year? ");
b = askForInt(scanner, "Amount paid off this year? ");
scanner.close();
int c;
c= a - b;
double d;
final double e;
d = c * 1.07 * 10.0; 
e = (int)d / 10.0; 

System.out.println("The new amount owed is (in pounds): " + e);

} //END of Calculator
public static int askForInt(Scanner scanner, String message) {
System.out.print(message);
return scanner.nextInt();
}
}

第一个程序,例如,您可以将calculate((方法拆分为2方法,并将int变量"移动到2;a";以及";b";在main((方法中

第一种方法:

void getInput(){
...
}

第二种方法:

int calculateAndreturnResult(int a, int b) {
...
}

最后在main((中使用这些方法并打印结果:

getInput();
int result = calculateAndreturnResult(){
}
system.out.println(result);

所以Java的工作方式是编译器只从;主";方法因此,在计算器的情况下,Java将看到您想要运行计算器方法,该方法的返回类型为";"无效";它变为PUBLIC(意味着其他类可以看到它并与它交互(STATIC(基本上意味着该方法属于类本身,而不是类的实例(VOID(这是您的返回类型,意味着在方法完成后,将什么放回main中(,所以如果您想让一个方法返回一些东西,您需要更改返回类型。在你的计算器项目的情况下,这样的东西会把它分成两个方法,其中一个返回一些东西:

公共类OnlineCalculator{

public static void main(String[] args) {
Calculator();

}//主方法结束

// Inserting your loan at the start of the year and the amount paid off
// and calculates the amount yet to pay with interest
//
//this will return an int type
public static int loanDifference(int amountOwed, int amountPaid) {
int c = amountOwed - amountPaid;
return c;
}

//这将返回一个双重类型public static double newAmountOwed(double d({

double e = (int)d / 10.0; 
return e;
}
public static void Calculator(){
int a;
int b;
Scanner scanner = new Scanner(System.in);

System.out.print("Amount of loan at start of year? ");
a = scanner.nextInt();
System.out.print("Amount paid off this year? ");
b = scanner.nextInt();
scanner.close();
int c = loanDifference (a, b);

double d;
d = c * 1.07 * 10.0; 
final double e = newAmountOwed(d);


System.out.println("The new amount owed is (in pounds): " + e);

} //END of Calculator

}

他们似乎希望你放入更多的代码,但他们的想法是希望你知道如何使用协同工作的方法来最终做出一些东西!

用同样的想法和另一个!

最新更新