java将if语句中的值存储到数组中
我无法将java将if语句中的值存储到数组中,java,arrays,variables,if-statement,static-methods,Java,Arrays,Variables,If Statement,Static Methods,我无法将if语句中的值存储到数组中。例如,我试图将使x平方+y平方小于或等于1的x和y值存储到数组中。这些都是在静态方法中完成的,然后我想将它们调用到我的主方法中。下面是我的代码: 静态方法: // simulation of x values static method public static int xValues(int choice){ int x = (int) Math.random(); return x; } // simulation of y val
if
语句中的值存储到数组中。例如,我试图将使x平方+y平方小于或等于1的x和y值存储到数组中。这些都是在静态方法中完成的,然后我想将它们调用到我的主方法中。下面是我的代码:
静态方法:
// simulation of x values static method
public static int xValues(int choice){
int x = (int) Math.random();
return x;
}
// simulation of y values static method
public static int yValues(int choice){
int y = (int) Math.random();
return y;
}
//////////////// create allowed values for calculation static method
public static int allowedValues(int x, int y){
if((Math.pow(x, 2) + Math.pow(y, 2)) <= 1){
/////////// store in an array
}
}
//模拟x值静态方法
公共静态int xvalue(int选项){
int x=(int)Math.random();
返回x;
}
//y值静态法的仿真
公共静态整数Y值(整数选择){
int y=(int)Math.random();
返回y;
}
////////////////为计算静态方法创建允许的值
公共静态int允许值(int x,int y){
if((数学功率(x,2)+数学功率(y,2))你的问题到底是什么?你试图实现什么?你有任何例外吗?请发布更多详细信息…为什么你要将选择权传递给那些方法?你没有在那些方法中使用它们。假设main
是数组定义的范围。然后在allowedValues
函数中,只需返回>1
或0
(true或false)来确定是否应在数组中存储x
和y
。请注意,您尚未捕获xValues(choice)
或yValues(choice)
的返回值,因此您也需要这些变量……顺便提一下,提示:当您执行(int)Math.random()时
您将得到的唯一数字是0。
// main method
public static void main(String args[]){
// create Scanner object
Scanner in = new Scanner(System.in);
// call to promptUser
promptUser();
// create variable for recieving user input
int choice = in.nextInt();
// for loop
for(int i = 0; i <= choice; i++){
// call to xValues
xValues(choice);
// call to yValues
yValues(choice);
// increment i
i++;
}
}