- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我正在制作一个程序,它也可以制作垂直线、水平线和对角线!我对我的一个没有任何意义的输出感到困惑。
所以我的伪代码是这样的:
//enter a char
//enter a number that will determine how long the line is
//define with easyreader what type of line it will be (hori, vert, diag)
//the idea of making the diag lines was this...
@
(two spaces) @
(four spaces) @
(six spaces) @
//we could use the sum spaces = spaces + 2; to keep on calculating what
//the previous spaces was
代码是:
class starter {
public static void main(String args[])
{
System.out.print("What char would you like? ");
EasyReader sym = new EasyReader();
String chars = sym.readWord();
System.out.print("How long would you like it to be? ");
int nums = sym.readInt();
System.out.print("Diag, Vert, or Hori? ");
//you want to read the __ varible, not the sym.readX()
String line = sym.readWord();
System.out.println("");
System.out.println("");
if(line.equals("Hori")){
for(int x = 0; x < nums; x++){
System.out.print(chars + " ");
}
}
else if(line.equals("Vert")){
for(int y = 0; y < nums; y++){
System.out.println(chars + " ");
}
}
else{
for(int xy = 0; xy < nums; xy++){
for(int spaces = 0; spaces < nums; spaces++){
spaces = spaces + 2;
System.out.print(spaces + " ");
System.out.println(chars);
}
}
}
}
}
在底部,您将看到一个名为 xy 的 for 循环,它将读取行的长度。在那个 for 循环下将控制空格。但是,由于某种原因,总和没有正确更新。输出总是:
2 (char)
5 (char)
8 (char)
2 (char)
5 (char)
8 (char)
...
输出应该是:
2 (char)
4 (char)
8 (char)
...
编辑 **********因为我现在需要帮助来改变增量,所以这里有一个例子(所以我不必在评论中解释太多)
示例:如果用户放置他想要多达 5 个单位的行。有两个 for 循环,一个控制他想要多少个空格,一个控制打印出多少个字符,那么输出将是 2、4、6、8、10。
最佳答案
在for
循环语句,你说'增加spaces
每次迭代后加一”(spaces++
):
for(int spaces = 0; spaces < nums; spaces++){
在循环体中,您还要求将它增加 2:
spaces = spaces + 2;
因此每次迭代它都会增加 3。
顺便说一下,您的嵌套循环似乎有问题(如果我理解正确的话)。如果外层循环(遍历 xy
)在每次迭代中画一条线,那么应该为当前行输出缩进的内层循环必须以 xy
为界。 (乘以 2)而不是 nums
.我会这样写:
for (int xy = 0; xy < nums; xy++) {
for (int spaces = 0; spaces < xy*2; spaces += 2) {
System.out.print(" ");
}
System.out.println(chars);
}
关于java - 使用 for 循环更新总和(例如 spaces = spaces + 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46042396/
我系统上的docker info命令输出如下: # docker info Containers: 0 Images: 0 Storage Driver: devicemapper ..... D
我如何捕获用户按下 ctrl + space + space? 我用这段代码成功测试了ctrl + space: RegisterHotKey(0, 1, MOD_CONTROL, VK_SPACE)
我有一个 Matlab 图,我想在论文中使用。此图包含多个 cdfplots。现在的问题是我不能使用标记,因为在情节中变得非常密集。如果我想让样本稀疏,我必须从 cdfplot 中删除一些样本,这将导
我正在尝试获取我将用于备份的服务器上所有数据库的列表。以下是我尝试打印数据库列表但出现错误的代码片段。如何解决?添加双方括号并不能解决问题。 我已经研究了一些类似的问题,但我无法弄清楚。 grep:字
我了解 new gen/old gen/perm gen 之间的区别,但我不知道“To Space”和“From Space”是什么。我看到我的“From Space”使用率达到 99.8%,而“To
我当前有一个返回时看起来像这样的字符串: //This is the url string // the-great-debate---toilet-paper-over-or-under-the-r
我太亲密了。我正在尝试编写用于Notepad++的正则表达式表达式,以用空格替换破折号,而忽略已经用前置/后置空格代替的破折号。我知道我可以用“foobarfoo”搜索/替换“-”,然后搜索“-”替换
我的 ANTLR 代码如下: LPARENTHESIS : ('('); RPARENTHESIS : (')'); fragment CHARACTER : ('a'..'z'|'0'..'9'|)
在过去的 6 个小时里,我一直在尝试在我的 webgl 应用程序中实现点击,但我找不到任何关于这个主题的足够清晰的内容。 到目前为止,我想出的是伪代码: screenSpace = mousePosi
如何使用正则表达式来测试空格或制表符,但不测试换行符? 我尝试了 \s,但我发现它也测试换行符。 我使用C# (.NET) 和 WPF ,但这应该不重要。 最佳答案 使用字符类:[\t] 关于rege
我想解析以下内容: name:name 名称以字母开头和结尾,并且可以包含字母和空格的任意组合。它们也可以是空白的。我的规则是: identifier = alnum (space* alnum)*;
有什么办法可以让 Eden 空间按比例大于 Tenured 空间吗?有 NewRatio 但它以相反的方式工作(Tenured 比 Eden 大几倍)。 我知道有 -XX:NewSize= 和 -XX
我正在编写一个程序,让人们输入他们的信息(姓名,年龄......)。对于姓名输入,我不希望他们留空,只允许字母和空格,但不能以空格开头。合适的正则表达式是什么?我尝试使用: ^[a-zA-Z\\s]*
好的,我正在制作一个程序,它也可以制作垂直线、水平线和对角线!我对我的一个没有任何意义的输出感到困惑。 所以我的伪代码是这样的: //enter a char //enter a number
当我使用 scp 从服务器复制文件时, 我错误地使用了命令 scp xxx@xxxx:xx.zip . 当我完成后,我发现该文件的名称是 .我无法解压 这是目录列表。 http://pfil
我需要一个 bat 文件来获取 Windows 系统中 C:\驱动器的总空间和可用空间(以 GB(千兆字节)为单位),并创建一个包含详细信息的文本文件。 注意:我不想使用任何外部实用程序。 最佳答案
作为预处理我的数据的一部分。我希望能够替换空格后跟数字,同时保留空格后跟一个字符。例如: Input String: '8.1.7 Sep 2000 Dec 2004 Dec 2006 Indefin
我正在编写一个 XPath 表达式,但我修复了一个奇怪的错误,但是以下两个 XPath 表达式之间有什么区别? "//td[starts-with(normalize-space()),'Posted
在 C 中,当读取文本文件时,是否有可能接受一个未知大小的整数值(假设它适合 int),因为它在数字之前和数字之后都有一个空格。例如。 363 865我想分别存储 363 和 865,因为它们有自己的
在 sys/ptrace.h 中,我看到类似这样的内容: @define PT_READ_I 1 /* read world in child's I space*/ @define PT_READ
我是一名优秀的程序员,十分优秀!