我的代码接受输入(首先选择颜色然后选择金额);问我是否愿意再做一次(如果是,那么它会重复输入颜色和金额的过程);然后它将值传递给两个不同的方法,计算每种颜色的价格,然后将计算出的数量返回给主。
问题在于何时使用第二种颜色(在问及&#34之后;您想再次购买?"),它只输出第二个输入的计算价格,而不输出第一个。
这是我的代码:
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!");
}
答案 0 :(得分:-1)
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("");
}
if
这里是问题所在。计算不应该是有条件的。