If-Else需要变量初始化

时间:2016-09-04 21:25:55

标签: java if-statement

为了使第一个If-Else语句有效,我必须用“= 0”初始化“quantity”和“productNo”变量。分配中未指定此内容。还有另一种方法可以在不初始化变量的情况下使If-Else工作吗?

第一个If语句允许用户在程序的第一个提示符处输入sentinel“-1”,以便在需要时退出(这是提示文本的一部分)。程序结束时的第二个If-Else语句允许用户在任何一点使用sentinel值退出。

这是一个Java应用程序,用于计算总订单零售额 购买的产品和数量的选择。共有5种产品,每个单位有5个固定成本。用户输入产品#,然后输入数量。输出是产品#,订购数量,生产线成本和订单总数。它的标记值为“-1”,输入时允许用户退出程序。

import java.util.Scanner;
public class Program3
{   
 public static void main(String[] args) 
 {

  final double COST_PROD1 = 2.98, COST_PROD2 = 4.50, 
  COST_PROD3 = 9.98, COST_PROD4 = 4.49, COST_PROD5 = 6.87;
  int productNo = 0;
  int quantity = 0;
  double lineAmount = 0;    
  double orderAmount = 0;

   Scanner inputDevice = new Scanner(System.in);


   System.out.print("Enter Product No. (1-5) or -1 to Quit: >> ");
   productNo = inputDevice.nextInt();


    if (productNo == -1)
        {
    System.out.println("You have entered -1. You have exited the program."); 
    System.out.println("Order grand total = $0.00");    
        }

     else 
     {
       System.out.print("Please enter unit quantity >> ");
       quantity = inputDevice.nextInt();
      }  

    while(productNo != -1)
        {    
     switch(productNo)
        {
     case 1:
     lineAmount = quantity * COST_PROD1;
     orderAmount += lineAmount;
       System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
         + "Total Order");
       System.out.println("   " + productNo + "          " + quantity + 
         "          " + lineAmount + "         " + orderAmount); 
       break;

      case 2:
        lineAmount = quantity * COST_PROD2;
        orderAmount += lineAmount;
         System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
            + "Total Order");
         System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount); 
           break;

       case 3:
         lineAmount = quantity * COST_PROD3;
         orderAmount += lineAmount;
         System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
           + "Total Order");
         System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount);  
        break;

        case 4:
        lineAmount = quantity * COST_PROD4;
        orderAmount += lineAmount;
          System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
            + "Total Order");
          System.out.println("   " + productNo + "          " + quantity + 
            "          " + lineAmount + "         " + orderAmount); 
          break;

          case 5:
          lineAmount = quantity * COST_PROD5;
          orderAmount += lineAmount;
          System.out.println("Product -- " + "Quantity -- " + "Line Cost -- " 
             + "Total Order");
           System.out.println("   " + productNo + "          " + quantity + 
             "          " + lineAmount + "         " + orderAmount); ; 
           break;

          default:
            System.out.println("You have entered an invalid product number.  
            Try again.");
             break;
        }//end switch

        System.out.print("Enter Product No. (1-5) or -1 to Quit: >> ");
        productNo = inputDevice.nextInt();
            if (productNo == -1)
       {
        System.out.println("You have entered -1. You have exited the 
              program."); 
        System.out.println("Order grand total..." + orderAmount);  
        }
        else 
        {
       System.out.print("Please enter unit quantity >> ");
       quantity = inputDevice.nextInt();
          }
     } // end while     
   } // end main
  } // end class

2 个答案:

答案 0 :(得分:0)

您需要确保输入标记值后,您的代码会立即退出。否则,代码将有一个路径,其中变量productNo或quantity用于计算而不进行初始化。

将变量设置为某些初始值是一种有效的方法;在使用未初始化的值之前退出是另一种选择。

答案 1 :(得分:0)

根据Java规范,局部变量(在函数内定义的变量)不会被初始化,开发人员需要对其进行初始化。