首页 \ 问答 \ 如何让代码在数组中将值(x * 2)加倍小于10?(How to get code to double values (x * 2) less than 10 in an array? (Java))

如何让代码在数组中将值(x * 2)加倍小于10?(How to get code to double values (x * 2) less than 10 in an array? (Java))

我正在上一个在线Java编程课程(我是初学者),我无法弄清楚如何正确完成代码。 我已经写了我认为要包含的内容,但是我遗漏了一些会让代码完全运行的东西。

方向:将任何元素的值加倍,小于minVal。 例如:如果minVal = 10 ,则dataPoints = {2, 12, 9, 20}变为{4, 12, 18, 20} dataPoints = {2, 12, 9, 20} {4, 12, 18, 20}

public class StudentScores {
   public static void main (String [] args) {
      final int NUM_POINTS = 4;
      int[] dataPoints = new int[NUM_POINTS];
      int minVal = 0;
      int i = 0;

      dataPoints[0] = 2;
      dataPoints[1] = 12;
      dataPoints[2] = 9;
      dataPoints[3] = 20;

      minVal = 10;
    // DO NOT EDIT ANYTHING ABOVE THIS COMMENT. ABOVE IS THE DEFAULT CODE OF ASSIGNMENT

      for (i = 0; dataPoints[i] < minVal; ++i) {
         dataPoints[i] = dataPoints[i] * 2;
      }

    // DO NOT EDIT ANYTHING BELOW THIS COMMENT. BELOW IS DEFAULT CODE OF ASSIGNMENT
      for (i = 0; i < NUM_POINTS; ++i) {
         System.out.print(dataPoints[i] + " ");
      }
      System.out.println();

      return;
   }
}

我评论了什么是不可混淆的,因为我使用的是在线交互式书籍,无法编辑或更改默认代码。 另外,我们在本章中使用数组。

我是这个网站的新手,因为我不知道stackoverflow的不成文方式,但我在网上多次搜索,看看如何在java中加倍数组的值,但我没有得到搜索结果我需要帮助我。

我得到的错误消息是:

Testing minVal = 10 and dataPoints = {2, 12, 9, 20}
Expected output:  4 12 18 20 
Your output:  4 12 9 20 

我认为编译器应该能够完全读取行以注册9也小于10,因此应该加倍到18,但事实并非如此。 我还缺少什么才能阅读9?


I'm taking an online Java programming class (I'm a beginner) and I cannot figure how to correctly complete the code. I've already written what I think is to be included, however I'm missing something that would make the code work completely.

Directions: Double any element's value that is less than minVal. Ex: If minVal = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}.

public class StudentScores {
   public static void main (String [] args) {
      final int NUM_POINTS = 4;
      int[] dataPoints = new int[NUM_POINTS];
      int minVal = 0;
      int i = 0;

      dataPoints[0] = 2;
      dataPoints[1] = 12;
      dataPoints[2] = 9;
      dataPoints[3] = 20;

      minVal = 10;
    // DO NOT EDIT ANYTHING ABOVE THIS COMMENT. ABOVE IS THE DEFAULT CODE OF ASSIGNMENT

      for (i = 0; dataPoints[i] < minVal; ++i) {
         dataPoints[i] = dataPoints[i] * 2;
      }

    // DO NOT EDIT ANYTHING BELOW THIS COMMENT. BELOW IS DEFAULT CODE OF ASSIGNMENT
      for (i = 0; i < NUM_POINTS; ++i) {
         System.out.print(dataPoints[i] + " ");
      }
      System.out.println();

      return;
   }
}

I made comments of what isn't to be messed with, as I am using an online interactive book and cannot edit or change the default code. Additionally, we are working with arrays in this chapter.

I'm new to this site in the sense that I don't know the unwritten ways of stackoverflow, but I've search multiple times online to see how to double the values for an array in java but I do not get the search results I need to help me.

The error messages I get are:

Testing minVal = 10 and dataPoints = {2, 12, 9, 20}
Expected output:  4 12 18 20 
Your output:  4 12 9 20 

I thought that the compiler should be able to read the lines fully to register that 9 is also less than 10 and thus should be doubled to 18, but it doesn't. What am I missing to make 9 be read as well?


