数组元素比较 Java
Array elements Comparison Java
作为一个初学者,我正在做在线问题来理解数组,这不是作业而是练习。任何建议都有帮助!
代码应接受用户输入。
问题:
打印两个 space 分隔的整数,表示 A 和 B 各自获得的比较分数。
- 示例输入
5 6 7
3 6 10
- 示例输出
1 1
说明
在这个例子中:
A = (a0, a1, a2) 其中值为 (5,6,7)
B = (b0,b1,b2) 其中值为 (3,6,10)
比较每个人的得分:
a0 > b0 ==> 所以 A 得到 1 分。
a0 = b0 ==> 没有人得到一分。
b0 > a0 ==> 所以 B 得到 1 分。
A的比较得分为1,B的比较得分为1。因此,我们在一行上打印1 1。
方法一:
首先,我虽然将其实现为二维数组,但由于我不确定在哪里实现比较,我只能做到这一点:
public class CompareElem2DArray{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int array2d [][]= new int[3][3];
System.out.println("Please enter 3 marks for A and 3 marks for B: ");
for(int a = 0; a<3; a++) //row
{
for(int b=0; b<3; b++)//column
{
int array2d[a][b] = in.nextInt();
}
}
for (int column = 0; column<3; column++)
{
for(int row=0; row<3; row++)
{
System.out.println( array2d[column][row]+" ");
}
}
System.out.println();
}
}
方法二:
这是我第二次尝试不使用二维数组。
public class Comparison {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a0 = in.nextInt();
int a1 = in.nextInt();
int a2 = in.nextInt();
int b0 = in.nextInt();
int b1 = in.nextInt();
int b2 = in.nextInt();
int a[] = new int[3];
int b[] = new int[3];
int firstAns = 0;
int secondAns = 0;
for(int i = 0; i<3; i++)
{
int a[i] = in.nextInt();
System.out.println(a[i]);
}
for(int j = 0; j<3; j++)
{
int b[j] = in.nextInt();
System.out.println(b[j]);
}
for(int z = 0; z<3; z++)
{
if(a[z]>b[z])
{
firstAns++;
}
else if(a[z]<b[z])
{
secondAns++;
}
else
{
return;
}
}
System.out.println(firstAns);
System.out.println(secondAns);
}
}
在方法 1 中,我认为您需要的是 [3][2] 或 [2][3] 数组(您有 2 组数据,一组有 3 个数字)。
然后比较进入第二个循环(在这种情况下,array2d 是 [3][2]):
for(int i=0; i<3; i++)
{
//comparison goes here
}
您需要比较 array2d[i][0] 和 array2d[i][1] 的值。
当输入为:5 6 7 3 6 10
你的二维数组是这样的:
5 6 7
3 6 10
所以第二个循环是将 5 与 3、6 与 6 以及 7 与 10 进行比较。
你可以试试
int[] ar = {5,6,7,3,6,10};
int halflen= (ar.length)/2;
int[] result = new int[halflen];
for(int i=0,j=halflen;i<halflen;i++,j++)
{
result[i]=ar[i]-ar[j];
}
现在你有一个包含 3 个结果的数组,如果绘制的是 0,无论 > 0 是如何表达的,你都可以根据需要消除循环内的数组,并添加你的 if there :
If (ar [i] > ar [j]) A++;
If (ar [i]<ar [j]) B++;
作为一个初学者,我正在做在线问题来理解数组,这不是作业而是练习。任何建议都有帮助!
代码应接受用户输入。
问题:
打印两个 space 分隔的整数,表示 A 和 B 各自获得的比较分数。
- 示例输入
5 6 7 3 6 10
- 示例输出
1 1
说明
在这个例子中: A = (a0, a1, a2) 其中值为 (5,6,7)
B = (b0,b1,b2) 其中值为 (3,6,10)
比较每个人的得分:
a0 > b0 ==> 所以 A 得到 1 分。
a0 = b0 ==> 没有人得到一分。
b0 > a0 ==> 所以 B 得到 1 分。
A的比较得分为1,B的比较得分为1。因此,我们在一行上打印1 1。
方法一:
首先,我虽然将其实现为二维数组,但由于我不确定在哪里实现比较,我只能做到这一点:
public class CompareElem2DArray{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int array2d [][]= new int[3][3];
System.out.println("Please enter 3 marks for A and 3 marks for B: ");
for(int a = 0; a<3; a++) //row
{
for(int b=0; b<3; b++)//column
{
int array2d[a][b] = in.nextInt();
}
}
for (int column = 0; column<3; column++)
{
for(int row=0; row<3; row++)
{
System.out.println( array2d[column][row]+" ");
}
}
System.out.println();
}
}
方法二:
这是我第二次尝试不使用二维数组。
public class Comparison {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a0 = in.nextInt();
int a1 = in.nextInt();
int a2 = in.nextInt();
int b0 = in.nextInt();
int b1 = in.nextInt();
int b2 = in.nextInt();
int a[] = new int[3];
int b[] = new int[3];
int firstAns = 0;
int secondAns = 0;
for(int i = 0; i<3; i++)
{
int a[i] = in.nextInt();
System.out.println(a[i]);
}
for(int j = 0; j<3; j++)
{
int b[j] = in.nextInt();
System.out.println(b[j]);
}
for(int z = 0; z<3; z++)
{
if(a[z]>b[z])
{
firstAns++;
}
else if(a[z]<b[z])
{
secondAns++;
}
else
{
return;
}
}
System.out.println(firstAns);
System.out.println(secondAns);
}
}
在方法 1 中,我认为您需要的是 [3][2] 或 [2][3] 数组(您有 2 组数据,一组有 3 个数字)。 然后比较进入第二个循环(在这种情况下,array2d 是 [3][2]):
for(int i=0; i<3; i++)
{
//comparison goes here
}
您需要比较 array2d[i][0] 和 array2d[i][1] 的值。
当输入为:5 6 7 3 6 10
你的二维数组是这样的:
5 6 7
3 6 10
所以第二个循环是将 5 与 3、6 与 6 以及 7 与 10 进行比较。
你可以试试
int[] ar = {5,6,7,3,6,10};
int halflen= (ar.length)/2;
int[] result = new int[halflen];
for(int i=0,j=halflen;i<halflen;i++,j++)
{
result[i]=ar[i]-ar[j];
}
现在你有一个包含 3 个结果的数组,如果绘制的是 0,无论 > 0 是如何表达的,你都可以根据需要消除循环内的数组,并添加你的 if there :
If (ar [i] > ar [j]) A++;
If (ar [i]<ar [j]) B++;