Java 我是编程新手,需要将以下输入存储在一个数组中。有什么建议吗?
下面是我正在使用的代码。我只需要将输入存储在一个数组中,我简直不知所措Java 我是编程新手,需要将以下输入存储在一个数组中。有什么建议吗?,java,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)
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);
}
}
试试这个
//Create an array
int[] arr = new int[3];
//store the values using the index
arr[0] = red;
arr[1] = green;
arr[2] = blue;
祝你好运 试试看
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};
使用ArrayList:
ArrayList<Integer> inputs=new ArrayList<Integer>();
inputs.add(sc.nextInt());
如果一个数组中有三个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;
希望这有帮助 如果知道输入元素的数量,可以像下面那样声明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());
希望这有助于创建一个数组,并使用以下索引存储它们:
//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();
到底是什么问题?谢谢你的帮助!你的建议很有用。谢谢你的帮助!我只是需要看看设置,所以这是非常有用的,再次感谢!这正是我需要的!谢谢你的帮助,非常感谢!谢谢你的评论!这确实帮助我建立了我的阵列。再次感谢!谢谢你的帮助!这对我来说是一种非常有效的方法来适应这个阵列。我真的很感激!谢谢你的帮助!我还没有使用ArrayList,所以这很有趣。再次感谢!你应该接受解决你问题的答案:谢谢你的帮助!我被卡住了,这对我很有用。我很感激!
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;
}
}