Write a program that asks the user for an integer, and if the input is positive, puts stars on the screen - one star on line one, two stars on line two, three stars on line three, and so on until there are as many as the user's input. The program repeats this until the user inputs a negative integer or zero.
How do you input a number in java and print a star on a line according to the integer entered? public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("Enter a number: ");
int input = sc.nextInt();
// if user enters a zero or negative number then exit the while loop
if(input %26lt;= 0)
break;
for(int x=1; x%26lt;=input; x++) {
for(int y=1; y%26lt;=x; y++)
System.out.print("*");
System.out.println();
}
}
}How do you input a number in java and print a star on a line according to the integer entered?get input from user
lineNumber=1;
while (input%26gt;0){
for(int i=1;i%26lt;=lineNumber;i++)
System.out.print("*");
input--;
if(input==0) break;
}
System.out.print("\n");
lineNumber++;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment