简体   繁体   中英

how can I choose an array based on user input?

I'd be really grateful for any help here.

I'm trying to code a program that reads a given amount of positive numbers after each other then adds, subtracts, multiplies, calculate the average them with each other.

I know how to code the operations, just I don't know how to read the user input.

  1. My program asks the user initially how many number he she is going to type in, and based on that I want to code my program in such a way that it creates an array that matches the size and data types of the user input.

I'm struggling with arrays, and would really appreciate any help whatsoever. Here's how it should function briefly:

  1. Ask for user input.
  2. Read the input
  3. Create an array based on the input (based on data type and amount of numbers)
  4. Proceed with the various arithmetic operations.

I hope I can find some help here. Thanks in advance, Charmaine!

here is my code so far, with comments stating where exactly i need help

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

    }    

}

If you're not sure how to begin, just try to write the code in the order of the steps you need to take. Ask for the input, (I imagine you mean prompting the user, which you could just do with a System.out.println() statement), and then actually reading the input, using something like the Scanner class. There are several different ways to handle the array. If you're not sure how to begin, the least frustrating way to handle the arithmetic would just be a series of if-else statements. This is the kind of thing that's more helpful to figure out on your own, rather than having someone else give you code that you don't understand how to use.

This should fill your array for you. I hope its clear enough. If any problems arise with my array declaration, check out this place: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

I hope this is helpful.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM