此Java程序中没有任何问题,唯一的问题是我想知道是否会有更简单的方法仅使用for循环来完成此操作,因为我刚刚开始学习for Java中的-loops。以一种更简单的方式,我的意思是一种使用更少的代码的更有效的方法。
import java.util.Scanner;
public class BarChart
{
public static void main(String[] args)
{
//#Declaring variables
int sales1, sales2, sales3, sales4, sales5;
//#Creating scanner object
Scanner keyboard = new Scanner(System.in);
//#Asking user for input
System.out.print("Enter today's sales for store 1: ");
sales1 = keyboard.nextInt();
System.out.print("Enter today's sales for store 2: ");
sales2 = keyboard.nextInt();
System.out.print("Enter today's sales for store 3: ");
sales3 = keyboard.nextInt();
System.out.print("Enter today's sales for store 4: ");
sales4 = keyboard.nextInt();
System.out.print("Enter today's sales for store 5: ");
sales5 = keyboard.nextInt();
//#Displaying the sales bar chart
System.out.println("\nSALES BAR CHART");
System.out.print("\nStore 1: ");
for (int num = 0; num < sales1; num += 100)
{
System.out.print("*");
}
System.out.print("\nStore 2: ");
for (int num = 0; num < sales2; num += 100)
{
System.out.print("*");
}
System.out.print("\nStore 3: ");
for (int num = 0; num < sales3; num += 100)
{
System.out.print("*");
}
System.out.print("\nStore 4: ");
for (int num = 0; num < sales4; num += 100)
{
System.out.print("*");
}
System.out.print("\nStore 5: ");
for (int num = 0; num < sales5; num += 100)
{
System.out.print("*");
}
}
}
输出应如下所示:
> What the Output should look like:
> Enter today's sales for store 1: 1000
> Enter today's sales for store 2: 1290
> Enter today's sales for store 3: 1850
> Enter today's sales for store 4: 800
> Enter today's sales for store 5: 1900
> SALES BAR CHART
> Store 1: **********
> Store 2: *************
> Store 3: *******************
> Store 4: ********
> Store 5: *******************
答案 0 :(得分:1)
当然,您可以遍历大多数重复性任务:
public static void main(String[] args){
int[] sales = new int[5];
Scanner keyboard = new Scanner(System.in);
//Ask the user for input five times, stored the input in the sales array
for (int i = 0; i < 5; i++){
System.out.print("Enter today's sales for store " + (i+1) ": ");
sales[i] = keyboard.nextInt();
}
//Print the sales
System.out.println("Sales bar chart");
for (int i = 0; i < 5; i++){
printStore(i, sales[i]);
}
}
//helper function that prints a resulting store line
private void printStore(int storeNumber, int sales){
System.out.print("Store " + (i + 1) + ": "):
for (int i = 0; i < sales / 100; i++){
system.out.print("*");
}
System.out.println();
}
请注意,尽管这减少了代码行,但并没有比您的解决方案更有效。
答案 1 :(得分:1)
每次编写同一行代码超过三遍时,都应考虑使用循环。同样,只要发现使用foo1
,foo2
等变量名,您就应该考虑将它们放入数据结构中。如果您使用简单的Array
并进行for循环,则程序会简化为:
int[] stores = new int[5];
Scanner in = new Scanner(System.in);
for(int i = 0; i < stores.length; i++) {
System.out.print("Enter today's sales for store " + (1+i) + ": ");
stores[i] = in.nextInt();
}
for(int i = 0; i < stores.length; i++) {
System.out.print("Store " + (i+1) + ": ");
for(int j =0; j < stores[i]; j++) {
System.out.print("*");
}
System.out.println();
}
样品运行:
Enter today's sales for store 1: 3
Enter today's sales for store 2: 5
Enter today's sales for store 3: 7
Enter today's sales for store 4: 2
Enter today's sales for store 5: 8
Store 1: ***
Store 2: *****
Store 3: *******
Store 4: **
Store 5: ********
答案 2 :(得分:0)
使用基本编码进行编码的一种简单方法是仅在条件条件下使用,并每次将*
附加到String
上,因此您只需要使用一个循环。这是下面的样子。我没有改变您从用户那里获取值的方式。
String count = "", count2 = "", count3 = "", count4 = "", count5 = "";
int max = Collections.max(Arrays.asList(sales1,sales2,sales3,sales4,sales5));
for (int num = 0; num < max; num += 100)
{
if (num < sales1)
{
count = count + "*";
}
if (num < sales2)
{
count2 = count2 + "*";
}
if (num < sales3)
{
count3 = count3 + "*";
}
if (num < sales4)
{
count4 = count4 + "*";
}
if (num < sales5)
{
count5 = count5 + "*";
}
}
System.out.print("\nStore 1: " + count);
System.out.print("\nStore 2: " + count2);
System.out.print("\nStore 3: " + count3);
System.out.print("\nStore 4: " + count4);
System.out.print("\nStore 5: " + count5);
int max
用于查找5个销售数字中的最大值,以便循环正确退出。由于在循环的同一遍中覆盖了多个*
,因此在速度上也是一种更有效的方法。我故意没有不调用方法或使用更复杂的方法来存储值,以使您始终专注于循环,因为您当前正在学习循环。
答案 3 :(得分:0)
如果您想尝试使用lambda(Java 8)
public static void main(String[] args) {
// Declaring steps
int N = 5;
// Declaring increment
int INCREMENT = 100;
// Creating scanner object
Scanner keyboard = new Scanner(System.in);
// Creating a loop from 1 to N (including the limits)
List<Integer> variables = IntStream.rangeClosed(1, N)
// Asking user for input
.peek(index -> System.out.printf("Enter today's sales for store %d: ", index))
// Read the user answer
.mapToObj(dummy -> keyboard.nextInt())
// Collect all the answers in a list
.collect(Collectors.toList());
// Displaying the sales bar chart
System.out.println("\nSALES BAR CHART\n");
// Creating a loop from 0 to N (without N)
IntStream.range(0, N)
// Display the message for each step
.peek(index -> System.out.printf("Store %d: ", index + 1))
// Get the saved value
.map(variables::get)
// Display the stars
.forEach(element -> System.out.println("*".repeat(element / INCREMENT)));
}