所以,我用15和5个数字做了一个A和B数组,然后我需要将最后5个数字改为零(A [10]到A [14]的元素),然后输入整数K:< / p>
任何人都可以通过第一个项目符号清单项帮助我吗?在我的工作中,我有点卡在这里。我知道这不应该是困难的但是在尝试用任意整数填充前十个数组元素之后我就卡住了..
这是我的代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class Ld3161RDB155
{
public static void main(String[] args)
{
int A[] = new int[15];
int B[] = new int[5];
int K, i;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("161RDB155");
System.out.print("K=");
try
{
K = Integer.parseInt(br.readLine());
}
catch (Exception ex)
{
System.out.println("input-output error");
return;
}
Random r = new Random();
if (K < 0 && K > 9)
{
K = 5;
i = 0;
do
{
A[i] = r.nextInt(51);
i++;
}
while (i < 10);
i = 0;
do
{
B[i] = r.nextInt(50) + 50;
i++;
}
while (i < 5);
}
else
{
i = 0;
do
{
A[i] = i * K;
i++;
}
while (i < 10);
i = 0;
do
{
B[i] = 10 * (i + 1) * K;
i++;
}
while (i < 5);
}
System.out.println("A:");
for (i = 0; i < 15; i++)
{
System.out.print(A[i] + "\t");
if (i == 14)
System.out.println();
}
System.out.println("B:");
for (i = 0; i < 5; i++)
{
System.out.print(B[i] + "\t");
if (i == 4)
System.out.println();
}
}
}
答案 0 :(得分:1)
您的代码似乎正在按计划进行。
替换do-while循环使其更具可读性。
最重要的是,您的第if (K < 0 && K > 9)
行必须是or
声明`||&#39;。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
public class Ld3161RDB155
{
public static void main(String[] args)
{
int a[] = new int[15];
int b[] = new int[5];
int k;
Random r = new Random();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("161RDB155");
System.out.print("K=");
try
{
k = Integer.parseInt(br.readLine());
}
catch (Exception ex)
{
System.out.println("input-output error");
return;
}
if (k < 0 || k > 9)
{
k = 5;
for (int i = 0; i < 10; i++)
{
a[i] = r.nextInt(51);
}
for (int i = 0; i < 5; i++)
{
b[i] = r.nextInt(51) + 50;
}
}
else
{
for (int i = 0; i < 10; i++)
{
a[i] = i * k;
}
for (int i = 0; i < 5; i++)
{
b[i] = 10 * (i + 1) * k;
}
}
System.out.println("A:" + (Arrays.toString(a)));
System.out.println("B:" + (Arrays.toString(b)));
}
}