【发布时间】: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