【问题标题】:Reading a file and storing data in an array, then find lowest decimal value读取文件并将数据存储在数组中,然后找到最小的十进制值
【发布时间】:2009-11-30 02:58:47
【问题描述】:

我的问题是我找不到正确显示最低人口值和拥有该值的省份的正确值的方法。

编写一个程序来读取 省份数据文件成数组 称为 ProvinceName 和一个数组 称为省人口。你可以 假设有 10 个省。 写一个循环:将数据读入 两个数组。将数组分析为 计算总人口 所有省份。写另一个 循环查找具有 最小的人口并打印 该省的名称。

数据存储方式如下(provinceData.txt):

Ontario
12891787
Quebec
7744530
Nova Scotia
935962
New Brunswick
751527
Manitoba
1196291
British Columbia
4428356
PEI
139407
Saskatchewan
1010146
Alberta
3512368
NF/LB
508270

这是我的 Java 代码:

import java.io.*;
import java.util.Scanner;

public class Slide50 
{
    public static void main(String[] args)throws IOException
    {
        File file = new File ("C:/provinceData.txt");
        Scanner in = new Scanner(file);
        int i = 0;      

        String province[] = new String[10];
        String lowProv = null;
        int pop[] = new int[10];        
        int totalPop = 0;
        int low = pop[0];

        //while there is data in the file to be processed       
        while(in.hasNext())
        {
            province[i] = in.nextLine();
            pop[i] = in.nextInt();

            //discard the \n on the line            
            in.nextLine();

            //regular processing goes here
            i++;
        }

        System.out.printf("\n\t%-16s %20s\n", "Province", "Population");
        System.out.printf("\t%-16s %20s\n", "========", "==========");  

        //print the province population report (which includes a total) using printf
        for (i = 0; i < pop.length; i++)
        {           
            System.out.printf("\t%-16s %,20d\n", province[i], pop[i]);      
            totalPop += pop[i];

            //find the province that has the smallest population 
            //and print out the province name and its population    
            if (pop[i] < low)
            {
                low = pop[i];                       
            }           
        }

        System.out.printf("\t%-16s %20s\n", "================", "==========");  
        System.out.printf("\t%-16s %,20d\n", "Total:", totalPop);                       
        System.out.println("\n\tThe province of " +  lowProv + " with a population of " +  low);
        System.out.println("\tis the least populated of all provinces.");   
    }
}

这是我基于此代码运行的示例:

Province                   Population
========                   ==========
Ontario                    12,891,787
Quebec                      7,744,530
Nova Scotia                   935,962
New Brunswick                 751,527
Manitoba                    1,196,291
British Columbia            4,428,356
PEI                           139,407
Saskatchewan                1,010,146
Alberta                     3,512,368
NF/LB                         508,270
================           ==========
Total:                     33,118,644

The province of null with a population of 0
is the least populated of all provinces.

【问题讨论】:

    标签: java arrays file


    【解决方案1】:

    你有两个问题:

    1. 您将low 初始化为零。 low 在创建pop 后立即初始化,因此pop[0] 仍然为0。我建议仅使用Integer.MAX_VALUE 对其进行初始化(或将其移至其他位置)。

    2. 在更新 low 值时,您不会更新 lowProv。所以总是null

    【讨论】:

      【解决方案2】:

      您将 low 设置为 0。请将其设置为 Integer.INT_MAX。

      e: notnoop 覆盖了它。但是,是的,您还需要更新lowProv

      【讨论】:

        【解决方案3】:

        最简单的方法是在遍历文件时找到最小值:

        while (...) {
          province[i] = ...
          pop[i] = ...
          if (pop[i] < minpop) {
            minpop = pop[i];
            minprovince = province[i];
          }
          ...
        }
        

        在第二个循环中,您已经完成了大部分工作,但是在将任何内容放入 pop 数组之前,您将设置为低以成为 pop 中的第一个条目,因此请播种该值类似于Integer.MAX_VALUE

        【讨论】:

          猜你喜欢
          • 2015-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-08
          • 2015-01-22
          • 1970-01-01
          • 2016-05-11
          相关资源
          最近更新 更多