我的Java程序不输出它应该的一切。没有错误

问题描述:

我的代码需要一个输入(第一个颜色选择,然后金额);问我是否愿意再次这样做(如果是的话,它会重复输入颜色和数量的过程);然后它将这些值传递给两个不同的方法,这些方法计算每种颜色的价格,然后将计算量返回给main。我的Java程序不输出它应该的一切。没有错误

问题是放在第二颜色时(后问:“你想再拍购买?”),其输出只有第二输入的计算价格,而不是第一个。

这里是我的代码:

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

请学习正确使用开关盒。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – 2015-02-11 01:41:42

+0

我投票结束这个问题,因为它显示没有任何基本的调试应用的证据。 – John3136 2015-02-11 01:43:40

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这儿都是问题。计算不应该是有条件的。