Java 如何递归调用方法?
我试图递归调用一个方法,直到获得所需的输出。但是,我想从另一个类调用一个方法,并从该方法获取信息,以便在递归方法中使用它。例如,假设我有一个名为Java 如何递归调用方法?,java,inheritance,recursion,methods,Java,Inheritance,Recursion,Methods,我试图递归调用一个方法,直到获得所需的输出。但是,我想从另一个类调用一个方法,并从该方法获取信息,以便在递归方法中使用它。例如,假设我有一个名为Cake的父类,其中包含有关蛋糕的信息,例如其面糊(即面糊的数量),一个扩展类,具有特定类型的蛋糕,其中包含面糊的唯一实例,我还有另一个课程叫面包房,我想在那里制作正在定购的蛋糕。我在面包房里有一个叫做createCake的方法,我想递归调用这个方法,直到锅里有足够的面糊来制作蛋糕。如果面糊量是在扩展类中随机生成的,我如何从该类调用getBatter方法
Cake
的父类,其中包含有关蛋糕的信息,例如其面糊(即面糊的数量),一个扩展类,具有特定类型的蛋糕,其中包含面糊的唯一实例,我还有另一个课程叫面包房,我想在那里制作正在定购的蛋糕。我在面包房里有一个叫做createCake
的方法,我想递归调用这个方法,直到锅里有足够的面糊来制作蛋糕。如果面糊量是在扩展类中随机生成的,我如何从该类调用getBatter
方法并捕获关于面糊量的信息,以便在创建蛋糕的递归方法中使用它?有人能帮我解决这个问题吗?我正在做一些类似的事情,但我不太明白如何才能真正获取信息,从而使递归工作。我下面有一个代码示例,这样您就可以了解我正在尝试做什么(我知道这不是很准确)。任何帮助都将不胜感激
import java.util.Random;
public abstract class Cake
{
static Random gen = new Random(System.currentTimeMillis());
public int type; //type of cake
public static int batter = gen.nextInt() * 3; //random amount of batter
public int getType()
{
return type;
}
public int getBatter()
{
return batter;
}
}
public class RedVelvet extends Cake
{
public int type;
public int batter = gen.nextInt(3)+6; //generates between 6-8 cups of batter inclusive
public int getType()
{
return 1;
}
public int getBatter()
{
return batter;
}
}
public class Chocolate extends Cake
{
public int type;
public int batter = gen.nextInt(3)+6; //generates between 6-8 cups of batter inclusive
public int getType()
{
return 2;
}
public int getBatter()
{
return batter;
}
}
public class Pound extends Cake
{
public int type;
public int batter = gen.nextInt(3)+6;
public int getType()
{
return 3;
}
public int getBatter()
{
return batter;
}
}
public class Bakery
{
import java.util.Scanner;
System.out.print("Enter desired size of cake to be baked (Must be at least 12):");
desiredSize=scan.nextInt();
public static void createCake(int desiredSize, int currentSize) //currentSize is the current amount of batter in the pan
{
if (currentSize == desiredSize)
return;
else if (currentSize < desiredSize)
{
//Recursively call createCake method so that batter continues to be added to the pan until there is enough to make the desired cake size. I want to get the batter information from one of the extended classes in order to add it to the cake.
}
}
import java.util.Random;
公共抽象类蛋糕
{
静态随机生成=新随机(System.currentTimeMillis());
public int type;//蛋糕的类型
public static int batter=gen.nextInt()*3;//电池的随机数量
公共int getType()
{
返回类型;
}
公共int getBatter()
{
回击机;
}
}
公共级红丝绒蛋糕
{
公共int类型;
public int batter=gen.nextInt(3)+6;//生成6-8杯面糊
公共int getType()
{
返回1;
}
公共int getBatter()
{
回击机;
}
}
公共级巧克力蛋糕
{
公共int类型;
public int batter=gen.nextInt(3)+6;//生成6-8杯面糊
公共int getType()
{
返回2;
}
公共int getBatter()
{
回击机;
}
}
公共类磅蛋糕
{
公共int类型;
公共int batter=gen.nextInt(3)+6;
公共int getType()
{
返回3;
}
公共int getBatter()
{
回击机;
}
}
公营面包店
{
导入java.util.Scanner;
系统输出打印(“输入需要烘焙的蛋糕尺寸(必须至少为12):”;
desiredSize=scan.nextInt();
public static void createCake(int desiredSize,int currentSize)//currentSize是锅中当前的面糊量
{
如果(currentSize==desiredSize)
回来
else if(currentSize
这是学校的课程还是某种课程,因为我个人不会走这条路,但这是我的观点。这就像,我对烘焙了解多少,我可以安全地告诉你……绝对没有什么。有些人甚至会说我的编程/编码技能,但话说回来,我不是程序员,我是在almos自学的t所有编程环境,包括良好的旧程序集,其中大部分我现在已经忘记了:/
我应该认为,当涉及到烘焙蛋糕(或大多数事情)为了避免浪费,必须建立某种精度。我们都知道浪费是要花钱的。我并不太相信随机生成的蛋糕面糊对于你最有可能尝试实现的目标来说足够精确,但话说回来,你已经知道这一点。我注意到,在你的不同蛋糕类别中,他们都称之为y生成一个从6到8的随机数。如果它们都做相同的事情,那么为什么有它们
我认为您根本不需要递归,而是需要从循环中调用一个简单的方法,例如:
while (currentSize < desiredSize) {
currentSize+= getBatter(cake, desiredSize);
}
while(currentSize
以下是我将如何做到这一点,如果你发现这一切都毫无意义,我现在向你道歉:
import java.util.Random;
import java.util.Scanner;
public class Bakery {
public static void main(String[] args) {
getBaking(); // :)
}
private static void getBaking(){
Scanner scan = new Scanner(System.in);
String cakeType = "";
while (cakeType.equals("")) {
System.out.print("Enter desired cake to be baked (Pound, Chocolate, "
+ "RedVelvet) or\nenter 'exit' to quit: -> ");
cakeType = scan.nextLine();
if (cakeType.isEmpty()) {
System.out.println("\nYou must supply the name of cake to bake or "
+ "enter 'exit' to quit!\n");
}
else if (cakeType.toLowerCase().equals("exit")) { System.exit(0); }
else if (!cakeType.toLowerCase().matches("(pound|chocolate|redvelvet)")) {
System.out.println("\nYou must supply the name of cake as shown above!\n");
cakeType = "";
}
}
int desiredSize = 0;
String size = "";
while (size.equals("")) {
System.out.print("\nEnter desired size of cake to be baked (must be at "
+ "least 12\"): -> ");
size = scan.nextLine();
if (size.isEmpty()) {
System.out.println("\nYou must supply the size of cake to bake or "
+ "enter 'exit' to quit!\n");
}
else if (size.toLowerCase().equals("exit")) { System.exit(0); }
else if (Integer.valueOf(size.replace("\"","")) < 12 ) {
System.out.println("\nYou must supply a cake size of 12\" or greater!\n");
size = "";
}
}
desiredSize = Integer.valueOf(size.replace("\"",""));
createCake(cakeType, desiredSize, 0);
}
public static void createCake(String cake, int desiredSize, int currentSize) {
//currentSize is the current amount of batter in the pan
while (currentSize < desiredSize) {
currentSize+= getBatter(cake, desiredSize);
System.out.println(currentSize);
}
System.exit(0); // Done! - Quit!
}
public static int getBatter(String cake, int desiredSize) {
Random gen = new Random(System.currentTimeMillis());
// Since the random generation for the batter amount is the
// same for all cake types according to your code we just
// need this:
int batterAmount = gen.nextInt(3)+6;
// But if we want to be more specific for each Cake Type
// then we can do it this way but first create the required
// batter equations for each cake type and remove the /* and
// */ from the code but first comment out (//) the batterAmount
// variable declaration above.
// NOTE: Both cake diameter AND 'Height' should play into the factor
// of how much batter is required unless of course all cakes made are
// of the same height.
/*
int batterAmount = 0;
switch (cake.toLowerCase()) {
case "pound":
batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12));
break;
case "chocolate":
batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12));
break;
case "redvelvet":
batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12));
break;
} */
return batterAmount;
}
}
import java.util.Random;
导入java.util.Scanner;
公营面包店{
公共静态void main(字符串[]args){
getBaking();/:)
}
私有静态void getBaking(){
扫描仪扫描=新扫描仪(System.in);
字符串cakeType=“”;
while(cakeType.equals(“”){
系统输出打印(“输入想要烘焙的蛋糕(磅,巧克力,”
+“RedVelvet)或\n输入“退出”退出:->”;
cakeType=scan.nextLine();
if(cakeType.isEmpty()){
System.out.println(“\n您必须提供要烘焙的蛋糕的名称或”
+“输入‘退出’退出!\n”);
}
else if(cakeType.toLowerCase().equals(“exit”){System.exit(0);}
如果(!cakeType.toLowerCase().matches(((pound | chocolate | redvelvet))匹配,则为else{
System.out.println(“\n您必须提供如上所示的蛋糕名称!\n”);
cakeType=“”;
}
}
int desiredSize=0;
字符串大小=”;
while(size.equals(“”){
System.out.print(“\n输入要烘焙的蛋糕的所需尺寸(必须为”
+“至少12\”:->“;
size=scan.nextLine();
if(size.isEmpty()){
System.out.println(“\n您必须提供要烘焙的蛋糕大小或”
+“输入‘退出’退出!\n”);
}
else if(size.toLowerCase().equals(“exit”){System.exit(0);}
else if(Integer.valueOf(size.replace(“\”,”)<12){
System.out.println(“\n您必须提供蛋糕大小为