简体   繁体   中英

How can I select a value from an array?

How can I select a value from an array? Example is this String[] ans = {"+", "-", "/", "*"}; then I want to select "+" .

public static void main(String[] args) {
    String[] ans = {"+","-","/","*"};
    Random random = new Random();
    Scanner calcu = new Scanner(System.in);
    System.out.print("Enter First number: ");
    numOne = calcu.nextInt();
    System.out.print("Enter Second number: ");
    numTwo = calcu.nextInt();
    System.out.print("Choose an Operator to use");

}

You can use ans[0] for "+" and so on.

ans[0] = "+";
ans[1] = "-";
ans[2] = "/";
ans[3] ="*";

In your case, this code will help you:

     public static void main(String[] a) {

        String[] ans = {"+","-","/","*"};
        double result = 0;
        Scanner calcu = new Scanner(System.in);
        System.out.print("Enter First number: ");
        int numOne = calcu.nextInt();
        System.out.print("Enter Second number: ");
        int numTwo = calcu.nextInt();
        System.out.print("Choose an Operator to use");
        String oparation= calcu.next();

        if(oparation.equals(ans[0])){
           result = numOne + numTwo;
        }
        else if(oparation.equals(ans[1])){
            result = numOne - numTwo;
        }
        else if(oparation.equals(ans[2])){
            result = numOne / numTwo;

        } else if(oparation.equals(ans[3])){
            result = numOne * numTwo;
        }
        System.out.println("result is " + result);

   }

If you want same result using a switch statement:

public static void main(String[] a) {

        double result = 0;
        Scanner calcu = new Scanner(System.in);
        System.out.print("Enter First number: ");
        int numOne = calcu.nextInt();
        System.out.print("Enter Second number: ");
        int numTwo = calcu.nextInt();
        System.out.print("Choose an Operator to use");
        String oparation= calcu.next();

        switch(oparation){
            case "+" :
            result = numOne + numTwo;
            break;

            case "-" :
            result = numOne - numTwo;
            break;

            case "/" :
            result = numOne / numTwo;
            break;

            case "*" :
            result = numOne * numTwo;
            break;
        }
        System.out.println("result is " + result);

   }

However, with the switch statement, if you want to compare against variables like case ans[0]: instead of case "*" , then you can use enum .

The way you're implementing it, you would need to display a list like this to the user:

1: +
2: -
3: /
4: *

When they select a number, you can determine the operator with ans[input-1] .

您可以通过以下get方法访问它:

ans[i]; // for getting first element you should set i to 0

ans[indexThatYouWantToaccess] make sure array index starts with 0

ans[0] -> +
ans[1] -> -
ans[2] -> /
ans[3] -> *

To reference single items in the array, use brackets with the position of the item you want to reference, starting with 0.

so

string txt = ans[0]; would yield +

and string txt = ans[2]; would yield /

You have the array as String[] ans = {"+","-","/","*"}; that means the index of the array from zero to array.length-1 contains the element you inserted into the array so for getting the element out of the array just iterate the array or simply get the element by the index of the array

for(String value : ans){
            System.out.println(value);
        }

or

for(int i=0;i<ans.length-1;i++){
            System.out.println(ans[i]);
        }

or simple

String value = ans[index];//index must be from 0 to arrayLength-1
System.out.println("value "+value);

ans[0]将返回第一个(0,因为第一个元素以0开头,而不是1开始)数组元素, ans[1]第二个,依此类推。

You select a value from an array by referring to the index of its element. Array elements (the things inside your array), are numbered/indexed from 0 to length-1 of your array.

In this case, if you want to first element of your array you'd do:

ans[0]

If you want the last element of your array:

ans[ans.length-1]

Have a look at this guide for a great intro to Arrays. http://www.cs.cmu.edu/~adamchik/15-121/lectures/Arrays/arrays.html

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