所以我正在尝试制作一个让用户输入他/她名字的程序。然后根据它的位置打印出他们名字的字符。
实施例
Enter your name : Joshua
Output : Joossshhhhuuuuuaaaaaa //multiplying the characters of their names.
所以这里是我的代码,因为我还不太熟悉字符串方法。
import java.util.Scanner;
class Namex {
public static void main(String[]args) {
Scanner p = new Scanner(System.in);
String name = "";
int z;
System.out.print("Enter name : ");
name = x.nextLine();
y = name.length()-1;
for(int i=0; i<=y; i++) { //i'm not quite sure what method to use yet
}
} // should i use charAt?
}
答案 0 :(得分:1)
在'for'循环中,您可以将此'for loop'置于打印状态。您应该使用'charAt(k)'来获得第k个位置的字符。
for(int j=0; j<=i; j++){
System.out.print(name.charAt(i));
}
答案 1 :(得分:0)
private String multiplyLetters(String text) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); ++i) {
char c = text.charAt(i);
for (int count = 0; count < i + 1; ++count) {
builder.append(c);
}
}
return builder.toString();
}
答案 2 :(得分:0)
import java.util.Scanner;
class name
{
public static void main(String[]args)
{
Scanner x = new Scanner(System.in);
System.out.print("Enter name : ");
String name = x.nextLine();
for(int i=1; i<=name.length(); i++)
{
for(int s=0;s<i;s++)
{
System.out.print(name.charAt(i-1));
}//End nested for
}//End main for
System.out.println();
}//End Main
}//End Class