Java,转图,每100个输出为*

Java, transfer graph, output as * to every 100

    import java.util.Scanner;

public class蝙蝠侠条形图{ public static void main(String[] args){

//store the amount of each transfer
 // storage for the variable
 int transfer1;
 int transfer2;
 int transfer3;
 int transfer4;
 int transfer5;

 //greet and ask for the transfers
 System.out.println("Good Morning Lucius. Enter Transfers - ");
 Scanner keyboard = new Scanner(System.in);

 //plug in the transfer
 System.out.println("Transfer 1");

 //keyboard 
 transfer1 = keyboard.nextInt();

 System.out.println("Transfer 2");
 transfer2 = keyboard.nextInt();

 System.out.println("Transfer 3");
 transfer3 = keyboard.nextInt();

 System.out.println("Transfer 4");
 transfer4 = keyboard.nextInt();

 System.out.println("Transfer 5");
 transfer5 = keyboard.nextInt();



//print a graph, line by line of each transfer
//Every asterisk (“*”) in each bar represents 0. A transfer of 00 would then have five stars: *****


 //Create another chart for suspicious transactions
 System.out.println("Suspicious transfer chart: ");
 //divides the number we entered by 100
 int Suspicious1 = (transfer1)/100;
 int Suspicious2 = (transfer2)/100;
 int Suspicious3 = (transfer3)/100;
 int Suspicious4 = (transfer4)/100;
 int Suspicious5 = (transfer5)/100;

 System.out.print("Transfer 1:");
 for (int N1 = Suspicious1; N1 > 0; N1--){
     System.out.print("*");
     }

 System.out.print("Transfer 2: ");
 for (int N2 = Suspicious2; N2 > 0; N2--){
     System.out.print("*");
 }
 System.out.print("Transfer 3: ");
 for (int N3 = Suspicious3; N3 > 0; N3--){
     System.out.print("*");
 }

 System.out.print("Transfer 4: ");
 for (int N4 = Suspicious4; N4 > 0; N4--){
     System.out.print("*");
 }

 System.out.print("Transfer 5: ");

 for (int N5 = Suspicious5; N5 > 0; N5--){
     System.out.print("*");
 }
}

}

这就是我的代码,我得到的输出是

    Suspicious transfer chart: 

转机 1:*转机 2:**转机 3:***转机 4:*****转机 5:*****

我如何让它们变成这样

出于教育目的,此代码实现了您的代码的明显意图。

import java.util.Scanner;

public class BatmanBarGraph {
    public static void main(String[] args)
    {
        System.out.println("Good Morning Lucius. Enter Transfers - ");
        Scanner keyboard = new Scanner(System.in);
        int[] transfers = new int[5];
        for (int i = 0; i < transfers.length; i++) {
            System.out.print("Transfer " + (i + 1) + ": ");
            transfers[i] = keyboard.nextInt();
        }
        System.out.println();
        System.out.println("Suspicious transfer chart:");
        for (int i = 0; i < transfers.length; i++) {
            System.out.print("Transfer " + (i + 1) + ": ");
            for (int n = transfers[i] / 100; n > 0; n--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

首先,请注意它在打印 "Transfer X: ****" 输出时使用 System.out.print 而不是 println;这将每次传输的所有信息都保存在同一行上。然后在循环结束时它执行 System.out.println 以移动到下一个传输。

另一个主要区别显然是它使用带有 for 循环的 5 元素数组来存储五个转账金额,而不是将每个金额存储在自己的变量中。

此修改后的代码的输出是(包括我的输入传输值):

Transfer 1: 600
Transfer 2: 1000
Transfer 3: 830
Transfer 4: 420
Transfer 5: 1200

Suspicious transfer chart:
Transfer 1: ******
Transfer 2: **********
Transfer 3: ********
Transfer 4: ****
Transfer 5: ************

为了向您展示没有数组的情况,这里是两次传输的相同代码复制和粘贴:

import java.util.Scanner;

public class BatmanBarGraph
{
    public static void main(String[] args)
    {
        System.out.println("Good Morning Lucius. Enter Transfers - ");
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Transfer 1: ");
        int transfer1 = keyboard.nextInt();
        System.out.print("Transfer 2: ");
        int transfer2 = keyboard.nextInt();

        System.out.println();
        System.out.println("Suspicious transfer chart:");

        System.out.print("Transfer 1: ");
        for (int n = transfer1 / 100; n > 0; n--) {
            System.out.print("*");
        }
        System.out.println();

        System.out.print("Transfer 2: ");
        for (int n = transfer2 / 100; n > 0; n--) {
            System.out.print("*");
        }
        System.out.println();

    }
}