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.