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}

huangapple go评论56阅读模式
英文:

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 &lt; dataPoints.length; ++i) {
            dataPoints[i] = scnr.nextInt();
        }
        //my solution//
        controlValue = 10;
        for (i = 0; dataPoints[i] &lt; NUM_POINTS ; ++i) {
            if(dataPoints[i] &lt; controlValue) {
                dataPoints[i] = dataPoints[i] * 2;
            }
        }

        for (i = 0; i &lt; dataPoints.length; ++i) {
            System.out.print(dataPoints[i] + &quot; &quot;);
        }
        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] &lt; NUM_POINTS ; ++i) {

Should actually be:

for (i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2020年10月1日 22:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64157782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定