编写一个方法来打印字符串“Name”多次
我正在回答编程入门课程的问题,但在读完我的笔记、Big Java 书并上网查找后,无法找到从哪里开始。
看起来好像应该很简单,但我只需要开始。所有这些都是针对 Java 的,并且正在 Eclipse 中进行。
任务是接收一个名称(“Name”)和一个数字(int x),并在一行上显示“Name”x 次,在另一行上显示 x-1 次,依此类推,直到您仅显示一次名称。看起来它应该是一个反向累加器,但我在启动我的方法时遇到问题。我该如何开始?我知道我不能像在 python 或其他语言中那样在 Java 中乘以字符串,但是如何在不构建数组或输入的情况下打印“名称”x 次
System.out.println("name" + " " + "name" + " "...).
任何建议都值得赞赏。我是这方面的新手。谢谢你!
I am answering a question for an Intro to Programming class and cannot - after reading through my notes, Big Java book, and looking online - find out where to begin.
It seems as though it should be very simple, but I just need to get started. All of this is for Java and is being worked on in Eclipse.
The task is to take in a name ("Name") and a number (int x) and display the "Name" x number of times on one line, x-1 times on another line, and so on and so on until you display the name only once. It seems like it should be a reverse accumulator, but I'm having trouble starting my method. How do I begin? I know I can't multiply strings in Java like you can in python or other languages, but how can I print the "Name" x number of times without building an Array or inputing
System.out.println("name" + " " + "name" + " "...).
Any suggestions are appreciated. I am a novice at this. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您需要一个循环,这是编程语言的常见功能。
仔细看看 java
for
循环。附加提示:
System.out.println("test")
在一行中打印其参数,而System.out.print("test")
不添加测试
后的CR/LF
。You need a loop, a common feature of programming languages.
Have a close look at the java
for
loop.Additional hint:
System.out.println("test")
prints it's argument in a single line whileSystem.out.print("test")
doesn't add aCR/LF
aftertest
.您应该阅读java 流控制语句。
for
和while
结构将允许您做您想做的事情。You should read about java flow control statements. The
for
andwhile
constructs would allow you to do what you want.您的方法获取一个包含名称和计数的字符串。使用一个 for 循环进行计数,并使用另一个 for 循环重复名称值,您应该能够获得所需的输出。
System.out
是一个打印流,它有println()
来输出以换行符结尾的行,还有print()
来输出字符串而不以换行符结束。Your method gets a string with the name and a count. Using one for loop to count and another to repeat the name value, you should be able to get the output you're after.
System.out
is a printstream and it has bothprintln()
to output a line ended with a linebreak andprint()
to output a string without ending it with a linebreak.您可以使用 System.out.print 和 System.out.println 来完成此操作。
这是示例代码:(不是您想要的实际代码,只是一个示例)
希望这有帮助。
You can do this by using
System.out.print
andSystem.out.println
.Here is the sample code: (not the actual code which you want, just a SAMPLE)
Hope this helps.
使用计数到 x 的外循环和计数到 (x - 外循环计数器) 的内循环。请注意常见的“一次性”错误。
Use an outer loop that counts to x and an inner loop that counts to (x - outer loop counter). Be aware of the common "one off" error.
Java 没有内置的字符串重复是正确的。但您可以定义自己的方法来完成此操作。
对于您来说,您想要使用这个“运算符”,而不是将所有内容都放在一大块代码中,这对您来说是个好点。这有助于保持程序的可读性。
It is correct that Java doesn't have built-in String repetition. But you can define your own method that does exactly this.
Good point for you that you want to use this "operator" instead of putting together everything into one large block of code. This helps to keep your programs readable.
第一个想法:您需要 n 行。所以,进入一个循环。
然后:您需要在第 i 行打印字符串 n - i 次。输入嵌套循环:
然后开始打印名称:
注意我没有使用
println
,因为我们希望所有名称都在同一行。现在,当我们打印
n - i
次时,您需要结束该行:关闭我们的外循环:
如果您不知道如何让程序询问名称和整数
n给用户:
First thought: you need
n
lines. So, enter a loop.Then: you need to print on line
i
the stringn - i
times. Enter a nested loop:Then start off printing the name:
Notice I didn't use
println
, because we want all the names on the same line.Now you need to end the line when we printed
n - i
times:Close our outer-loop:
And if you don't know how to make the program ask the name and the integer
n
to the user: