I’m having a problem with my while loop. The program asks the user for their name and after the user have made their input, the program asks how many times you would like to print the input.
I’ve been stuck on my while-loop for quite a time and can only make it work if I do something like: } while (antal > some random number)
package uppg2;
import java.util.Scanner;
public class Uppg2 {
public static void main(String[] args) {
Scanner name = new Scanner(System.in);
Scanner ant = new Scanner(System.in);
int antal;
String namn;
System.out.print("Whats your name?: ");
namn = name.nextLine();
System.out.print("How many times u wanna print ur name?: ");
antal = ant.nextInt();
do {
System.out.print(namn);
} while (????);
antal++;
namn = null;
antal = 0;
}
}
This would be rather a use-case for a for-loop like some others suggested. But when you insist on using a while loop:
When you don’t need the value of
antalanymore when the loop is over, you can also do it without the counter variable and just reduce antal every loop and check if it has reached 0.