[英]How to Repeat Characters of Strings using For Loops in Java
I'm trying to figure out how to take a word as input from a user, then return that same string in a different format. 我试图弄清楚如何将单词作为用户的输入,然后以不同的格式返回相同的字符串。
For example, the input "dog" needs to show up as 例如,输入“ dog”需要显示为
d d
oo OO
ggg GGG
I can only use for-statements, not if-statements...and the program is supposed to only have 2 methods, the second one should return a value. 我只能使用for语句,不能使用if语句...并且该程序应该只有2个方法,第二个应该返回一个值。
I know how to take the input from the user, but I'm a bit lost on how to return a string that's formatted like the one above. 我知道如何从用户那里获取输入,但是我对如何返回格式如上的字符串有点迷茫。
This IS a homework question, so I'm not expecting anyone to just give me the answer. 这是一个作业问题,所以我不希望有人给我答案。 I'm really just stuck and have no idea what to do next.
我真的只是被卡住了,不知道下一步该怎么做。 It's a basic java 1 class, so I can't use anything too complicated.
这是基本的Java 1类,因此我不能使用任何过于复杂的方法。
Here's my unfinished code so far. 到目前为止,这是我未完成的代码。 I know it has a ton of problems >.<
我知道它有很多问题>。<
import java.util.Scanner;
public class Lab06Edit {
public static void main(String[] args ) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a word: ");
String input = keyboard.next();
wordAngle(input);
System.out.print("" + wordAngle(input));
}
public static String wordAngle(String word) {
String result = "";
for (int i=0; i<=input.length(); i++) {
System.out.println(input.charAt(i)*i +"\n");
return result;
}
}
}
If you multiply char
with integer than char will be converted to ASCII
code and result will be integer
. 如果将
char
与整数相乘,则char将转换为ASCII
代码,结果将为integer
。 You should concat
char with String
to produce your desired result. 你应该
concat
字符与String
产生你想要的结果。 Look at your expected output code in below: 在下面查看您的预期输出代码:
public class Lab06Edit {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a word: ");
String input = keyboard.next();
wordAngle(input);
}
public static void wordAngle(String input) {
for (int i = 0; i < input.length(); i++) {
String result = "";
for (int j = 0; j <= i; j++) {
result += input.charAt(i);
}
System.out.println(result);
}
}
}
input.charAt(i)*i
is a little surprising at first but you are trying to multiple a letter. input.charAt(i)*i
有点令人惊讶,但是您尝试将一个字母input.charAt(i)*i
多个。 Unsurprisingly java doesn't like this and converts the letter to a number before multiplying it. 毫不奇怪,java不喜欢这样,并且在将字母乘以之前将其转换为数字。 Hence (after removing a few bugs) an input of dog gives an output of.
因此(删除了一些错误之后)dog的输入给出了输出。 To an extent this is Java's fault as
string + string
works. 在某种程度上,这是Java的错,因为
string + string
有效。
0 % d=100, 0*100=0
111 % 0=111 1*111=111
206 % g=103 2*103=206
If you want to print something multiple times, then the easiest way (not necessarily the best way, but this is homework after all) is to put it in a for loop, you can use System.out.print rather than System.out .println to avoid advancing the line. 如果要多次打印某些内容,那么最简单的方法(不一定是最佳方法,但这毕竟是作业)是将其放入for循环中,可以使用System.out.print而不是System.out。 println避免前进行。 In your case a for loop within a for loop.
在您的情况下,for循环中的for循环。 Alternatively you can build a string piece by piece and
+
them together in a loop (still not best but better) and return that string to be printed within a println statement. 或者你可以建立一块串片和
+
在循环在一起(仍然没有最好,只有更好),并返回该字符串被一个println语句中打印。 Give it a go and see if you can figure it out, ask if you need more of a hint. 试一试,看看是否可以解决,询问是否需要更多提示。
for (int i=0; i<=input.length(); i++)
remember everything is zero based in java, so if something has length 3 it has elements 0,1,2. for (int i=0; i<=input.length(); i++)
记住在Java中一切都是零,所以如果某东西的长度为3,则它的元素为0,1,2。 You current code has 0,1,2,3. 您当前的代码为0、1、2、3。 When your code gets to the mythical element 3 it returns a
String index out of range
exception. 当您的代码到达神话元素3时,它将返回
String index out of range
的String index out of range
异常。
You've put the return statement inside the for loop, I presume a mistake, and you've also changed from using variable name word to input, again I assume a typo 您已将return语句放入for循环中,我想是一个错误,并且您也已从使用变量名word更改为输入,再次假设输入错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.