java - 如何添加返回结果的方法?
问题描述
我一直在研究这些程序。他们没有任何错误,但我需要让他们返回结果才能让他们正常工作。更具体地说,添加一个返回结果的方法。
指令如下: 编写一个程序,该程序被拆分为至少一个返回结果的方法
这是第一个程序:
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
}
解决方案
所以Java的工作方式是编译器只会从“main”方法中读取命令。所以在计算器的情况下,Java 会看到你想要运行计算器方法,它的返回类型是“void” 它变成 PUBLIC(意味着其他类可以看到并与之交互) STATIC(基本上意味着该方法属于类本身,而不是类的实例)VOID(这是你的返回类型,意思是在方法完成后,什么被放回main)所以如果你想让一个方法返回一些东西,你需要改变返回类型。在您的计算器项目的情况下,这样的事情会将其拆分为 2 种方法,其中一种方法会返回一些内容:
公共类在线计算器{
public static void main(String[] args) {
Calculator();
} //main方法结束
// 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
}
似乎他们希望您添加更多代码,但他们的想法是他们希望您知道如何使用协同工作的方法最终做出一些事情!
与另一个使用相同的想法!
推荐阅读
- outlook-redemption - 无法从 RDOFolder 获取 FolderPath
- javascript - 使我的 Toast 组件可用于子组件
- swift - 在 ARKit 中渲染新图像时如何清除内存?
- sql-server - 我想在使用 smslib 时将数据从附加日志插入到 sql 表中
- oracle - Oracle左连接给出重复
- node.js - 未连接到运行时
- android - ConstraintLayout 的求解器无法正确测量包含
- velo - wixLocation.to() - 如何使用 wixLoation.to() 转移到动态页面
- javascript - 如何在 Angular 的“then”链中返回?
- inheritance - 运行程序时不显示 JavaFX Circle() 形状