简体   繁体   English

如何从数组中选择一个值?

[英]How can I select a value from an array?

How can I select a value from an array? 如何从数组中选择一个值? Example is this String[] ans = {"+", "-", "/", "*"}; 例如这个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[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: 如果您想使用switch语句获得相同的结果:

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 . 但是,使用switch语句,如果要比较case ans[0]:类的变量case ans[0]:而不是case "*" ,那么你可以使用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] . 当他们选择一个数字时,您可以使用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[indexThatYouWantToaccess]确保数组索引从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. 要引用数组中的单个项目,请使用括号表示要引用的项目的位置,从0开始。

so 所以

string txt = ans[0]; would yield + 会屈服+

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

You have the array as String[] ans = {"+","-","/","*"}; 你有数组作为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 这意味着数组的索引从zeroarray.length-1包含您插入到数组中的元素,以便从元素中获取元素只是迭代数组或者只是通过数组的索引获取元素

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. 数组元素(数组中的内容)从数组的0到length-1编号/索引。

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 http://www.cs.cmu.edu/~adamchik/15-121/lectures/Arrays/arrays.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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