如何在if else案例中继续循环?

时间:2013-05-01 14:47:42

标签: loops for-loop product continue

final static int arrayLength = 1000; //maximum array size
    static float[] productPrice = new float[arrayLength]; //stores the prices of products
    static String[] productName = new String[arrayLength]; //stores the names of products

    public static void main(String[] args) {

        boolean quit = false;
        do {
            System.out.print("enter product: ");
            String product = keyboard.nextLine();;
            for(int i=0; i<productName.length; i++)
                productName[i] = product;

            System.out.print("enter price: ");
            float price = keyboard.nextFloat();
            for(int i=0; i<productPrice.length; i++)
                productPrice[i] = price;

            if(price == -1)
                quit = true;
        } while(!quit);

        for(int i=0; i<productPrice.length; i++)
            if(productPrice[i] > productPrice[i+1]) {
                float temp = productPrice[i+1];
                productPrice[i+1] = productPrice[i];
                productPrice[i] = temp;
                System.out.println("Product: " + productName[i] + "\nPrice: " + productPrice[i]);
            }
    }

你如何继续循环?我需要保持它,直到我按-1停止? 我需要帮助说明这部分陈述请说出你可能的答案,并说明为什么我的循环不是连续的,以及我如何能使它保持gpoing以及我可以用来阻止它的内容

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

Price被声明为浮动,你将它与int进行比较。尝试:

if((int)price == 1)

if(price == 1.0f)