Simple calculator program in Java. No. of integers read and Average

青春壹個敷衍的年華 提交于 2019-12-24 02:46:04

问题


I have a task that requires me to print out the following quantities:

I need to write a java program that can calculate: 1. the number of integers read in 2. the average value—which need not be an integer!

My final output should look like:

$ javac SimpleCalc 
$ java SimpleCalc 3 4 6 8 1 
5
8
4.4 
7

I know i need to use a scanner class to read then as the user inputs then, i dont know how to make it display a count. I think that i can work this out, i can compute the average by adding up the numbers and divide the toal by the number of integers entered.

However its my first time writing a program, and this is doing my head in. Any help would be appreciated, ill even accept pseudocode if it helps me work out how to approach this.


回答1:


Here you are supposed to use the arguments of the main function:

public class PrintArgs {
    public static void main (String[] args) {
        for (int i = 0; i<args.length; ++i) {
          System.out.println("Args " + (i + 1) + " is " + args[i]);
        }
    }
}

Save this class in a file called PrintArgs.java and compile it using javac:

$ javac PrintArgs.java

Run the generated class file using java:

$ java PrintArgs hello world
Args 1 is hello
Args 2 is world

$ java PrintArgs 3 4 6 8 1 
Args 1 is 3
Args 2 is 4
Args 3 is 6
Args 4 is 8
Args 5 is 1

Now you know how to read input from the user. To convert a String to int, use the parseInt method of the Integer class:

int i = Integer.parseInt("980");
System.out.println(100 + i); // => 1080

Now you know how to write your calculator program!




回答2:


No you don't need a scanner in solving this. What you need is :

  • Understanding of 'main method'
  • Understanding of array and 'varargs'
  • Understanding of parsing a string into another Object like Double or Integer
  • Simple math (i believe you possess this)

If this is homework, your teacher ask you to read what i mention above.




回答3:


As I remember all you arguments are passed and stored in program in the variable that you main function expose:

public class Echo 
{
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
} 

So all is in your string[] args. This is array (the first is your program path and file), it has something like "Count()" method (don't remember), then you need to parse them to numbers using some wrapper, like Integer.Parse(args[2]).

http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html




回答4:


public static void main(String args[])
{
   // 1. the number of integers read in

    int len = str.length

   //2. the average value


       int sum = 0;
       int avg = 0;
       for(int i = 0 ; i < len ; i++)
       { 
          sum = sum + str[i];
       }
       avg = sum/len ;
}


来源:https://stackoverflow.com/questions/6993060/simple-calculator-program-in-java-no-of-integers-read-and-average

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!