我是Java编程的初学者,我想根据用户输入打印字符,但是我没有得到正确的输出。
String z;
double x;
double c;
double v;
System.out.print("Input character(s): ");
z = MNL.next();
System.out.print("Input range: ");
v = Integer.parseInt(reader.readLine());
for(x=1; x<=v; x++)
{
for(c=1; c<=x; c++)
{
System.out.print(" "+z);
}
System.out.println(" ");
{
for(x=1; x>v; x--)
{
for (c=1; c<x; c++)
{
System.out.print(" "+z);
}
System.out.println(" ");
}
}
}
}
我得到的输出:
Input character(s): JSP
Input range: 10
JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
JSP JSP
输出应为:
Input character(s): JSP
Input range: 10
JSP
JSP JSP
JSP JSP JSP
JSP JSP JSP JSP
JSP JSP JSP JSP JSP
JSP JSP JSP JSP
JSP JSP JSP
JSP JSP
JSP
答案 0 :(得分:0)
我不知道您的算法指定了什么,但这是一种可以产生预期结果的解决方案。
public static void main(String[] args) {
String word;
int range;
Scanner input = new Scanner(System.in);
System.out.println("Input characters");
word = input.nextLine();
System.out.println("Input range");
range = Integer.parseInt(input.nextLine());
//Ascending
int rows = range/2;
for(int i=0; i<rows; i++){
for(int k=0; k<=i; k++){
System.out.print(word + " ");
}
System.out.println();
}
//Descending
for(int i=rows-1; i>0; i--){
for(int k=0; k<i; k++){
System.out.print(word + " ");
}
System.out.println();
}
}