2014-09-22 4 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, он будет читать эти следующие коды:для цикла внутри if и else if

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 .. Что мне делать, чтобы прочитать внутри "else if (code == 2)", когда я нажимаю номер 2? Благодарю за тех, кто мне поможет. :)

+0

Логическая ошибка будет где-то в установке значения 'code', что покажет нам, что находится внутри этого оператора if, который не поможет многому. – leigero

+0

Как вы получаете значение '1' или' 2' в переменной 'code'? – Himanshu

+2

В чем смысл 'for (int q = 1; q <= 1; q ++)'? –

ответ

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(); 
    } 
} 

Это выглядит отлично. Покажите нам свой полный код. для дальнейшего изучения.

Смежные вопросы