Java中的简单计算器程序。读取的整数和平均值 [英] Simple calculator program in Java. No. of integers read and Average

查看:293
本文介绍了Java中的简单计算器程序。读取的整数和平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务要求我打印出以下数量:

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

我需要编写一个可以计算的java程序:
1. the在
中读取的整数数量2.平均值 - 不必是整数!

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.

推荐答案

在这里你应该使用 main 函数的参数:

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]);
        }
    }
}

将此类保存在文件中叫 PrintArgs.java 并使用 javac 编译它:

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

$ javac PrintArgs.java

使用生成的类文件运行 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

现在你知道如何读取用户的输入。要将 String 转换为 int ,请使用 parseInt 方法整数类:

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!

这篇关于Java中的简单计算器程序。读取的整数和平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