2014-09-22 59 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(); 
    } 
} 

这看起来不错。向我们显示您的完整代码。作进一步调查。