0

作业:你小表弟上的一所学校卖饼干。如果你表弟的班比其他班卖的饼干多,老师就答应带全班去野餐。当然,您的表弟自愿让您跟踪所有销售情况并确定获胜者。

每个班级都由老师的名字标识。每张销售单上都有老师的姓名和售出的箱子数量。您决定创建两个并行数组:一个保存老师的姓名,另一个记录售出的盒子数量。以下是数据示例:

第一个数字给出了班级的数量,然后是老师的名字,然后是售出的盒子数量 15 Smith 3 Courtney ... 依此类推


我的主要问题(因为我可以将它复制为“未来”平行数组)正在让每隔一行保存到一个数组中以存储已售出的盒子,因此数组“boxSold”看起来像

[1] 15
[2] 3

    package assignment5Package;

    import java.util.Scanner;

    import java.io.*;



    public class assignment5Demo 
    {

        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException 
        {
            // TODO Auto-generated method stub
            //create arrays, variables
            Scanner keyboard = new Scanner(System.in);
            BufferedReader input = new BufferedReader
                (new FileReader ("/Users/lee/Desktop/class/cs 113/Assignment5/cookies.txt"));

            System.out.println("How many sale slips are there");

            int numSaleSlips = keyboard.nextInt();
            int[] soldBox = new int[numSaleSlips];
            //______String[] teacherName = new String[numSaleSlips];
            int soldBoxIndex;
            int teacherNameIndex;
            //String soldBoxString; (line 50)




            //initializing both strings to 0 and "_"
            for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
            {
                soldBox[soldBoxIndex] = 0;
            }

            //**for (teacherNameIndex = 0; teacherNameIndex < numSaleSlips; teacherNameIndex++)
            //**{
            //**    teacherName[teacherNameIndex] = "_";
            //**}

            //reading from the cookies.txt file
            for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
            {
                if (soldBoxIndex % 2 != 0
                {
                    String soldBoxString;


                    soldBoxString = input.readLine(); //reads in value and assigns/re-assigns
                    soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array
                    System.out.println(soldBox[soldBoxIndex]);
                }
                else 
                {
                    System.out.println("Error at " + soldBoxIndex +".");
                }

            }       
}
4

1 回答 1

0

以下可能是一个快速而肮脏的解决方案,但可以完成工作:

for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++)
{
    if (soldBoxIndex % 2 != 0
    {
        String soldBoxString;


        soldBoxString = input.readLine(); //reads in value and assigns/re-assigns
        soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array
        System.out.println(soldBox[soldBoxIndex]);
    }
    else 
    {
        input.readLine(); //read the following line, but ignore its content, effectivly skipping the line
    }

}

您可能还需要稍微处理一下 for 循环的数字,以适应跳过的行。

于 2012-11-28T08:43:02.650 回答