我对编码很陌生并且对使用其他类的switch语句有疑问。这是我的原始功能类,它接受用户输入,然后提供有关公寓定价的信息。首先,要求用户选择公寓类型(#1-3),然后提示他们决定是否需要停车位或车库(1或2)。我试图将类分开,以便只有方法在一个类中,而main方法中的其余代码在另一个类(测试类)中。我遇到的问题是找出一种通过switch语句传递用户输入的方法。我试图找到方法,但没有运气。
import java.util.Scanner;
public class CondoSales
{
public static void main(String[] args)
{
System.out.println("Please select one of the following for pricing
information: " +
"1 for a condo with a park view, 2 for a condo with a golf course view, or 3
for a condo with a lake view.");
Scanner input = new Scanner(System.in);
int priceSelection;
priceSelection = input.nextInt();
final int PARK_VIEW = 150000;
final int GOLF_COURSE_VIEW = 175000;
final int LAKE_VIEW = 210000;
int parkingCost;
switch(priceSelection)
{
case (1):
System.out.println("The condo with a park view costs $" + PARK_VIEW);
parkingCost = PARK_VIEW;
break;
case (2):
System.out.println("The condo with a golf course view costs $" + GOLF_COURSE_VIEW);
parkingCost = GOLF_COURSE_VIEW;
break;
case (3):
System.out.println("The condo with a lake view costs $" + LAKE_VIEW + ".");
parkingCost = LAKE_VIEW;
break;
default:
System.out.println("$0");
parkingCost = 0;
}
System.out.println("Select 1 for a parking spot or 2 for a garage:");
int parkingSpecification;
parkingSpecification = input.nextInt();
switch(parkingSpecification)
{
case (1):
System.out.println("With a parking spot, the cost is $" + parkingCost);
break;
case (2):
System.out.println("With a garage, the cost is $" + (parkingCost + 5000));
break;
default:
System.out.println("You have entered an invalid response; the condo will cost $" + parkingCost + ".");
}
}
}
这是测试类,我还没有弄清楚如何将价格选择输入传递到原始类的开关。任何帮助将不胜感激,因为我一直在撞墙试图解决这个问题。谢谢!
import java.util.Scanner;
public class TestCondoSales
{
final int PARK_VIEW = 150000;
final int GOLF_COURSE_VIEW = 175000;
final int LAKE_VIEW = 210000;
int parkingCost;
public static void main(String[] args)
{
CondoSales methodRetrieval = new CondoSales();
System.out.println("Please select one of the following for pricing
information: " +
"1 for a condo with a park view, 2 for a condo with a golf course view, or 3
for a condo with a lake view.");
Scanner input = new Scanner(System.in);
int priceSelection;
priceSelection = input.nextInt();
methodRetrieval.priceReturn(priceSelection);
System.out.println("Select 1 for a parking spot or 2 for a garage:");
int parkingSpecification;
Scanner input2 = new Scanner(System.in);
parkingSpecification = input.nextInt();
methodRetrieval.parkingReturn(parkingSpecification);
}
}
答案 0 :(得分:0)
尝试将切换条件用作另一个类中的方法参数,然后将输入传递给该方法。例如:
import java.util.Scanner;
public class CondoPriceSelector {
public static void main(String[] args) {
PriceSelector selector = new PriceSelector();
try(Scanner scan = new Scanner(System.in)){
int priceSelection = scan.nextInt();
selector.printPrice(priceSelection);
}
}
}
class PriceSelector {
final int parkingCost = 5000;
public void printPrice(int selection) {
switch (selection) {
case (1):
System.out.println("With a parking spot, the cost is $" + parkingCost);
break;
case (2):
System.out.println("With a garage, the cost is $" + (parkingCost + 5000));
break;
default:
System.out.println("You have entered an invalid response; the condo will cost $" + parkingCost + ".");
}
}
}