英文:
Double any element's value that is less than controlValue. Ex: If controlValue = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}
问题
以下是翻译好的代码部分:
import java.util.Scanner;
public class StudentScores {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_POINTS = 4;
int[] dataPoints = new int[NUM_POINTS];
int controlValue;
int i;
controlValue = scnr.nextInt();
for (i = 0; i < dataPoints.length; ++i) {
dataPoints[i] = scnr.nextInt();
}
// 我的解决方案 //
controlValue = 10;
for (i = 0; i < dataPoints.length; ++i) {
if (dataPoints[i] < controlValue) {
dataPoints[i] = dataPoints[i] * 2;
}
}
for (i = 0; i < dataPoints.length; ++i) {
System.out.print(dataPoints[i] + " ");
}
System.out.println();
}
}
英文:
Hi could someone please explain what I'm doing wrong with this problem. For one of my outputs I'm getting 4,12,9,20 instead of 4,12,18,20.
import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_POINTS = 4;
int[] dataPoints = new int[NUM_POINTS];
int controlValue;
int i;
controlValue = scnr.nextInt();
for (i = 0; i < dataPoints.length; ++i) {
dataPoints[i] = scnr.nextInt();
}
//my solution//
controlValue = 10;
for (i = 0; dataPoints[i] < NUM_POINTS ; ++i) {
if(dataPoints[i] < controlValue) {
dataPoints[i] = dataPoints[i] * 2;
}
}
for (i = 0; i < dataPoints.length; ++i) {
System.out.print(dataPoints[i] + " ");
}
System.out.println();
}
}
答案1
得分: 2
以下是翻译好的内容:
原句:
The line:
for (i = 0; dataPoints[i] < NUM_POINTS ; ++i) {
应更正为:
for (i = 0; i < NUM_POINTS ; ++i) {
即:您不需要检查元素 dataPoints[i]
是否小于 NUM_POINTS
,而只需要检查索引 i
本身是否较小。
英文:
The line:
for (i = 0; dataPoints[i] < NUM_POINTS ; ++i) {
Should actually be:
for (i = 0; i < NUM_POINTS ; ++i) {
I.e: you don't want to check if the element dataPoints[i]
is smaller than NUM_POINTS
but only if the index i
itself is smaller.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论