How to Loop the Whole Program

How do I loop the whole program?

I assume this is what you are looking for, but before you take and use this code, consider these few things: the way your code is written is not maximizing the use of Java being object oriented. This code would be far more legible and better in general should it have more than one class. The Main class should only be used to initialize another class or group of classes. Okay so now for the code. This should do the trick:

    import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Shuffle {
public static void main(String[] args) {
startGame();
}
public static void shuffle(String input,String rword) throws InterruptedException{
Scanner scanner = new Scanner(System.in);

List<Character> characters = new ArrayList<>();
for(char c:input.toCharArray()){
boolean scword = characters.add(c);
}
StringBuilder scrmbldword = new StringBuilder(input.length());
while(!characters.isEmpty()){
int randPicker = (int)(Math.random()*characters.size());
scrmbldword.append(characters.remove(randPicker));
}

// Game loads the game xD
System.out.print("\rLoading");
Thread.sleep(500);
System.out.print("\rLoading.");
Thread.sleep(500);
System.out.print("\rLoading..");
Thread.sleep(500);
System.out.print("\rLoading...");
Thread.sleep(500);
System.out.print("\rLoading....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(100);
System.out.print("\rCompleted");

// Game introduces itself to the user.
Thread.sleep(2000);
System.out.println("\rWelcome to Scramble PT!");
Thread.sleep(1000);
System.out.println("This is a game where you guess a word that is scrambled.");

// Game request name input from the user
Thread.sleep(2000);
System.out.println("First, What is your Name?");
String name = scanner.next();

// Game prints name input from the user
Thread.sleep(2000);
System.out.println("Hello " + name + ",Please get ready because the game is Starting!");

// Game ask the user for Y/N input
Thread.sleep(2000);
System.out.print("Are you ready?: ");
String yon = scanner.next();

switch (yon.toLowerCase()) {
case "yes":
// Game prints the Scrambled Word
System.out.print("Scrambled Word: ");
System.out.print(scrmbldword.toString());
// Game let user enter it's guess
System.out.print("\nEnter your Answer: ");
String answer;
boolean win;
win = false;

while (!win){
answer = scanner.next();
if (!answer.equalsIgnoreCase(rword)){
System.out.println("Wrong, please Try Again!");
Thread.sleep(500);
System.out.print("Enter your Answer: ");
}else{
System.out.println("Correct!");
Thread.sleep(500);
System.out.print(String.format("Thanks for playing %s!",name));
win = true;
Thread.sleep(500);
System.out.println(" Would you like to try again?");
System.out.println("Enter Yes or No: ");
String retry = scanner.next();
Thread.sleep(500);

if (retry.equalsIgnoreCase("Yes")){
System.out.println("Alright! reloading the game");
startGame();
return;
}else
if (retry.equalsIgnoreCase("No")){
System.out.println(String.format("Goodbye! %s !",name));
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}else{
System.out.println("I can't understand you.... So.... Bye!");
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}break;
}
}
case "no":
Thread.sleep(500);
System.out.print("Ok, Please come again!");
break;
default:
Thread.sleep(500);
System.out.print("Please answer exactly Yes or No!");
break;
}
}

public static void startGame() {
String[] word = new String[22];

word[0] = "Package";
word[1] = "Import";
word[2] = "Public";
word[3] = "Private";
word[4] = "Static";
word[5] = "Void";
word[6] = "String";
word[7] = "Integer";
word[8] = "Character";
word[9] = "Boolean";
word[10] = "Public";
word[11] = "High-Level";
word[12] = "Low-Level";
word[13] = "Class";
word[14] = "Statements";
word[15] = "Constructor";
word[16] = "Default";
word[17] = "Method";
word[18] = "Declaration";
word[19] = "Object";
word[20] = "Variable";
word[21] = "Null";

String rword = word[(int) (Math.random() * word.length)];
try {
shuffle(rword,rword);
} catch (InterruptedException ignored) {}
}

}

How to repeat program after running the whole program in Python?

You can use isnumeric to check whether the input is integer or not.

def main():

from random import randint
rn = randint(0,5)
print("The randomly generated integer is:",rn)
userint= input("Enter an integer:")
if userint.isnumeric():
userint=int(userint)
try:
if userint == rn:
print("Numbers are equal!")
elif userint > rn:
print(userint)
else:
print(rn)
except:
print("You have not entered an integer!")

restart = input("Do you want to start again?").lower()
if restart == "yes":
main()
else:
exit()
main()

Reapeating a program with a while loop in python

The first step is to clean up the code. The creation of the password boils down to:

import random
import string


def main():
characters = list(string.ascii_letters + string.digits + string.punctuation)
random.shuffle(characters)
password = ''.join(characters[:15])
print(password)

if __name__ == '__main__':
main()

Your description isn't quite clear in what you want to check. For the moment I assume you want the user to enter the password and to repeat the whole process of password generation and the check until the generated password matches the input of the user.

def main():
characters = list(string.ascii_letters + string.digits + string.punctuation)
while True:
random.shuffle(characters)
password = ''.join(characters[:15])
print(password)
password_input = input('Enter the password: ')
if password == password_input:
break

Now you can only leave the loop if you enter the correct random password (with 15 different characters). It's highly unlikely that you are able to do that (that's why I left the print in the code) and I'm not sure that this is what you want the program to do. You might want to clarify your question and explain what the "it" in "if it's an empty string" is.

How do I repeat the program in python

Put the whole code in a while True loop and at the end ask the user if they want to repeat. If not, break the loop.

Something like this:

while True:
sentence=input("Please enter sentence(s)")
words = sentence.split()
number_of_words = len(words)
counter=0
for x in sentence:
if x in "!?.":
counter=counter+1
print("There is "+str(counter)+" sentences and " + str(number_of_words) + "words")
if input('Do you want to repeat(y/n)') == 'n':
break


Related Topics



Leave a reply



Submit