【问题标题】:how can I choose an array based on user input?如何根据用户输入选择数组?
【发布时间】:2016-02-23 22:18:00
【问题描述】:

如果能提供任何帮助,我将不胜感激。

我正在尝试编写一个程序,该程序依次读取给定数量的正数,然后将它们相加、相减、相乘、计算它们的平均值。

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

  1. 我的程序最初会询问用户她要输入多少个数字,并基于此我想对我的程序进行编码,使其创建一个与用户输入的大小和数据类型相匹配的数组.

我正在努力解决数组问题,并且非常感谢任何帮助。以下是它的简要功能:

  1. 请求用户输入。
  2. 读取输入
  3. 根据输入创建一个数组(基于数据类型和数字数量)
  4. 继续各种算术运算。

我希望我能在这里找到一些帮助。 提前感谢,佘诗曼!

到目前为止,这是我的代码,cmets 说明了我需要帮助的确切位置

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

    }    

}

【问题讨论】:

  • 您已经为您列出了步骤,并且您应该尝试一次解决每个步骤,即使在困难的情况下也可以单独解决,然后将它们放在一起。在来这里之前,您应该真正尝试一下,如果只是为了帮助您更集中您的问题。
  • 谢谢!请检查我发布的代码,也许你可以帮忙。

标签: java arrays math types


【解决方案1】:

如果您不确定如何开始,请尝试按照您需要执行的步骤顺序编写代码。要求输入,(我想您的意思是提示用户,您可以使用 System.out.println() 语句来完成),然后使用类似 Scanner 类的东西实际读取输入。有几种不同的方法来处理数组。如果您不确定如何开始,处理算术最不令人沮丧的方法就是一系列 if-else 语句。这种事情更有助于您自己弄清楚,而不是让其他人给您提供您不了解如何使用的代码。

【讨论】:

  • 我已经或多或少地弄清楚了算术,我如何使用扫描仪读取 n 个数字,并将它们保存在一个数组中,我以后可以在算术中使用它。
  • 像这样实例化一个 Scanner 对象:Scanner in = new Scanner(System.in),并使用 Scanner 方法 nextInt() 实际读取输入。
  • 我的代码中就有这个(参见原始帖子),但是当多个输入应该一个接一个地读取时,它是如何工作的?然后保存在数组中?我迷失了想法..
  • 他们输入的数字是单独输入的吗?例如,用户每次输入数字时都按回车键?还是一次性输入的数字,用空格、逗号之类的分隔?
  • 要么要么都可以,我想我更喜欢个人输入。
【解决方案2】:

这应该会为您填充您的数组。我希望它足够清楚。如果我的数组声明出现任何问题,请查看这个地方: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. 
}

【讨论】:

    猜你喜欢
    • 2022-06-30
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多