2016-03-04 63 views
1

我的代碼行如下。我試圖讓星號在商店旁邊打印,並與之相關聯。眼下,例如,如果我輸入200店1,它會輸出:如何在同一行上打印一行文字作爲循環輸出

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"); 
+0

嘗試'的System.out.println();是System.out.print( 「存儲1:」);'代替'系統.out.println(「Store 1:」);' – Gangaraju

回答

0

System.out.println("Store 1:");應該是System.out.print("Store 1:");正如你可能知道,的println打印出的文本,然後進入一個新的生產線。

+0

非常感謝你 –

+0

不客氣! – Logan

0

使用System.out.print()而不是System.out.println()可以在不打印換行的情況下打印內容。

例如,

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

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

System.out.print("\n"); 

應該是

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

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

System.out.println();