【发布时间】:2015-02-11 01:39:32
【问题描述】:
我的代码需要一个输入(首先选择颜色,然后是数量);询问我是否想再做一次(如果是,则重复输入颜色和数量的过程);然后它将这些值传递给两个不同的方法,这些方法计算每种颜色的价格,然后将计算出的金额返回给 main。
问题是当输入第二种颜色时(在询问“你想再购买一次吗?”之后),它只输出第二种输入的计算价格,而不是第一种。
这是我的代码:
import java.util.Scanner;
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
String answer, color;
int red_gallons=0;
int red_gallons_2=0;
int green_gallons_2=0;
int green_gallons=0;
double green_cost=0;
double red_cost=0;
System.out.println("which paint color do you wish to purchase (g or r)");
color = in.next();
switch (color)
{
case"G":
case "g":
{
System.out.println("how many full gallons of paint do you need");
green_gallons = in.nextInt();
break;
}
case"R":
case "r":
{
System.out.println("how many full gallons of paint do you need");
red_gallons = in.nextInt();
break;
}
}
System.out.println("Would you like to make another purchase? (y/n)");
answer = in.next();
if (answer.equalsIgnoreCase("Y"))
{
System.out.println("which paint color do you wish to purchase (g or r)");
color = in.next();
switch (color)
{
case"G":
case "g":
{
System.out.println("how many full gallons of paint do you need");
green_gallons_2 = in.nextInt();
break;
}
case"R":
case "r":
{
System.out.println("how many full gallons of paint do you need");
red_gallons_2 = in.nextInt();
break;
}
}
}
else
{
System.out.print("");
}
if (color.equalsIgnoreCase("g"))
{
green_cost = green_paint(green_gallons,green_gallons_2);
}
else if (color.equalsIgnoreCase("r"))
{
red_cost = red_paint(red_gallons,red_gallons_2);
}
else
{
System.out.print("");
}
double total_cost = total_bill(red_cost, green_cost);
String store = store_title();
thank_you(store, red_cost, green_cost, total_cost ); //BILL
}
//calculates cost of red paint an returns it to main
public static double red_paint(int r_amount_1, int r_amount_2 )
{
double cost_of_red = 21.95;
double total_red = cost_of_red*(r_amount_1+r_amount_2);
return total_red;
}
//calculates cost of green paint and returns it to main
public static double green_paint(int g_amount_1, int g_amount_2)
{
double cost_of_green = 19.95;
double total_green = cost_of_green*(g_amount_1+g_amount_2);
return total_green;
}
//title of store, returned to main
public static String store_title()
{
String str = "\n\n*******************\n*THE RAINBOW STORE*\n*******************";
return str;
}
//calculates bill with tax, and returns it to main
public static double total_bill(double red$, double green$)
{
double bill = red$+green$;
double plus_tax = bill+(bill*.08);
return plus_tax;
}
//prints the bill
public static void thank_you(String stor, double red, double green, double total)
{
System.out.println(stor);
System.out.printf("Red Paint: $%.2f",red); System.out.println();
System.out.printf("Green Paint: $%.2f",green); System.out.println();
System.out.printf("Total: $%.2f",total); System.out.println();
System.out.println("Thank You for shopping at the Rainbow Store!");
}
【问题讨论】:
-
请学会正确使用switch case。 docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
-
我投票决定将此问题作为离题结束,因为它没有显示任何基本调试已应用的证据。