原文:https://stackoverflow.com/questions/39719460
更新时间:2021-11-05 06:11

最满意答案

如果我理解正确,问题就在于以下几点:

 <textarea id="txtArea" rows="10" cols="50" 
    ng-model="$parent.comments" type="text"  ng-trim="false"></textarea>
 {{comments}}

{{comments}}没有显示换行符。

但这是自然的HTML行为。 还有自然的HTML解决方案<pre>

 <textarea id="txtArea" rows="10" cols="50" 
    ng-model="$parent.comments" type="text"  ng-trim="false"></textarea>
 <pre>{{comments}}</pre>

http://www.w3schools.com/tags/tag_pre.asp

<pre>标签定义了预格式化的文本。

<pre>元素中的文本以固定宽度字体(通常为Courier)显示,并保留空格和换行符。


If I do understand you correctly, the issue is in these lines:

 <textarea id="txtArea" rows="10" cols="50" 
    ng-model="$parent.comments" type="text"  ng-trim="false"></textarea>
 {{comments}}

that the {{comments}} is not showing line breaks.

But that's natural HTML behaviour. And there is also natural HTML solution <pre>:

 <textarea id="txtArea" rows="10" cols="50" 
    ng-model="$parent.comments" type="text"  ng-trim="false"></textarea>
 <pre>{{comments}}</pre>

see http://www.w3schools.com/tags/tag_pre.asp:

The <pre> tag defines preformatted text.

Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

相关问答

更多
  • 将它保存到数据库时不要执行nl2br 。 当你在HTML中显示文本时,请执行nl2br 。 我强烈建议不要在数据库中存储任何HTML格式(除非您使用的是丰富的HTML编辑器,在这种情况下,它不会是愚蠢的)。 换行符\n只会成为textarea中的换行符。 Don't do nl2br when you save it to the database. Do nl2br when you're displaying the text in HTML. I can strongly recommend to n ...
  • 在rails中使用simple_format帮助器。 http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html 它将解释换行符并插入正确的HTML,以便正确显示文本。 Use the simple_format helper in rails. http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html It will interpret line ...
  • 问题来自于换行符(\ n \ r?)与HTML
    标签不同的事实 var text = document.forms[0].txt.value; text = text.replace(/\r?\n/g, '
    '); Problem comes from the fact that line breaks (\n\r?) are not the same as HTML
    tags var text = document.forms[0].txt.value; text = ...
  • 我认为这就像你说的那样:textarea根据宽度约束来包装单词 - 这意味着你没有换行符可以找到。 您要查找的换行符是字符,并且这些字符尚未插入到您的文本中。 So the way I did it was: Clone the textarea in question - $("#text") into a transparent textarea. Use transparent font. Change id value of clone to, say, $("#newtext") and appe ...
  • 你需要使用nl2br()函数。 它将按照您在文本区域中编写的确切方式格式化输入。 当您检索POST数据时,请写下: echo nl2br($_POST['sendto']); You need to use nl2br() function. It will format your input the exact way you'd written in your text area. When you're retrieving your POST data, write this: echo nl2b ...
  • 要通过AngularJS注入HTML,可以使用ng-bind-html指令代替ng-bind 。 它要求你将ngSanitize模块注入你的应用并首先导入它的JS文件 这样,文本被理解为html。 所以,一个
    标记将被渲染为一个新行。 但是\n不是HTML,所以你可以创建一个过滤器来代替它,像这样的 ...
  • 我有几乎相同的问题,并且无法首先重现它,但后来我想出了原因:在缩小HTML之后,每个textarea将显示在一行中。 似乎有所不同。 您应该考虑原始HTML和缩小HTML之间的区别。 如果您使用的是WYSIWYG-Editor(例如TinyMCE),请不要尝试。 如果没有任何帮助,我会在开头添加一个换行符 - 正如您已经提到的那样。 不幸的是,感觉有点脏。 I had almost the same problem and wasn't ab ...
  • 如果我理解正确,问题就在于以下几点: {{comments}} {{comments}}没有显示换行符。 但这是自然的HTML行为。 还有自然的HTML解决方案