gpt4 book ai didi

java - 如何在 for 循环输出的同一行上打印一行文本

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:33 24 4
gpt4 key购买 nike

我的代码行如下。我试图让星号打印在与其相关的商店#旁边。现在,例如,如果我为商店 1 输入 200,它将输出:

Store 1:  
**

但我希望它输出:

Store 1: **

我该怎么做?

  int store1;
System.out.print("Enter today's sales for store 1 in dollars: $");
store1 = keyboard.nextInt();

int store2;
System.out.print("Enter today's sales for store 2 in dollars: $");
store2 = keyboard.nextInt();

int store3;
System.out.print("Enter today's sales for store 3 in dollars: $");
store3 = keyboard.nextInt();

int store4;
System.out.print("Enter today's sales for store 4 in dollars: $");
store4 = keyboard.nextInt();

int store5;
System.out.print("Enter today's sales for store 5 in dollars: $");
store5 = keyboard.nextInt();

System.out.println("SALES BAR CHART");

System.out.println("Store 1:");

for (int a = 0; a <= store1; a += 100)
{
System.out.print("*");
}

System.out.print("\n");
System.out.println("Store 2:");

for (int b = 0; b <= store2; b += 100)
{
System.out.print("*");
}

System.out.print("\n");
System.out.println("Store 3:");

for (int c = 0; c <= store3; c += 100)
{
System.out.print("*");
}

System.out.print("\n");
System.out.println("Store 4:");

for (int d = 0; d <= store4; d += 100)
{
System.out.print("*");
}

System.out.print("\n");
System.out.println("Store 5:");

for (int e = 0; e <= store5; e += 100)
{
System.out.print("*");
}

System.out.print("\n");
System.out.print("Each * represents $100");

最佳答案

System.out.println("Store 1:"); 应该是 System.out.print("Store 1:"); 正如您可能知道的,println 打印出文本,然后转到新行。

关于java - 如何在 for 循环输出的同一行上打印一行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com