Java 随机整数的距离公式。未生产的产品
所以,我要做的是计算两点之间的距离。我得到了种子和要运行的模拟的数量。我的代码运行,但不工作?随机生成器不会生成数字并将其添加到数组中。我正在使用r对随机生成器进行播种,并使用n运行模拟 编辑:在我通过控制台输入输入后,程序卡在循环中,它卡在第Java 随机整数的距离公式。未生产的产品,java,Java,所以,我要做的是计算两点之间的距离。我得到了种子和要运行的模拟的数量。我的代码运行,但不工作?随机生成器不会生成数字并将其添加到数组中。我正在使用r对随机生成器进行播种,并使用n运行模拟 编辑:在我通过控制台输入输入后,程序卡在循环中,它卡在第X1[I]=rand.nextInt(1000)行 示例输入 2---t(待测巴黎数) 12087 400---r,n(r=种子变量的数量,n=运行rand的次数) 7418778---r,n(第二个r和n) 示例输出示例输入---(摘自本书) 553.9
X1[I]=rand.nextInt(1000)行代码>
示例输入
2---t(待测巴黎数)
12087 400---r,n(r=种子变量的数量,n=运行rand的次数)
7418778---r,n(第二个r和n)
示例输出示例输入---(摘自本书)
553.994
525.789
根据这些数字,我们使用随机生成器生成X1,X2,Y1,Y2。这些数字将用于距离计算
import java.io.*;
import java.util.*;
public class Distance {
public static void main(String[] args) {
double distance = 0; // initiates all numbers needed and sets to 0)
int r[] = new int[10];
int n[] = new int[10];
int X1[] = new int[10]; // Array initiations
int X2[] = new int[10];
int Y1[] = new int[10];
int Y2[] = new int[10];
Scanner q = new Scanner(System.in);
int t = q.nextInt(); // Scans in t (Number of lines to follow)
for (int i = 0; i < t; i++) {
r[i] = q.nextInt(); // Scans in r (Number of Random Objects)
n[i] = q.nextInt(); // Scans in n (Number of Simulations)
}
for (int i = 0; i < n[i]; i++) {
Random rand = new Random(r[i]);
X1[i] = rand.nextInt(n[i]); // fills index(s) i with Random number
// in X1 values
X2[i] = rand.nextInt(n[i]); // fills index(s) i with Random number
// in X2 values
Y1[i] = rand.nextInt(n[i]); // fills index(s) i with Random number
// in Y1 values
Y2[i] = rand.nextInt(n[i]); // fills index(s) i with Random number
// in Y2 values
distance = Math.sqrt(Math.pow((X2[i] - X1[i]), 2)
+ Math.pow((Y2[i] - Y1[i]), 2));
}
System.out.println(distance);
}
}
import java.io.*;
导入java.util.*;
公共类距离{
公共静态void main(字符串[]args){
double distance=0;//启动所需的所有数字并设置为0)
int r[]=新int[10];
int n[]=新int[10];
int X1[]=new int[10];//数组初始化
int X2[]=新int[10];
整数Y1[]=新整数[10];
整数Y2[]=新整数[10];
扫描仪q=新扫描仪(System.in);
int t=q.nextInt();//在t中扫描(要跟随的行数)
for(int i=0;i
在第二个循环中,0小于t,因此循环控制变量没有执行任何操作。
你需要把它改成i小于t,这样循环就会在我到达t时结束
这解决了无限循环的问题,但我不确定你的程序是否正常工作。
在第二个循环中,每次都替换r和n,因此循环没有任何意义,因为r和n只存储最后的输入
使用以下代码修复此方法
for(int i = 0; i<t; i++) {
r = q.nextInt(); //Scans in r (Number of Random Objects)
n = q.nextInt(); //Scans in n (Number of Simulations)
}
公共课帮助{
public static void main(String[] args)
{
double distance = 0; //initiates all numbers needed and sets to 0)
int r[];
int n[];
int X1[];
int X2[];
int Y1[];
int Y2[];
Scanner q = new Scanner (System.in);
int t = q.nextInt(); //Scans in t (Number of lines to follow)
r = new int[t];
n = new int[t];
X1 = new int[t];
X2 = new int[t];
Y1 = new int[t];
Y2 = new int[t];
for(int i = 0; i<t; i++) {
r[i] = q.nextInt(); //Scans in r (Number of Random Objects)
n[i] = q.nextInt(); //Scans in n (Number of Simulations)
}
for(int i = 0; i < t; i++)
{
Random rand = new Random (r[i]);
X1[i] = rand.nextInt(n[i]); // fills index(s) i with Random number in X1 values
X2[i] = rand.nextInt(n[i]); // fills index(s) i with Random number in X2 values
Y1[i] = rand.nextInt(n[i]); // fills index(s) i with Random number in Y1 values
Y2[i] = rand.nextInt(n[i]); // fills index(s) i with Random number in Y2 values
distance = Math.sqrt( Math.pow((X2 [i] - X1 [i] ), 2) + Math.pow((Y2 [i] - Y1 [i] ), 2) );
System.out.println(distance);
}
}
publicstaticvoidmain(字符串[]args)
{
double distance=0;//启动所需的所有数字并设置为0)
int r[];
int n[];
int-X1[];
int X2[];
int Y1[];
int Y2[];
扫描仪q=新扫描仪(System.in);
int t=q.nextInt();//在t中扫描(要跟随的行数)
r=新整数[t];
n=新整数[t];
X1=新整数[t];
X2=新整数[t];
Y1=新整数[t];
Y2=新整数[t];
对于(int i=0;i而不是读入一个变量,t,要告诉您需要多少额外的变量,您可以执行以下操作:
System.out.println("How many random objects?");
r = q.nextInt(); // Scans in r (Number of Random Objects)
System.out.println("How many simulations?");
n = q.nextInt(); // Scans in n (Number of Simulations)
如果出现问题,这将让您看到每个步骤中发生的情况,并且更清晰。在任何情况下,当您希望用户输入任何内容时,您都应该提示程序用户
此外,第一个for循环需要如下所示:
for (int i = 0; i < t; i++) {
r = q.nextInt(); // Scans in r (Number of Random Objects)
n = q.nextInt(); // Scans in n (Number of Simulations)
}
编辑:基于OP更新的更新
如果每次都要随机化种子,则需要将第二个循环与种子输入相结合。列出了完整的主方法:
public void main() {
double distance = 0; // initiates all numbers needed and sets to 0)
int r = 0;
int n = 0;
Scanner q = new Scanner(System.in);
System.out.println("How many simulations?");
n = q.nextInt(); // Scans in n (Number of Simulations)
int X1[] = new int[n]; // Array initiations
int X2[] = new int[n];
int Y1[] = new int[n];
int Y2[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("How many random objects?");
r = q.nextInt(); // Scans in r (Number of Random Objects)
Random rand = new Random(r);
X1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X1 values
X2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X2 values
Y1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y1 values
Y2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y2 values
distance = Math.sqrt(Math.pow((X2[i] - X1[i]), 2)
+ Math.pow((Y2[i] - Y1[i]), 2));
System.out.println("distance: " + distance);
}
System.out.println(distance);
q.close();
}
public void main(){
double distance=0;//启动所需的所有数字并设置为0)
int r=0;
int n=0;
扫描仪q=新扫描仪(System.in);
System.out.println(“多少次模拟?”);
n=q.nextInt();//以n为单位扫描(模拟次数)
int X1[]=new int[n];//数组初始化
int X2[]=新的int[n];
整数Y1[]=新整数[n];
int Y2[]=新的int[n];
对于(int i=0;i
没有。我没有任何想法。为什么不先发布您遇到的具体问题?“不起作用”非常vague@DavidWallace,t是用来告诉程序要计算多少对数字的距离。一旦人们开始评论和回答,你就不应该一直更改问题。这是对所有评论和回答的嘲弄。没有人能完全分辨出有人在回答或评论哪个版本的问题,没有什么是有意义的。它使问题、所有答案和所有评论完全无用
for (int i = 0; i < n; i++) {
// If you want to make it clearer to read
System.out.println("-------------------");
System.out.println("i: " + i + ", n: " + n);
Random rand = new Random(r);
X1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X1 values
System.out.println("X1[" + i + "]: " + X1[i]);
X2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X2 values
System.out.println("X2[" + i + "]: " + X2[i]);
Y1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y1 values
System.out.println("Y1[" + i + "]: " + Y1[i]);
Y2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y2 values
System.out.println("Y2[" + i + "]: " + Y2[i]);
distance = Math.sqrt(Math.pow((X2[i] - X1[i]), 2)
+ Math.pow((Y2[i] - Y1[i]), 2));
System.out.println("distance: " + distance);
// If you want to make it clearer to read
System.out.println("-------------------");
}
System.out.println("How many random objects?");
r = q.nextInt(); // Scans in r (Number of Random Objects)
System.out.println("How many simulations?");
n = q.nextInt(); // Scans in n (Number of Simulations)
int X1[] = new int[n]; // Array initiations
int X2[] = new int[n];
int Y1[] = new int[n];
int Y2[] = new int[n];
public void main() {
double distance = 0; // initiates all numbers needed and sets to 0)
int r = 0;
int n = 0;
Scanner q = new Scanner(System.in);
System.out.println("How many simulations?");
n = q.nextInt(); // Scans in n (Number of Simulations)
int X1[] = new int[n]; // Array initiations
int X2[] = new int[n];
int Y1[] = new int[n];
int Y2[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("How many random objects?");
r = q.nextInt(); // Scans in r (Number of Random Objects)
Random rand = new Random(r);
X1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X1 values
X2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in X2 values
Y1[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y1 values
Y2[i] = rand.nextInt(1000); // fills index(s) i with Random number
// in Y2 values
distance = Math.sqrt(Math.pow((X2[i] - X1[i]), 2)
+ Math.pow((Y2[i] - Y1[i]), 2));
System.out.println("distance: " + distance);
}
System.out.println(distance);
q.close();
}