数组和方法问题(JAVA)
分配是接受数组“a”,从“a”中提取备用值,将它们以相反顺序存储在另一个数组“b”中,并打印“b”的值。 我编写了以下代码,但打印的“b”的值都是0数组和方法问题(JAVA),java,arrays,methods,reverse,alternate,Java,Arrays,Methods,Reverse,Alternate,分配是接受数组“a”,从“a”中提取备用值,将它们以相反顺序存储在另一个数组“b”中,并打印“b”的值。 我编写了以下代码,但打印的“b”的值都是0 import java.io.*; public class Assignment { public int[] array(int[] a) { int l=a.length; int x=l-1; int[] b=new int[l]; for(int i=x;i&
import java.io.*;
public class Assignment
{
public int[] array(int[] a)
{
int l=a.length;
int x=l-1;
int[] b=new int[l];
for(int i=x;i>=0;i=-2)
{
b[x-i]=a[i];
}
return b;
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Assignment asg=new Assignment();
System.out.println("How many numbers do you want in the array?");
int l=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers");
int[] a =new int[l];
for(int i=0;i<l;i++)
a[i]=Integer.parseInt(br.readLine());
int[] b=asg.array(a);
for(int j=0;j<l;j++)
System.out.println(b[j]);
}
}
import java.io.*;
公共课堂作业
{
公共int[]数组(int[]a)
{
int l=a.长度;
int x=l-1;
int[]b=新的int[l];
对于(int i=x;i>=0;i=-2)
{
b[x-i]=a[i];
}
返回b;
}
公共静态void main()引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
分配asg=新分配();
System.out.println(“您希望数组中有多少个数字?”);
int l=Integer.parseInt(br.readLine());
System.out.println(“输入数字”);
int[]a=新的int[l];
对于(int i=0;i主方法签名不正确,请检查此i=-2
条件。主方法签名必须如下所示:
public static void main(String s[]){
....
}
这里,在for()
循环中,i应该递减1
public int[] array(int[] a)
{
int l=a.length;
int x=l-1;
int[] b=new int[l];
for(int i=x;i>=0;i--) // decrement i by 1;
{
b[x-i]=a[i];
}
return b;
}
首先,您的prgram无法编译-main()方法具有错误的签名。
使用
然后将在新数组中存储值的循环更改为:
for(int i = x; i >= 0; i--) {
b[x - i] = a[i];
}
固定主方法签名后,将代码更改为:
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MainClass asg=new MainClass();
System.out.println("How many numbers do you want in the array?");
int l=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers");
int[] a =new int[l];
for(int i=0;i<l;i++) a[i]=Integer.parseInt(br.readLine());
int[] b=asg.array(a);
int newSize = 0;
if(l% 2 == 0)
newSize = l/2;
else
newSize = (l/2) +1;
for(int j=0;j<newSize;j++) System.out.println(b[j]);
}
catch(Exception ex)
{
ex.printStackTrace();
}
public int[] array(int[] a)
{
int l=a.length;
int x=l-1;
int newSize = 0;
if(l% 2 == 0)
newSize = l/2;
else
newSize = (l/2) +1;
int[] b=new int[newSize];
int i = 0;
while(x >= 0)
{
b[i]=a[x];
i++;
x -= 2;
}
return b;
}
试试看
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
MainClass asg=新的MainClass();
System.out.println(“您希望数组中有多少个数字?”);
int l=Integer.parseInt(br.readLine());
System.out.println(“输入数字”);
int[]a=新的int[l];
for(int i=0;iI)没有足够的“声誉”来否决投票。我也不知道是谁否决了你,也不知道为什么。代码运行得很好,谢谢。尽管我删除了catch(Exception ex)块,因为我遇到了一个错误,“try”语句缺失,我们不允许使用教学大纲以外的任何内容(高中教育有时是有限制的)。无论如何,我感谢你的努力。
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MainClass asg=new MainClass();
System.out.println("How many numbers do you want in the array?");
int l=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers");
int[] a =new int[l];
for(int i=0;i<l;i++) a[i]=Integer.parseInt(br.readLine());
int[] b=asg.array(a);
int newSize = 0;
if(l% 2 == 0)
newSize = l/2;
else
newSize = (l/2) +1;
for(int j=0;j<newSize;j++) System.out.println(b[j]);
}
catch(Exception ex)
{
ex.printStackTrace();
}
public int[] array(int[] a)
{
int l=a.length;
int x=l-1;
int newSize = 0;
if(l% 2 == 0)
newSize = l/2;
else
newSize = (l/2) +1;
int[] b=new int[newSize];
int i = 0;
while(x >= 0)
{
b[i]=a[x];
i++;
x -= 2;
}
return b;
}