如何根据用户输入选择数组?

时间:2015-11-21 12:36:20

标签: java arrays math types

我真的很感激这里的任何帮助。

我正在尝试编写一个程序,在彼此之后读取给定数量的正数,然后相加,减去,相乘,计算它们之间的平均值。

我知道如何编写操作代码,只是我不知道如何读取用户输入。

  1. 我的程序最初向用户询问她要输入多少个数字,并根据我想要编写程序的方式创建一个与用户输入的大小和数据类型相匹配的数组。
  2. 我正在努力使用数组,并且非常感谢任何帮助。以下是它应该如何简单地运作:

    1. 询问用户输入。
    2. 阅读输入
    3. 根据输入创建数组(基于数据类型和数量)
    4. 继续进行各种算术运算。
    5. 我希望我能在这里找到一些帮助。 谢谢,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);
      
          }    
      
      }
      

2 个答案:

答案 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. 
}