Skip to content
← Back to Community
How to loop a program?
Profile icon
ThomasWeaver

I made a game in java that generates a number and you have to guess the number that the computer generates. Once they get it right I'm supposed to ask if they want to play again, but I don't understand how I'm supposed to re-run the game. Anybody know?

Voters
Profile icon
SAVANNAHWOOD1
Profile icon
HaunaleiBaker
Profile icon
Sean-Chen-8GChe
Profile icon
SofiaHernandez3
Profile icon
MacHerrington
Profile icon
Rivera-NixonNix
Profile icon
tvdfan
Profile icon
d3xvr
Profile icon
lucrezia70
Profile icon
ThomasWeaver
Comments
hotnewtop
Profile icon
Geocube101

My first guess is a while loop.

import java.util.Scanner; class Main { public static void main(String[] args) { while (true != false) { //Game Code System.out.print("Play Again? "); Scanner sc = new Scanner(System.in); String again = sc.nextLine(); if (again.equals("yes") == false) { sc.close(); break; } } } }

My second guess is to use functions:

import java.util.Scanner; class Main { public static void game() { //Game Code loopGame(); } public static void loopGame() { System.out.print("Play Again? "); Scanner sc = new Scanner(System.in); String again = sc.nextLine(); if (again.equals("yes") == true) { game(); } } public static void main(String[] args) { game(); } }