for if和else if中的循环

时间:2014-09-22 03:51:55

标签: java

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,它将读取以下代码:

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的相同代码。我将如何阅读&#34;否则如果(代码== 2)&#34 ;当我按数字2?谢谢你们的帮助。 :)

1 个答案:

答案 0 :(得分: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();
    }
}

看起来很好。向我们展示您的完整代码。进一步调查。