我对Java编程以及整个对象编程都是陌生的,因此我对使用哪种类型的数组感到困惑。

我需要创建一个数组,以接受用户输入的NAME和PRICE并将其存储在该数组中。我尝试制作两个单独的数组并使用一个计数器,但这不符合我的需求。问题在于我需要能够分别平均PRICE以及反转数组并过滤数组(如果输入了特定名称)的问题。我也尝试创建对象数组,但不确定是否合适。

这是我的代码:

String names[] = new String[100]; // sets array size to almost ulimited #
            double prices[] = new double[100]; // sets array size to almost unlimited #
            double average = 0; // initializes the array
            int count = 0; // initializes the count for the arraysize
            boolean flag = false;// initializes flag to false so it can be changed later


            while(true){
           System.out.print("Enter item " + (count + 1) + " name: "); // enter name
           names[count] = in.next(); // next string becomes count index
           if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break
           System.out.print("Enter item " + (count + 1) + " price: "); // enter price
           prices[count] = in.nextDouble(); // stores price entered as array index
            // if next price == -1 then the program ends
           if(names[count].equalsIgnoreCase("peas")) flag = true;
           average += prices[count];
           count++;

       }
            System.out.println("The items and their prices are: ");
            for(int i = 0; i < count; i++){
           System.out.println(names[i] + ": " + formatter.format(prices[i]));
       }
            average /= count;
            if(flag == true){
           System.out.println("Average: " + formatter.format(average));
       }
            else{
                System.out.println("no average output");
       }


我并不一定要找人为我或类似的事情这样做,我很困惑,正在寻找我的逻辑应该思考的方式。提前谢谢大家!

最佳答案

我认为您的循环基本上很不错,只是我做了一些使其有效的事情。
-将其全部放入一个循环中
-摆脱“如果平均值为真”,则平均值是正确的,只是以某种方式被认为是错误的

    String names[] = new String[5]; // sets array size to almost unlimited #
            double prices[] = new double[100]; // sets array size to almost unlimited #
            double average = 0; // initializes the array
            int count = 0; // initializes the count for the arraysize
            boolean flag = false;// initializes flag to false so it can be changed later


            while(true){
           System.out.print("Enter item " + (count + 1) + " name: "); // enter name
           names[count] = System.console().readLine();//in.next(); // next string becomes count index
           if(names[count].equals("-1")){ break;} // if the next name entered is sentinel break
           System.out.print("Enter item " + (count + 1) + " price: "); // enter price
           prices[count] = Double.valueOf(System.console().readLine());//in.nextDouble(); // stores price entered as array index
            // if next price == -1 then the program ends
           if(names[count].equalsIgnoreCase("peas")) flag = true;
           average += prices[count];
           count++;
//     }//deleted

            System.out.println("The items and their prices are: ");
            for(int i = 0; i < count; i++){
           System.out.println(names[i] + ": " + prices[i]);//formatter.format()
//     }//deleted
            average /= count;
            //if(flag == true){
           System.out.println("Average: " + average);//formatter.format()
       //}
            else{
                System.out.println("no average output");
       }//added
       }//added
       }

07-24 09:49
查看更多