At the bottom of this is what I have so far...pretty much everything right?...I
ID: 3640400 • Letter: A
Question
At the bottom of this is what I have so far...pretty much everything right?...I think everything is right but I do not have the X's in my chart. How to make the x's show up on "every 10th * "? Thanks in advance.
John
Here is what I am trying to do:
Write a Java program to collect the sales data from three stores of a given company. The program will ask the user to enter the number of items sold at each store. The program should display a bar chart to illustrate the sales at each store. A bar chart is a row of printed asterisks -- each asterisk represents the sale of one item. To make the bar chart easier to read, every tenth asterisk is replaced by an 'x'. Here is a sample run of the program:
Enter today's sales for store 1: 27
Enter today's sales for store 2: 15
Enter today's sales for store 3: 34
SALES BAR CHART
Store 1: *********x*********x*******
Store 2: *********x*****
Store 3: *********x*********x*********x****
Your program should validate the data entered for each store. The store data should be a value zero or greater (i.e., reject negative values). Here is a sample run of the program in which the user attempted to enter invalid data for store 2 -- eventually the user entered the valid value of 23:
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******
Here is what I have so far...I think everything is right but I do not have the X's in my chart. How to make the x's show up on "every 10th * "? Thanks in advance.
John
import java.util.Scanner;
public class MainApplication {
private Scanner in;
private int sales[];
private String input;
public MainApplication() {}
public static void main(String[] args) {
MainApplication inst = new MainApplication();
inst.go();
}
public void go() {
in = new Scanner(System.in);
sales = new int[5];
for(int i=0;i<5;i++) {
System.out.print("Enter today's sales for store " + (i+1) + ": ");
input = in.nextLine();
sales[i] = Integer.parseInt(input);
System.out.print(" ");
}
System.out.println(" Sales chart: ");
for(int j=0; j<5; j++) {
System.out.print("Store " + (j+1) + ": ");
for(int k=0; k<(sales[j]/100);k++)
System.out.print("*");
System.out.print(" ");
}
}
}
Explanation / Answer
If you still need an answer, here it is:
Change the code
for(int j=0; j<5; j++) {
System.out.print("Store " + (j+1) + ": ");
for(int k=0; k<(sales[j]/100);k++)
System.out.print("*");
}
to
for(int j=0; j<5; j++) {
System.out.print("Store " + (j+1) + ": ");
for(int k=0, int l=1; k<(sales[j]/100);k++,l++) {
System.out.print("*");
if (l == 10) {
System.out.print("x");
l = 0;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.