Question

In: Computer Science

How do I go back to the start of the main method with the given code?...

How do I go back to the start of the main method with the given code?

Hello I am making this java program and there is a point in the code where if the user types the character y it will start the program back at the beginning of the main method.

How can I do this without drastically changing the code?

For instance, if I type y, the program will go back to line 1 and repeat the entire process until I press n for no.

public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
       System.out.println("Welcome to schedule generator!");
       System.out.println("How many classes would you like to take?");
      
       boolean validInput = false;
      
       int userInput = scnr.nextInt();
      
       while(!validInput) {
           if(userInput <= 0 || userInput >= 6) {
           System.out.println("Please choose between 0 and 6 classes.");
           System.out.println("How many classes would you like to take?");
           userInput = scnr.nextInt();
           }
           else if(userInput >= 1 && userInput <= 5) {
               validInput = true;
               System.out.println(":)");
           }
           }
       System.out.println("Would you like to generate another schedule? y - yes, n - no");
            String userResponse = scnr.next();
            if(userResponse.equals("y")) {
                //FixME
            }
            else if(userResponse.equals("n")) {
               System.out.println("Goodbye");
            }else {
               System.out.println("Goodbye");
            }
       }
       }

Solutions

Expert Solution

Create a while loop contains flag variable interchange with 1 or 0 based on the requirement. Done.

import java.util.*;
class Main{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int flag=1;
while(flag==1){
System.out.println("Welcome to schedule generator!");
System.out.println("How many classes would you like to take?");
  
boolean validInput = false;
  
int userInput = scnr.nextInt();
  
while(!validInput) {
if(userInput <= 0 || userInput >= 6) {
System.out.println("Please choose between 0 and 6 classes.");
System.out.println("How many classes would you like to take?");
userInput = scnr.nextInt();
}
else if(userInput >= 1 && userInput <= 5) {
validInput = true;
System.out.println(":)");
}
}
System.out.println("Would you like to generate another schedule? y - yes, n - no");
String userResponse = scnr.next();
if(userResponse.equals("y")) {
flag=1;
}
else if(userResponse.equals("n")) {
flag=0;
System.out.println("Goodbye");
}else {
flag=0;
System.out.println("Goodbye");
}
}
}
}


Related Solutions

3. In a typical service routine, to go back to the previous code we do RTI....
3. In a typical service routine, to go back to the previous code we do RTI. Suppose we replace RTI by a sequence of two instructions: POPpc POPpsw The POPpc instruction will pop the PC value from the control stack (assume that PC, PSW values are on the stack in the right order) and put it into the PC register. The POPpsw instruction will pop the PSW value from the control stack and put it in the PSW register. Assume...
how do i start with this question
how do i start with this question
Start with code given for Student, StudentOther, StudentUpdate See code comments Student has two method that...
Start with code given for Student, StudentOther, StudentUpdate See code comments Student has two method that overloads rating StudentOther extends Student and has one method that overrides rating StudentUpdate call both classes Code as required public class studentUpdate { public static void main(String[] args) { // instantiate Student object Student s = ?; // instantiate StudentOther object StudentOther sO = ?; // set the fields s.setStudentName ?; s.setId ?; s.setCredits ?; s.setGpa ?; s.setScholarship ?; s.setCommuter ?; // check input...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
How can I answer this question in Capsim simulation ? "If you could go back and...
How can I answer this question in Capsim simulation ? "If you could go back and change your company decisions for one of the 6 rounds which round would it be and why. "
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (1) A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the...
****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (3) Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT