import java.util.Scanner; //import scanner statement public class BarChartProgram { public static void main(String [] args) { Scanner keyboard = new Scanner (System.in); //scanner object int store1Sales; //number of store1 sales int store2Sales; //number of store2 sales int store3Sales; //number of store 3 sales int cols1; //Asterisks line 1 int cols2; //Asterisks line 2 int cols3; //Asterisks line 3 //Prompt store1 sales number System.out.print("Enter today's sales for store 1: "); store1Sales = keyboard.nextInt(); while (store1Sales < 0) //while statement if prompt number is less than 0 { System.out.print("Invalid input - enter a value of zero or greater: "); store1Sales = keyboard.nextInt(); } //Prompt store2 sales number System.out.print("Enter today's sales for store 2: "); store2Sales = keyboard.nextInt(); while (store2Sales < 0) //while statement if prompt number is less than 0 { System.out.print("Invalid input - enter a value of zero or greater: "); store2Sales = keyboard.nextInt(); } //Prompt store3 sales number System.out.print("Enter today's sales for store 3: "); store3Sales = keyboard.nextInt(); while (store3Sales < 0) //while statement if prompt number is less than 0 { System.out.print("Invalid input - enter a value of zero or greater: "); store3Sales = keyboard.nextInt(); } System.out.println(""); System.out.println("SALES BAR CHART\n"); //Print out store1 sales number in asteriks System.out.print("Store 1: "); for (cols1 = 1; cols1 <= store1Sales; ++cols1) { System.out.print("*"); } System.out.println(""); //Print out store2 sales number in asteriks System.out.print("Store 2: "); for (cols2 = 1; cols2 <= store2Sales; ++cols2) { System.out.print("*"); } System.out.println(""); //Print out store3 sales number in asteriks System.out.print("Store 3: "); for (cols3 = 1; cols3 <= store3Sales; ++cols3) { System.out.print("*"); } } }
Edit: I figured out 1st question.
2nd question
How do I make it so replace every tenth asteriks with "X"?
for example, if i enter 15 for store1 sales, it shows like
Store1: *********X*****
I've been on this for a while but just can't figure it out.
this is example of print out result
Enter today's sales for store 1: 9
Enter today's sales for store 2: -2
Invalid input - enter a value of zero or greater: -1
Invalid input - enter a value of zero or greater: 23
Enter today's sales for store 3: 16
SALES BAR CHART
Store 1: *********
Store 2: *********x*********x***
Store 3: *********x******
and currently mine would print it out like
SALES BAR CHART
Store 1: *********
Store 2: ***********************
Store 3: ****************