简体   繁体   中英

Java, transfer graph, output as * to every 100

    import java.util.Scanner;

public class BatmanBarGraph { 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 $100. A transfer of $1000 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("*");
 }
}

}

thats my code, the output i get is

    Suspicious transfer chart: 

Transfer 1:*Transfer 2: **Transfer 3: ***Transfer 4: ****Transfer 5: *****

how do i make them like this 在此输入图像描述

For educational purposes, this code achieves the apparent intent of your code.

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();
        }
    }
}

Primarily, notice that it's using System.out.print instead of println when printing the "Transfer X: ****" output; this keeps all information for each transfer on the same line. Then at the end of the loop it does a System.out.println to move to the next transfer.

The other main difference is obviously that it's using a 5-element array with for loops to store the five transfer amounts, instead of storing each in its own variable.

The output of this modified code is (including my input transfer values):

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: ************

To show you what this would look like without the array, here's the same code copy-and-paste for two transfers:

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();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM