2013-06-28 35 views
-1

我需要知道如何对用户输入的数字列表进行计算。这是我的代码到目前为止,我失去了如何进行。我需要编写一个代码,询问用户是否想要手动输入数字或从文件上传。如果用户选择手动,我必须找到平均值,最小值,最大值和站点。开发。他们输入的数字。任何帮助,将不胜感激如何在JAVA中对用户输入的数字列表进行计算?

import java.io.*; 

public class computation { 
//main method starts 
    public static void main (String[] args) throws Exception { 

     //create text input reader 
     InputStreamReader get = new InputStreamReader(System.in); 
     BufferedReader got = new BufferedReader(get); 
     //create a text printer 
     PrintWriter send = new PrintWriter(System.out,true); 

     //defining a string type variable for user input 
     String answer1; 
     //asks the user if they want to input data from keyboard 
     String question = "Do you want to input data manually? Enter y or n"; 
     send.println(question); 
     //system reads user input 
     answer1 = got.readLine(); 

     if (answer1.equals("y")) { 
      send.println("Enter numbers separated by spaces"); 
      String datacurrent = got.readLine(); 
     } //end of "y" if 
     if (answer1.equals("n")) { 
      send.println("Enter the path of your data file"); 
     } //end of "n" if 


    } //end of main method 
} //end of class computation 
+2

''我需要编写一个代码,询问用户是否想手动输入数字或从文件上传。如果用户选择手动,我必须找到平均值,最小值,最大值和立场。dev。的数字,他们输入。任何帮助,将不胜感激 - 任何第一次尝试,你也将赞赏,请至少尝试。 –

+2

这似乎是一个家庭作业的问题,一般皱眉在这里。 – hexafraction

+1

家庭作业是好的,但他们必须*至少*显示一个尝试,否则我们该如何知道如何帮助他们。我们不能替代教程/书籍和努力学习语言。问题被关闭 –

回答

0

我刚刚运行您的程序,它似乎没事。

(A)当通过控制台输入值时该怎么办?

分割字符串到一个数组,提取每个号码,然后计算平均最大,和支架偏差

对于前:

String[] numbers = datacurrent.split(","); 

for(int i=0; i<number.length; i++) 
{ 
.... 
} 

(B)时作为文件路径输入该怎么办?

您将不得不使用File Stream/Buffer Reader来阅读文件。

BufferedReader reader = new BufferedReader(new FileReader(Your string variable pointing to path)); 

然后遍历文件数据。

注意:您需要知道文本文件格式才能遍历文件内容。

+0

是的,我需要的是通过切割逗号分割字符串到数组!谢谢,这就是我真正需要的,我现在知道如何去做其余的事情。 – user2530133

+0

不用担心队友,欢呼! –

0

您可能想看看较新的类System.console,我们得到的便利控制台I/O更容易。你的程序显示了尝试,如果你学习一两本优秀的Java书籍,你将可以无错地编写这个程序。两本很好的新Java书籍是Sierra/Bates OCJP学习指南和新的A Press Java 7 book。我建议你从这些书中的一本或两本中读一两本,你将学习如何使用?用于控制台I/O的System.console。

System.out.print("Enter something:"); 
String input = System.console().readLine(); 
相关问题