Hello im new to java so can someone tell me how can i make my code rerun it self after getting the final result idk what method to use and i dont think (while or for) loops are helpful at that sitiuation
for exp in this code after entering the number and getting the result i want it go back and ask me if i wanna enter new numbers or finish
#how can i make my code rerun it self till i stop it from cmd
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
There are multiple ways to make your code rerun until you stop it. One common approach is to use a loop, such as a while loop or do-while loop. Here's an example using a while loop:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String userInput;
do {
// Your code goes here
System.out.println("Do you want to rerun the code? (yes/no)");
userInput = scanner.nextLine();
} while (userInput.equalsIgnoreCase("yes"));
System.out.println("Program stopped.");
}
}
In this example, the code inside the do block will be executed at least once, and then it will keep running as long as the user enters "yes" when prompted. If the user enters anything other than "yes", the loop will exit and the program will stop.
You can modify this example according to your specific requirements and add your own code inside the do block.
Hi, you should look into infinite loops and how to break out of it.
what makes you think loops arent helpful for this?
thank u i will give it a look now
i only mentioned while and for loops and as i said im new to java so i dont really know all the functions of while loop i only know the basic one so if theres a way while loop can help me to rerun my code till i stop i would be thankful to know it
but you said "not helpful for the situation"
i tried the do-while loop here but it doesnt give me a chance to put my answer
they are the perfect tool for the situation
maybe i need to study while loop more
that's because nextInt(), or any next...() method, doesnt work well with nextLine() when used for console input
if you want something to keep repeating, thats a loop. you could use while loop, do-while works too
point is, a loop is the tool to use
should i change the answers from yes / no to 1/2 or anything thats next.Int?
you could do that, or look into the other work arounds
i hear u
okay i will give it a look
thank u for your time sir appreicate the help
/tag scanner btw
ah, thanks