2014-09-22 64 views
-3
if(code==1) 
       { 
       System.out.print("Enter product number: "); 
       int productnumber=Integer.parseInt(a.readLine()); 
       System.out.print("Enter product name: "); 
       String productname=a.readLine(); 
       System.out.print("Enter price: "); 
       int price1=Integer.parseInt(a.readLine()); 
       System.out.println(""); 
       { 
         int p=0; 
          for(int q=1;q<=1;q++) 
          { 
           System.out.println("1. Maintenance"); 
           System.out.println("2. Transaction"); 
           System.out.println(""); 

           System.out.print("Enter Code: "); 
           int code1=Integer.parseInt(a.readLine()); 
           System.out.println(""); 
          } 
       } 
       { 
       int w; 
       for(productnumber=2; productnumber<=2; productnumber++) 
       { 
       System.out.println("Product number: "+productnumber); 
       System.out.print("Product name: "); 
       String prodname=a.readLine(); 
       System.out.print("Price: "); 
       int price2=Integer.parseInt(a.readLine()); 
       System.out.println(""); 
       } 
       } 
       } 
        else if(code==2) 
         { 
          System.out.println("Display List of Product-Prices"); 
          System.out.print("Display product number: "); 
          int productnumber2=Integer.parseInt(a.readLine()); 
          System.out.println("Total: "); 
          System.out.print("Payment: "); 
          int payment=Integer.parseInt(a.readLine()); 
          } 
         } 

//這段代碼有什麼問題?有一個選項是1.維護2.交易。如果我按數字1,它會讀取這個以下代碼:for循環裏面的if和else如果

int w; 
       for(productnumber=2; productnumber<=2; productnumber++) 
       { 
       System.out.println("Product number: "+productnumber); 
       System.out.print("Product name: "); 
       String prodname=a.readLine(); 
       System.out.print("Price: "); 
       int price2=Integer.parseInt(a.readLine()); 
       System.out.println(""); 

//但是,如果我按數字2,它會讀取與1號相同的代碼..我該怎麼辦閱讀當我按下數字2時,「else if(code == 2)」中的語句?感謝那些會幫助我的人。 :)

+0

邏輯錯誤將在設置'code'的值的某個地方,以便向我們展示如果語句不會有多大幫助。 – leigero 2014-09-22 03:57:35

+0

如何在變量'code'中獲得值'1'或'2'? – Himanshu 2014-09-22 04:02:28

+2

'for(int q = 1; q <= 1; q ++)'是什麼意思? – 2014-09-22 04:03:18

回答

0

您需要向我們顯示完整的代碼。只有這樣我們才能明白你錯在哪裏。基於上面提供的任何代碼片段,我已經構建了一個包裝器並運行代碼,似乎很好。看下面:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class test123 { 
    public static void main(String[] args) throws NumberFormatException, IOException { 
     BufferedReader a = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.print("Enter your code : "); 
     int code = Integer.parseInt(a.readLine()); 
     System.out.println(); 
     if (code == 1) { 
      System.out.print("Enter product number: "); 
      int productnumber = Integer.parseInt(a.readLine()); 
      System.out.print("Enter product name: "); 
      String productname = a.readLine(); 
      System.out.print("Enter price: "); 
      int price1 = Integer.parseInt(a.readLine()); 
      System.out.println(""); 
      { 
       int p = 0; 
       for (int q = 1; q <= 1; q++) { 
        System.out.println("1. Maintenance"); 
        System.out.println("2. Transaction"); 
        System.out.println(""); 

        System.out.print("Enter Code: "); 
        int code1 = Integer.parseInt(a.readLine()); 
        System.out.println(""); 
       } 
      } 
      { 
       int w; 
       for (productnumber = 2; productnumber <= 2; productnumber++) { 
        System.out.println("Product number: " + productnumber); 
        System.out.print("Product name: "); 
        String prodname = a.readLine(); 
        System.out.print("Price: "); 
        int price2 = Integer.parseInt(a.readLine()); 
        System.out.println(""); 
       } 
      } 
     } else if (code == 2) { 
      System.out.println("Display List of Product-Prices"); 
      System.out.print("Display product number: "); 
      int productnumber2 = Integer.parseInt(a.readLine()); 
      System.out.println("Total: "); 
      System.out.print("Payment: "); 
      int payment = Integer.parseInt(a.readLine()); 
     } 
     a.close(); 
    } 
} 

這看起來不錯。向我們顯示您的完整代碼。作進一步調查。