创建从文件读取数据的程序。数据将存储在两个单独的一维数组中,用于显示文件信息。

寻找有关JAVA编码的帮助。即使到处寻找不同的示例,也不了解如何编写程序代码。 [oil.txt] [1]

  • Add import java.util.; and import java.io.;
  • Create Scanner object that will be associated with a file named oil.txt. Each line of this file contains the name of a country and the percentage or the world's oil supply exported by the country.
  • Define a String array with 15 elements that will contain the names of the countries.
  • Define a double array with 15 elements that will contain the percentages of the world's oil supply exported by each country.
  • Call a void method named readData that has three parameters: a Scanner, a String array, and a double array.
  • Call a method named findSmallest that returns a String and has three parameters: a String array, a double array, and an integer for the length of the arrays.
  • The readData method should use a while loop to store the name of each country in the String array and store the percentage for each country in the double array.
  • The findSmallest method should use a loop of any type to find the smallest percentage in the double array and return a string containing both the country with the smallest percentage and the percentage for that country. EX output: Venezuela exports the smallest percentage of oil at 2.31%.

Here is what I've come up with so far: import java.util.*; import java.io.*;

    public static void main(String[] args) {
        Scanner kb = new Scanner ("oil.txt");
        String[] Countries = new String[15];

        Countries[0] = "Kuwait";
        Countries[1] = "Venezuela";
        Countries[2] = "Nigeria";
        Countries[3] = "Iraq";
        Countries[4] = "Russia";
        Countries[5] = "Norway";
        Countries[6] = "Canada";
        Countries[7] = "Libya";
        Countries[8] = "Kazakhstan";
        Countries[9] = "SaudiArabia";
        Countries[10] = "USA";
        Countries[11] = "Angola";
        Countries[12] = "Mexico";
        Countries[13] = "Iran";
        Countries[14] = "UAE";
 }
public static void readData() {
}
}


  [1]: https://i.stack.imgur.com/WNz4R.jpg
评论