Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从数组中选择值?_Java_Arrays - Fatal编程技术网

Java 如何从数组中选择值?

Java 如何从数组中选择值?,java,arrays,Java,Arrays,如何从数组中选择值?例如,String[]ans={“+”,“-”,“/”,“*”}然后我想选择“+” ans[0]将返回数组的第一个(0,因为第一个元素以0开头,而不是1)元素,ans[1]第二个元素依此类推。在实现它的方式中,您需要向用户显示如下列表: 1: + 2: - 3: / 4: * 当他们选择一个数字时,您可以使用ans[input-1]来确定操作员。您可以通过get这样的方法来访问它: ans[i]; // for getting first element you shoul

如何从数组中选择值?例如,
String[]ans={“+”,“-”,“/”,“*”}然后我想选择
“+”


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

在实现它的方式中,您需要向用户显示如下列表:

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

当他们选择一个数字时,您可以使用
ans[input-1]

来确定操作员。您可以通过get这样的方法来访问它:

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

ans[indexThatYouWantToaccess]
确保数组索引以0开头

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

若要引用数组中的单个项,请使用括号表示要引用的项的位置,从0开始

所以

string txt=ans[0]将产生
+


string txt=ans[2]
将产生
/

通过引用数组元素的索引从数组中选择一个值。数组元素(数组中的内容)从数组的0到长度1进行编号/索引

在这种情况下,如果要删除数组的第一个元素,请执行以下操作:

ans[0]
如果需要数组的最后一个元素:

ans[ans.length-1]
请阅读本指南,了解阵列的精彩介绍。

您可以使用
ans[0]
表示“+”等

ans[0] = "+";
ans[1] = "-";
ans[2] = "/";
ans[3] ="*";
在您的情况下,此代码将帮助您:

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

   }
如果希望使用
开关
语句获得相同的结果:

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

   }

但是,使用
switch
语句,如果要与
case ans[0]等变量进行比较:
而不是
case“*”
,则可以使用
enum

数组为
String[]ans={“+”、“-”、“/”、“*”}
表示数组的索引从
数组。length-1
包含插入到数组中的元素,因此要从数组中获取元素,只需迭代数组或通过数组的索引获取元素

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


如果我希望用户使用扫描仪输入选择“-”运算符,我该怎么办?请帮帮我,我是java新手(@AngelaVinagrera您需要使用if条件来检查that@AngelaVinagrera当您说“选择要使用的运算符”时,您希望用户输入什么?用户输入1,2,3,4以选择运算符或-+,-,/,*用户将根据自己的选择输入-+,-,/,*运算符。
for(String value : ans){
            System.out.println(value);
        }
for(int i=0;i<ans.length-1;i++){
            System.out.println(ans[i]);
        }
String value = ans[index];//index must be from 0 to arrayLength-1
System.out.println("value "+value);