0
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 以及我可以使用什么来阻止它

4

2 回答 2

0

do while 是您要查找的语句

https://www.google.ca/search?q=do+while&rlz=1C1SVEE_enES423ES423&aq=f&oq=do+while&aqs=chrome.0.57j0l3j62l2.1261j0&sourceid=chrome&ie=UTF-8

于 2013-05-01T14:51:18.443 回答
0

价格被声明为浮点数,您将其与 int 进行比较。尝试:

if((int)price == 1)

或者

if(price == 1.0f)
于 2013-05-01T15:07:15.443 回答