我真的很感激这里的任何帮助。
我正在尝试编写一个程序,在彼此之后读取给定数量的正数,然后相加,减去,相乘,计算它们之间的平均值。
我知道如何编写操作代码,只是我不知道如何读取用户输入。
我正在努力使用数组,并且非常感谢任何帮助。以下是它应该如何简单地运作:
我希望我能在这里找到一些帮助。 谢谢,Charmaine!
这是我的代码到目前为止,评论说明我需要帮助的地方
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Amount of numbers "); //I need to code an array based on this answer
double zahl1 = scan.nextDouble();
System.out.println("Type in the numbers "); // also how can I save n amount of numbers the user gives?
Scanner scan = new Scanner(System.in);
double summe = (zahl1 + zahl2 + zahl3); //I know how to code the rest just not with an unknown amount of number, this is with two numbers
double durchschnitt = ((summe)/3);
double produkt = zahl1 * zahl2 * zahl3;
double Kleinste; if (zahl1 < zahl2) {
if (zahl1 < zahl3) {System.out.println( zahl1 + " ist die Kleinste");}
else {System.out.println(zahl3 + "ist die kleinste");}
}
else {
if(zahl2 < zahl3){System.out.println(zahl2 + " ist die Kleinste");}
else System.out.println(zahl3 + " ist die Kleinste");
}
double Größte; if (zahl1 > zahl2) {
if (zahl1 > zahl3) {System.out.println( zahl1 + " ist die Größte");}
else {System.out.println(zahl3 + "ist die Größte");}
}
else {
if(zahl2 > zahl3){System.out.println(zahl2 + " ist die Größte");}
else System.out.println(zahl3 + " ist die Größte");
}
System.out.println("Die Summe betraegt " + summe);
System.out.println("Der Durchschnitt betraegt " + durchschnitt);
System.out.println("Das Produkt ist " + produkt);
}
}
答案 0 :(得分:0)
如果您不确定如何开始,只需尝试按照您需要采取的步骤的顺序编写代码。请求输入,(我想你的意思是提示用户,你可以用System.out.println()语句),然后实际读取输入,使用类似Scanner类的东西。有几种不同的方法来处理数组。如果你不确定如何开始,那么处理算术最不令人沮丧的方法就是一系列if-else语句。这是一种更有助于自己弄清楚的事情,而不是让别人给你一些你不懂的代码。
答案 1 :(得分:0)
这应该为您填充阵列。我希望它足够清楚。如果我的数组声明出现任何问题,请查看此地点:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
我希望这有用。
Scanner scan = new Scanner(System.in);
System.out.println(" number of numbers to be processed please:"); //asks for the total number of numbers that will be inputed.
int totalNum = scan.nextInt(); //stores the number of numbers to be processed in the int totalNum
array = new int[totalNum]; //creates an array, called array, of size totalNum; the number of numbers to be processed is the size of the array.
for ( int i = 0; i < totalNum; i++ )
{
array[i] = scan.nextInt(); //fills the array with the numbers that the user inputs.
}