简体   繁体   中英

Required: Variable, Found Value (array trouble shooting)

import java.util.*;
public class WhileSum_Swigart2
{
    public static void main(String[]args)
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("Hello, please input how many integers will be added: Ex: 7");
        int amountMustTake = scan.nextInt();
        System.out.println("Enter values:");
        int array[] = new int [amountMustTake];
        for (int i =0; i<=amountMustTake;i++)
        {
            array [i]=scan.nextInt();
        }
        for (int i =0; i<=amountMustTake;i++)
        {
            System.out.println("Values are:"=array[i]);  // <-- error on this line
        }
    }
}

I get an error message on my last line saying that it requires a variable, but found a value.

This:

System.out.println("Values are:"=array[i]);

is wrong because you can't assign the value of array[i] to "Values are:" <- this isn't a variable.

So, it should be:

System.out.println("Values are:" + array[i]);

which is appending the value of array[i] to the string and printing both.

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