我是编程新手,需要将下面的输入存储在一个数组中。有什么建议?

时间:2014-02-06 04:49:41

标签: java

以下是我正在使用的代码。我只需要将输入存储在一个数组中,我很丢失!

package Code.simpleInput;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.util.Scanner;

public class LEDSetter
    {
    private static Scanner sc;

public static void main(final String[] args)
    {
    // Instantiating the Finch object
    Finch myFinch = new Finch();
    sc = new Scanner(System.in);

    // Providing instructions to the user

    System.out.println("Enter the red, green, and blue intensity for the LED (values from 0 to 255)");

    // Reading in the three integers
    System.out.print("Red: <=200 ");
    int red = sc.nextInt();
    System.out.print("Green: <=250 ");
    int green = sc.nextInt();
    System.out.print("Blue: <=250 ");
    int blue = sc.nextInt();

    /* Potential improvement here - check the user input to make sure that it is in range (0-255) */

    // Setting the LED

    System.out.println("Thanks, the beak will now glow for 8 seconds according to your specifications");

    myFinch.setLED(red,green,blue);
    myFinch.sleep(8000);

    // Always end your program with finch.quit()

    myFinch.quit();
    System.exit(0);
    }
}

7 个答案:

答案 0 :(得分:0)

试试这个

//Create an array
int[] arr = new int[3];

//store the values using the index
arr[0] = red;
arr[1] = green;
arr[2] = blue;
祝你好运!

答案 1 :(得分:0)

尝试,

System.out.print("Red: <=200 ");
int red = sc.nextInt();
System.out.print("Green: <=250 ");
int green = sc.nextInt();
System.out.print("Blue: <=250 ");
int blue = sc.nextInt();

int colors[] ={red,green,blue};

答案 2 :(得分:0)

使用ArrayList:

ArrayList<Integer> inputs=new ArrayList<Integer>();
inputs.add(sc.nextInt());

答案 3 :(得分:0)

如果要在数组中有三个int值,那么

int[] inputs = new int[3];
...
...   
sc = new Scanner(System.in);    
// Providing instructions to the user    
System.out.println("Enter the red, green, and blue intensity for the LED (values from 0 to 255)");    
// Reading in the three integers    
System.out.print("Red: <=200 ");    
int red = sc.nextInt();    
inputs[0] = red;
System.out.print("Green: <=250 ");    
int green = sc.nextInt();    
inputs[1] = green;    
System.out.print("Blue: <=250 ");    
int blue = sc.nextInt();      
inputs[2] = blue;

希望这有帮助!

答案 4 :(得分:0)

如果你知道输入元素的数量,你可以声明一个如下所示的int数组。

      int[] inputArray = new int[size]; 
      int[0] = sc.nextInt();
      int[1] = sc.nextInt(); 
      int[2] = sc.nextInt();  

现在您可以使用您的阵列了。但是如果您不确定元素的数量,那么请使用ArrayList。这里的类型应该是一个对象而不是一个原语。

      List<Integer> list = new ArrayList<Interger>();  
      list.add(sc.nextInt());  

希望这有帮助

答案 5 :(得分:0)

创建一个数组并使用索引存储它们:

//Create the array

int[] arr = new int[3];

//Store the values using the index

arr[0] = sc.nextInt();

arr[1] = sc.nextInt();

arr[2] = sc.nextInt();

答案 6 :(得分:0)

    public enum ColorEnum 
    {
      Red(getValue("Red","200")),
      Green(getValue("Green","250")),
      Blue(getValue("Blue","250"));

      final int value;
    private static int getValue(String Color,String Range)
    {
      Scanner sc = new Scanner(System.in);
      System.out.println(Color+" : <="+Range);
      return sc.nextInt();
    }


       ColorEnum(int value) {
          this.value = value;
       }
    }