Home > database >  i want to write a code which accepts int and makes a horizontal bar with the number of asterisks as
i want to write a code which accepts int and makes a horizontal bar with the number of asterisks as

Time:10-23

import java.util.*;

public class assign {
    public static void main(String[]args){

        Scanner scanner=new Scanner(System.in);
        System.out.println("Enter first number between 1 to 30");
        int a = scanner.nextInt();
        System.out.println("Enter second number between 1 to 30");
        int b = scanner.nextInt();
        System.out.println("Enter third number between 1 to 30");
        int c = scanner.nextInt();
        System.out.println("Enter fourth number between 1 to 30");
        int d = scanner.nextInt();
        System.out.println("Enter fifth number between 1 to 30");
        int e = scanner.nextInt();
        scanner.close();

        for(int i=0;i<a-1;i  );{
            System.out.print("*");
        }
        System.out.println();
        for(int i=0;i<b-1;i  );{
            System.out.print("*");
        }
        System.out.println();
        for(int i=0;i<c-1;i  );{
            System.out.print("*");
        }
        System.out.println();
        for(int i=0;i<d-1;i  );{
            System.out.print("*");
        }
        System.out.println();
        for(int i=0;i<e-1;i  );{
            System.out.print("*");
        }
        System.out.println();
    }
    
}

enter image description here

the output gives only one asterisk per line instead of required the number of scanned quantity

CodePudding user response:

Fix

Remove the semicolon, and change the bound from a-1 to a to have the good amount

for (int i = 0; i < a; i  ) {
    System.out.print("*");
}

Improve

With String.repeat

System.out.println("*".repeat(a));
System.out.println("*".repeat(b));
System.out.println("*".repeat(c));
System.out.println("*".repeat(d));
System.out.println("*".repeat(e));
  •  Tags:  
  • java
  • Related