English 中文(简体)
阅读一个阵列的档案和储存数据,发现最低的恶性价值
原标题:Reading a file and storing data in an array, then find lowest decimal value

www.un.org/Depts/DGACM/index_spanish.htm 我的问题是,我找不到正确展示最低人口价值和拥有最低人口价值的正确价值的途径。

Write a program to read in the province data file into an array called provinceName and an array called provincePopulations. You can assume that there are 10 provinces. Write a loop to: read the data into two arrays. Analyse the array to calculate the total population of all the provinces. Write another loop to find the province with the smallest population and print that the name of that province.

数据以下列方式储存(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 
 on the line            
            in.nextLine();

            //regular processing goes here
            i++;
        }

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

        //print the province population report (which includes a total) using printf
        for (i = 0; i < pop.length; i++)
        {           
            System.out.printf("	%-16s %,20d
", 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("	%-16s %20s
", "================", "==========");  
        System.out.printf("	%-16s %,20d
", "Total:", totalPop);                       
        System.out.println("
	The province of " +  lowProv + " with a population of " +  low);
        System.out.println("	is 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.
最佳回答

你有两个问题:

  1. 缩略语 0. 我建议以<条码”开始。 Integer. Max_VALUE(或将其移至别处)。

  2. 页: 1 因此,总是<代码>null。

问题回答

页: 1 由此而来。 INT_/2008/5。

e:不适用。 但确实,你需要更新<代码>lowProv。 以及

这样做最容易的方法是,在你通过档案时找到最低点:

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

你用你的第二座乐团来 most,但是,在你把任何东西放入“人阵列”之前,你会重新确定低端为首批登机。 Integer. 页: 1





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签