【问题标题】:Simple java method using text.io使用 text.io 的简单 java 方法
【发布时间】:2014-12-02 22:05:50
【问题描述】:

如果有人可以帮我解决这个简单的任务,我将永远感激不尽!我对 Java 很陌生,不明白如何调试这个简单的代码。

我需要:

• 读取每行列出的冰淇淋风味的外部文件

• 计算冰淇淋的总数

• 数一数名为“草莓”的冰淇淋的数量

• 计算“草莓”冰淇淋的百分比

这是我到目前为止所得到的......

package icecreamcounter;

import java.io.*; //Import TextIO

public class IcecreamCounter {

public static void main(String[] args) {
    // Open file for reading; if it cannot be opened, end the program
         try {
     TextIO.readFile("icecream.dat");
  }
  catch (IllegalArgumentException e) {
     System.out.println("Can't open file \"icecream.dat\" for reading!");
     System.out.println("Please make sure the file is present before");
     System.out.println("running the program.");
     System.exit(1);  // Terminates the program.

     //read the file
     string flavor;
     int strawb; //Total number of strawberry icecreams
     int totalIcecream; // Total number of icecreams
     double percentageStrawb; //Percentage of icecreams which are strawberry

     strawb = 0;
     percentageStrawb = (totalIcecream - strawb)/totalIcecream;

     while (!TextIO.eof()) {  // process one line of data.
         flavor = TextIO.getlnString();  // Get the rest of the line.
         totalIcecream = totalIcecream + 1; // Add one to the count of total icecreams
         return totalIcecream;
         if (flavor.equals("Strawberry") { // If icecream name is strawberry 
             strawb=strawb+1;         //then add one to the count of strawberry
             return strawb;
         }
     }

     System.out.println("Total number of cones is" totalIcecream);
     System.out.println("Total number of strawberry cones is" strawb);
     System.out.println("Percentage of strawberry cones is" percentageStrawb);
  }
}

}

问题可能与读取文件部分有关,但当我尝试打印答案时,它也会在底部出现错误。另外,我是否必须返回例如“totalIcecream”的值才能稍后打印出来?任何帮助将不胜感激!

【问题讨论】:

  • 第一件事,在System.exit(1)之后关闭catch,而不是将整个程序都放在里面。
  • 你必须使用TextIO吗?
  • 不幸的是:(这是一个作业,专门要求使用TextIO。

标签: java


【解决方案1】:

首先,在system.exit(1) 之后,关闭catch 块,因为之后的代码永远不会到达。

其次,在println中,应该是"this is some text" + somevariable

将字符串变量或数字添加到字符串需要加号。

【讨论】:

    【解决方案2】:

    我建议使用 NIO,对我来说它运行良好且易于理解,这是我使用 Java 的 NIO API 的方法(自 Java 1.5 起可用)

    try {
            Path pathToMyTextFile = Paths.get("c:/data/icecream.dat");
    
            //Here you must use the Charset according to the encoding of your TEXT file (I use ISO_8859_1)
            List<String> linesInFile = Files.readAllLines(pathToMyTextFile, StandardCharsets.ISO_8859_1);
    
            int totalNumberOfIceCreams = linesInFile.size();
            String targetFlavor = "Strawberry";//I would like to handle this in a variable
            int numberOfTargetFlavor = 0;
    
            for (String iceCreamLine : linesInFile) {
                if (iceCreamLine.equals(targetFlavor)) {
                    numberOfTargetFlavor++;
                }
            }
    
            //Here I calculate the percentage:
            double percentage = (numberOfTargetFlavor * 100) / totalNumberOfIceCreams;
    
            System.out.println("Total number of Ice Creams: " + totalNumberOfIceCreams);
            System.out.println("Total number of " + targetFlavor + " Ice Creams: " + numberOfTargetFlavor);
            System.out.println("Percentage of Ice Creams that are flavor " + targetFlavor + ": " + percentage + "%");
    
        } catch (IOException e) {
            //Control your exception here
            e.printStackTrace();
        }
    

    我使用了一个名为“icecream.dat”的文件,该文件位于我的 c:/data 文件夹中。该文件的内容如下:

    Strawberry
    Chocolate
    Strawberry
    Orange
    Cheese
    Strawberry
    Raspberry
    Apple
    Strawberry
    

    我得到的输出是:

    Total number of Ice Creams: 9
    Total number of Strawberry Ice Creams: 4
    Percentage of Ice Creams that are flavor Strawberry: 44.0%
    

    我知道这不是 TextIO,但也是一个好方法。如果您必须使用 TextIO 进行练习,那么现在您有一种新方法,如果您将来需要它。

    问候和快乐编码:)

    【讨论】:

    • 不幸的是,我需要使用 Text.IO,但感谢您,我以后一定会尝试的!
    猜你喜欢
    • 2015-10-15
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多