使用数组&;Java中的循环
我想要一个零数据、50个名字和数字的输入文件。我使用循环和数组来保存名称和数字。我认为我的数组或循环语句有问题使用数组&;Java中的循环,java,arrays,loops,Java,Arrays,Loops,我想要一个零数据、50个名字和数字的输入文件。我使用循环和数组来保存名称和数字。我认为我的数组或循环语句有问题 import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.FileOutputStream; public class StudentParty { p
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
public class StudentParty
{
public static void main(String[] args)
{
String name1;
double number1;
String name2;
double number2;
String name3;
double number3;
double count = 3;
double total = 0;
double average;
String [] name = new String [50];
String [] number = new String [50];
Scanner fileIn = null;
PrintWriter OutputStream = null;
try
{
fileIn = new Scanner
(new FileInputStream
("StudentPartyInput.txt"));
OutputStream = new PrintWriter(new FileOutputStream
("StudentPartyOutput.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not foud.");
System.exit(0);
}
while (fileIn.hasNextLine())
{
line = fileIn.nextLine();
count++;
name1 = fileIn.nextLine();
number1 = fileIn.nextDouble();
fileIn.nextLine();
total = total + number1;
}
average = total / count;
fileIn.close();
OutputStream.println();
OutputStream.println(name1 + " had " + (number1 - average) + " more drinks than the average ");
OutputStream.println(name2 + " had " + (number2 - average) + " more drinks than the average ");
OutputStream.println(name3 + " had " + (number3 - average) + " more drinks than the average ");
OutputStream.close();
}
}
我假设您的示例输入与此非常相似
sample One
120.00
sample Two
130.92
sample three
140.34
我已经修改了你的代码。。在您的代码中,您使用了太多冗余变量,如name1、number1和all。。。我已经通过数组声明删除了所有这些。。。。试试这个
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class StudentParty
{
public static void main(String[] args)
{
double total = 0;
double average;
String [] name = new String [50];
Double [] number = new Double [50];
Scanner fileIn = null;
PrintWriter OutputStream = null;
try
{
fileIn = new Scanner
(new FileInputStream
("StudentPartyInput.txt"));
OutputStream = new PrintWriter(new FileOutputStream
("StudentPartyOutput.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
System.exit(0);
}
int cnt=0;
while (fileIn.hasNextLine())
{
name[cnt]=fileIn.nextLine();
number[cnt] = Double.valueOf(fileIn.next());
total = total + number[cnt];
cnt++;
if(fileIn.hasNextLine()) fileIn.nextLine();
}
average = total / (double)cnt;
fileIn.close();
OutputStream.println();
OutputStream.println(name[0] + " had " + (number[0] - average) + " more drinks than the average ");
OutputStream.println(name[1] + " had " + (number[1] - average) + " more drinks than the average ");
OutputStream.println(name[2] + " had " + (number[2] - average) + " more drinks than the average ");
OutputStream.close();
}
}
预计的样品O/p为
sample One had -10.419999999999987 more drinks than the average
sample Two had 0.5 more drinks than the average
sample three had 9.920000000000016 more drinks than the average
是什么让你觉得有什么不对劲?当你试图编译你的代码时,你会遇到错误吗?当您尝试运行代码时,是否出现错误?您是否得到意外的输出?请编辑您的问题并添加相关详细信息。您是否得到意外结果或任何编译错误??您没有声明变量行。。。您必须获得编译错误。请考虑扫描仪是一个Bug类。维克多说得很公平,不过我仍然会对你使用的“车”、“不工作”和“错误”等词进行争论。但我会让它成为现实。这是我个人的建议。非常感谢!终于成功了!我花了很多时间在谷歌上搜索如何使用循环和数组,但每次我改变一件事,就会发生另一个错误。谢谢大家!^-^