Java 如何解决这个数组和循环?正确的输出,但
我的目标是编写一个循环,将新闻核心设置为左移的旧分数,并将元素0复制到末尾 编辑:这是我的主要问题,我初始化了Java 如何解决这个数组和循环?正确的输出,但,java,arrays,loops,Java,Arrays,Loops,我的目标是编写一个循环,将新闻核心设置为左移的旧分数,并将元素0复制到末尾 编辑:这是我的主要问题,我初始化了新闻核心[3]=oldScores[0]但问题是,当没有旧分数[4]或新闻核心[4]时,for循环会生成i=4,但由于我将新闻核心[3]初始化为旧分数[0],它会编译并运行,但问题是[4]根本不在数组中。我如何解决这个问题?我离得那么近,却离得那么远,这让我心烦 例如: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30,
新闻核心[3]=oldScores[0]代码>但问题是,当没有旧分数[4]或新闻核心[4]时,for循环会生成i=4
,但由于我将新闻核心[3]初始化为旧分数[0],它会编译并运行,但问题是[4]根本不在数组中。我如何解决这个问题?我离得那么近,却离得那么远,这让我心烦
例如:
If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}
有趣的是,我有正确的输出,但我使用的学习网站告诉我,我有正确的输出,但它也显示“运行时错误(通常是由于无效的数组/向量访问,除以0等)。测试中止。”
公共班级学生成绩{
公共静态void main(字符串[]args){
最终int分数_SIZE=4;
int[]旧分数=新整数[分数大小];
int[]新闻核心=新的int[分数大小];
int i=0;
旧分数[0]=10;
旧分数[1]=20;
旧分数[2]=30;
旧分数[3]=40;
新闻核心[3]=旧分数[0];
对于(i=0;i,在第二个for循环中,在使用它之前先增加i,即+i
这意味着,我将在循环中System.out之前递增。由于您将i初始化为0,它将打印的第一个索引为1(不是0)。它将尝试打印
newScores[1]
newScores[2]
newScores[3]
newScores[4]
最后一个将导致异常(无效索引)。将for循环更改为
for (i = 0; i < SCORES_SIZE; i++) {
for(i=0;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;
}
}
公共班级学生成绩{
公共静态void main(字符串[]args){
最终int分数_SIZE=4;
int[]旧分数=新整数[分数大小];
int[]新闻核心=新的int[分数大小];
int i=0;
旧分数[0]=10;
旧分数[1]=20;
旧分数[2]=30;
旧分数[3]=40;
新闻核心[3]=旧分数[0];
对于(i=0;i我注意到您以错误的方式使用i++和++i。
第一个循环:新闻核心数组索引从0增加到2,每次增加1。
第二次循环:您试图打印新闻核心,但使用++i循环。这意味着新闻核心索引将从1打印到3。
=>索引3处的错误。
解决方案:用
for(i=0;i
只需对代码进行简单更改即可
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();
oldScores[0]=10;
旧分数[1]=20;
旧分数[2]=30;
旧分数[3]=40;
新闻核心[3]=旧分数[0];
对于(i=0;i以克服“运行时错误(通常由于无效的数组/向量访问,除以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];
}
}
for(i=0;i
这是将通过所有测试的答案:
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] + " ");
}
newScores[newScores.length-1]=oldScores[0];
对于(i=0;i
为什么要重新发明轮子?Java API已经允许做这种事情了
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次运行测试
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));
}
就是这样:
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];
}
}
for(i=0;i0){
新闻核心[i+1]=旧分数[i];
}
//这将使第一个值移动并成为新闻核心的最后一个值,而不考虑分数大小值
否则{
newScores[newScores.length-1]=旧分数[i];
}
}
请用适当的语言标记问题请标记解决问题的答案。哦,伙计们,我忘了在这个练习中提到这一点。我唯一可以放置我的答案的地方是oldScores[3]=40;在(i=0;ifor (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];
}
}