处理返回和接收方法的java程序有问题。我不知道如何调用我的方法。
Scanner变量是唯一的全局变量。可变大小将是2种方法的本地。
setOcean():此方法提示输入海洋并返回从键盘捕获的内容。
setSize():此方法接收海洋的名称,提示输入描述性的大小 海洋并返回捕获的内容。将显示描述性大小列表(请参阅示例输出)。
用户从列表中进行选择。根据选择,使用开关确定
描述性大小:最大,第二大,第三大,第四大,最小或显示
错误消息:再试一次!重新输入选择。如果选择,请使用do / while重新提示
任何小于1或大于5的东西。将有2个变量:一个用于存储选择
列表和另一个将大小存储为“最大”或“第二大”等。交换机使用
第一个变量,用于确定第二个变量中的值或打印错误消息。
示例输出
五大世界
输入海洋:南方
从上面的列表中,选择南大洋的大小:4
以千米为单位输入南大洋的总面积:20.327
世界海洋
海洋:南方 规模:第四大 总平方英里:12.623进入另一个海洋? ÿ
进入海洋:北极
从上面的列表中,选择北冰洋的大小:6
再试一次!重新输入选择。
从上面的列表中,选择北冰洋的大小:0
再试一次!重新输入选择。
从上面的列表中,选择北冰洋的大小:5
以千米为单位输入北冰洋的总面积:14.056
世界海洋
海洋:北极 尺寸:最小 总平方英里:872.9万
进入另一个海洋? Ñ
这是我到目前为止所拥有的。
import java.util.Scanner;
public class OceanMLE52
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{//BEGIN main()
char answer = 'Y';
do
{
System.out.printf("\n\nThe 5 World Oceans");
setOcean();
setSize(ocean);
setKilometers(ocean);
calcTotalAreaMiles(oceanKilo, conversion);
printOcean(ocean, oceanSizeName, totalSqMiles);
input.nextLine();
System.out.printf("%nEnter another ocean? ");
answer = input.nextLine().charAt(0);
} while (Character.toUpperCase(answer)=='Y');
System.exit(0);
} //END OF MAIN()
public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
{
String ocean = "";
System.out.printf("%nEnter an ocean :");
ocean = input.nextLine();
return ocean;
}
public static void setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
//accessible to the other methods. The user selects from the list.
//Based on the selection a switch is used to determine the descriptive
//Error message is displayed if one
{
int selection = 0;
do
{
System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
+ "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean: ", ocean);
selection = input.nextInt();
String oceanSizeName = "";
switch(selection)
{
case 1: oceanSizeName = "Largest";
break;
case 2: oceanSizeName = "Second Largest";
break;
case 3: oceanSizeName = "Third Largest";
break;
case 4: oceanSizeName = "Fourth Largest";
break;
case 5: oceanSizeName = "Smallest";
break;
default: System.out.printf("%nTry Again! Re-enter selection. %n");
}
} while (selection < 1 || selection > 5);
}
public static double setKilometers(String ocean)
{
double oceanKilo = 0;
System.out.printf("%nEnter total area for the %s Ocean in kilometers: ", ocean);
oceanKilo = input.nextDouble();
return oceanKilo;
}
public static double calcTotalAreaMiles(double oceanKilo, double conversion )
{
double totalSqMiles = 0;
totalSqMiles = oceanKilo * conversion;
return totalSqMiles;
}
public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
{
System.out.printf("\n\nOCEANS OF THE WORLD" +
"\n\nOcean: %s" +
"\n\nSize: %s" +
"%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);
}
}
我不知道如何调用不同的方法。
请帮忙!
答案 0 :(得分:0)
我看到的主要问题是您从方法中返回内容但未将其分配给要传递到其他方法的任何变量。例如,你的setOcean方法返回一个字符串,然后你将一个名为ocean的变量传递给setSize,但是在你的main方法中没有名为ocean的局部变量。您应该将方法分配给名为ocean的字符串变量,即String ocean = setOcean();
任何其他返回需要传递给另一个方法的方法的方法都应该被调用并进行类似的方式。
答案 1 :(得分:0)
您需要正确使用变量才能使其正常工作。 我还使用了Tom关于组合input.nextInt()和input.nextLine()的内容,然后你就去了:
import java.util.Scanner;
公共类OceanMLE52 {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{//BEGIN main()
char answer = 'Y';
String ocean = "";
String oceanSizeName = "";
double oceanKilo = 0;
double conversion = .38610; // convert km^2 to mi^2
double totalSqMiles = 0;
do
{
System.out.printf("\n\nThe 5 World Oceans");
ocean = setOcean();
oceanSizeName = setSize(ocean);
oceanKilo = setKilometers(ocean);
totalSqMiles = calcTotalAreaMiles(oceanKilo, conversion);
printOcean(ocean, oceanSizeName, totalSqMiles);
input.nextLine();
System.out.printf("%nEnter another ocean? ");
answer = input.nextLine().charAt(0);
} while (Character.toUpperCase(answer)=='Y');
System.exit(0);
} //END OF MAIN()
public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
{
String ocean = "";
System.out.printf("%nEnter an ocean :");
ocean = input.nextLine();
return ocean;
}
public static String setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
//accessible to the other methods. The user selects from the list.
//Based on the selection a switch is used to determine the descriptive
//Error message is displayed if one
{
int selection = 0;
String oceanSizeName = "";
do
{
System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
+ "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean: ", ocean);
selection = input.nextInt();
switch(selection)
{
case 1: oceanSizeName = "Largest";
break;
case 2: oceanSizeName = "Second Largest";
break;
case 3: oceanSizeName = "Third Largest";
break;
case 4: oceanSizeName = "Fourth Largest";
break;
case 5: oceanSizeName = "Smallest";
break;
default: System.out.printf("%nTry Again! Re-enter selection. %n");
}
} while (selection < 1 || selection > 5);
input.nextLine(); // consume the newline
return oceanSizeName;
}
public static double setKilometers(String ocean)
{
double oceanKilo = 0;
System.out.printf("%nEnter total area for the %s Ocean in kilometers: ", ocean);
oceanKilo = input.nextDouble();
input.nextLine(); // consume the newline
return oceanKilo;
}
public static double calcTotalAreaMiles(double oceanKilo, double conversion )
{
double totalSqMiles = 0;
totalSqMiles = oceanKilo * conversion;
return totalSqMiles;
}
public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
{
System.out.printf("\n\nOCEANS OF THE WORLD" +
"\n\nOcean: %s" +
"\n\nSize: %s" +
"%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);
}
}