java 我如何解决这个数组和循环?正确的输出但是
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33663329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i solve this array and loop? correct output but
提问by silversk8terz
My objective is to write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end.
我的目标是编写一个循环,将 newScores 设置为向左移动一次的 oldScores,并将元素 0 复制到末尾。
Edit: Here's my main problem, I initialized newScores[3] = oldScores[0];
but the thing is the for loop makes i = 4
when there's no oldScores[4] or newScores[4] but since I initialized newScores[3] to oldScores[0] it compiles and runs, but the problem is that [4] is not in the array at all. How do i get rid of this problem? I'm so close but so far away it's bugging me.
编辑:这是我的主要问题,我进行了初始化,newScores[3] = oldScores[0];
但问题是 for 循环i = 4
在没有 oldScores[4] 或 newScores[4] 时产生,但由于我将 newScores[3] 初始化为 oldScores[0],它编译并运行,但问题是是 [4] 根本不在数组中。我如何摆脱这个问题?我很近,但又很远,这让我很烦。
Example:
例子:
If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}
Funny thing is I have the correct output but the learning website I'm using it tells me that I have the correct output but it also displays this "Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.). Tests aborted.".
有趣的是我有正确的输出,但我正在使用它的学习网站告诉我我有正确的输出,但它也显示了这个“运行时错误(通常是由于无效的数组/向量访问、除以 0 等。 ) 测试中止。”。
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i = 0;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
newScores[3] = oldScores[0];
for(i=0; i<SCORES_SIZE-1; i++){
newScores[i] = oldScores[i +1];
}
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(newScores[i] + " ");
}
System.out.println();
return;
}
}
回答by Nate
The issue is in the second loop where you have ++i. i++ and ++i have two different meanings. Here is a link to describe the meanings to you. What is the difference between ++i and i++?. If you change that, you should not get the error any more. Below is the change in your code.
问题出在您拥有 ++i 的第二个循环中。i++ 和 ++i 有两种不同的含义。这是一个向您描述含义的链接。 ++i 和 i++ 有什么区别?. 如果您更改它,则不应再出现错误。以下是您的代码中的更改。
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i = 0;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
newScores[3] = oldScores[0];
for(i=0; i<SCORES_SIZE-1; i++){
newScores[i] = oldScores[i +1];
}
for (i = 0; i < SCORES_SIZE; i++) {
System.out.print(newScores[i] + " ");
}
System.out.println();
return;
}
}
回答by Kabir
In your second for loop, you are incrementing i prior to using it i.e. ++i
在你的第二个 for 循环中,你在使用它之前增加 i ,即 ++i
What that means is, i will be incremented before your System.out in the loop. Since you intialized i to 0, the first index it will print is of 1 (not 0). It will try to print
这意味着, i 将在循环中的 System.out 之前递增。由于您将 i 初始化为 0,它将打印的第一个索引为 1(不是 0)。它会尝试打印
newScores[1]
newScores[2]
newScores[3]
newScores[4]
The last will result in an exception (invalid index). Change the for loop to
最后一个将导致异常(无效索引)。将 for 循环更改为
for (i = 0; i < SCORES_SIZE; i++) {
(where i is incremented after use)
(其中 i 在使用后递增)
回答by Hoang Tran Son
I noticed that you are using i++ and ++i in wrong way.
First loop: newScores array index from 0 to 2 and increase 1 each time.
Second loop: you are trying to print newScores but loop using ++i. It means that newScores index will be printed from 1 to 3.
=> error at index 3.
Solution: replace second loop byfor (i = 0; i < SCORES_SIZE - 1; i++)
我注意到您以错误的方式使用 i++ 和 ++i。
第一个循环:newScores 数组索引从 0 到 2,每次增加 1。
第二个循环:您正在尝试打印 newScores 但使用 ++i 循环。这意味着 newScores 索引将从 1 打印到 3。=> 索引 3 处的错误。解决方案:将第二个循环替换为for (i = 0; i < SCORES_SIZE - 1; i++)
回答by Doc
Just a simple change to make your code work
只需一个简单的更改即可使您的代码正常工作
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;
newScores[3] = oldScores[0];
for(i=0; i<SCORES_SIZE-1; i++){
newScores[i] = oldScores[i +1];
}
/* REMOVE THIS PART
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(newScores[i] + " ");
}
*/TILL HERE
// USE THE FOLLOWING
for (i = 0; i < SCORES_SIZE; i++) {
System.out.print(newScores[i] + " ");
}
System.out.println();
Because you are increasing the value prior to its use which is causing the error.
因为您在使用它之前增加了导致错误的值。
回答by vickig
In order to overcome the " Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.). Tests aborted." I used an if/else statement:
为了克服“运行时错误(通常是由于无效的数组/向量访问、除以 0 等)。测试中止。” 我使用了一个 if/else 语句:
for (i = 0; i < SCORES_SIZE; i++)
{
if (i == (SCORES_SIZE - 1))
{
newScores[i] = oldScores[0];
}
else
{
newScores[i] = oldScores[i + 1];
}
}
回答by Fred
This is the answer that will pass all the tests:
这是将通过所有测试的答案:
newScores[newScores.length -1] = oldScores[0];
for (i = 0; i < oldScores.length - 1; i++) {
newScores[i] = oldScores[i +1];
}
for (i = 0; i < newScores.length; ++i) {
System.out.print(newScores[i] + " ");
}
回答by Black Lion
Why re-inventing the wheel? Java API already allows doing this kind of stuff
为什么要重新发明轮子?Java API 已经允许做这种事情
1 - create method
1 - 创建方法
public static int[] switchScores(int[] oldScores){
if(oldScores != null && oldScores.length > 0) {
final int SIZE = oldScores.length;
int[] newScores = new int[SIZE];
newScores = Arrays.copyOfRange(oldScores, 1, SIZE+1);
newScores[SIZE-1] = oldScores[0];
return newScores;
}
else{
throw new IllegalArgumentException("Null or Empty Array provided");
}
}
2 - run test
2 - 运行测试
public static void main(String[] args) {
int[] input = new int[]{10, 20, 30, 40};
System.out.println("Old scores : " + Arrays.toString(input));
int[] output = switchScores(input);
System.out.println("New scores : " + Arrays.toString(output));
}
回答by TaeterTot
This is it:
就是这个:
for (i = 0; i < newScores.length; ++i) {
// if statement to shift numbers to the left starting with the 2nd value
if (i > 0) {
newScores[i + 1] = oldScores[i];
}
// This will make the 1st value move and become newScores last value regardless of SCORES_SIZE value
else {
newScores[newScores.length - 1] = oldScores[i];
}
}