控制台条形图使用循环,无图形JAVA
目前正在编写一本书循环章节中的示例。基本上,有4个汽车销售商,每个人都应该输入他们售出的汽车数量。在输入的下方,他们的名字应该打印出来,并且与他们销售的汽车的行号相同(来自输入),但不能用数字,使用X或星号*。x或星形表示该数字输入。 这是到目前为止我的代码。我被困在打印X或星星旁边的名字。它打印在错误的名字旁边。有没有办法只使用循环而不使用数组
import java.io.*;
public class BarGraphCarSold {
public static void main(String[] args)throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter no of cars Bob sold >> ");
int bobSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Pam sold >> ");
int pamSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars John sold >> ");
int johnSold = Integer.parseInt(buf.readLine());
System.out.print("Enter no of cars Kim sold >> ");
int kimSold = Integer.parseInt(buf.readLine());
System.out.println();
System.out.println("Car Sales for Month");
System.out.print("Bob \n");
System.out.print("Pam \n");
System.out.print("John \n");
System.out.print("Kim ");
for(int i = 1; i<=bobSold;i++){
System.out.print("X");
}
}
}
OUTPUT
Enter no of cars Bob sold >> 5
Enter no of cars Pam sold >> 7
Enter no of cars John sold >> 4
Enter no of cars Kim sold >> 6
Car Sales for Month
Bob
Pam
John
Kim XXXXX
# 1 楼答案
你很接近
试试这样的
您正在打印每个名称,然后有换行符(
\n
),然后尝试打印X。因此,您需要先打印名称,然后打印X,然后打印新行。对于要用X打印出来的每个名称,都需要这样的循环有很多不同的方法可以做到这一点。你可以有一个带有名字的
String
,然后循环并将X添加到string
,然后一次性打印出来使用数组
玩得开心。继续
# 2 楼答案
你需要为每个人打印X