如果有人可以请帮我解决这个简单的任务,我会永远感谢!我对Java非常陌生,无法理解如何去调试这个简单的代码。简单的java方法使用text.io
我需要:
•阅读对各行
上市的冰淇淋口味的外部文件•计数冰淇淋
总数•伯爵冰的数量名称为“草莓”
面霜•计算是“草莓”
这是我的冰淇淋的百分比就是我有这么远......
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'。 – RealSkeptic 2014-12-02 22:07:41
您是否强制使用TextIO? – 2014-12-02 22:17:20
不幸的是:(这是一个任务,特别要求使用TextIO。 – Jessica 2014-12-03 03:09:44