简体   繁体   中英

How to take user input using while loop asking if he/she wants to continue or exit?

I am trying to take user input and ask if the user wants to enter the gamer's information. if "yes" then take the info and if "no" then exit the program.

My code is working well except that when I am asking the user for the second time and enter "no", it is not exiting the program but instead is asking again for the gamer's name.

import java.util.Scanner;

public class A3 {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        String name;
        double L1, L2, L3, ES;
        boolean yes = true

        System.out.println("Welcome to the Total XP calculation program!");
        
        
        System.out.print("Do you want to enter gamer's data? Yes/No => ");
        String y_n= sc.nextLine();
        while (yes = true) {
            
            if (y_n.equals("yes") || y_n.equals("Yes")) {
                System.out.print("Enter gamer's name: ");
                name = sc.next();
                System.out.println("Enter gamer's Level XP scores separated by space: L1 L2 L3 ES => ");
                L1 = sc.nextDouble();
                L2 = sc.nextDouble();
                L3 = sc.nextDouble();
                ES = sc.nextDouble();
                
                //  calculating total score
                double score = L1+L1*0.20+L2+L2*0.30+L3+L3*0.50+ES+ES*0.60;
                //  printing informations.
                
                System.out.println("Gamer's name: "+name + " L1 = "+L1 + " L2 = " +L2 + " L3 = " +L3 + " ES = " +ES);
                
                System.out.println("Final XP score with bonuses= "+score);
                
                System.out.println("\nDo you want to enter another gamer's data? Yes/No => ");
                String choice;
                sc.nextLine(); 
                choice = sc.nextLine();
                switch (choice) {
                case "no" :
                case "No" :
                    System.out.println("Thank you for using the Total XP calculator!");
                yes = false;
                    
                    
                }
                
            }
            else if (y_n.equals("no") || y_n.equals("No")) {
                System.out.println("Thank you for using the Total XP calculator!");
                break;
            }
        }
    }
    
}

I'd write it more like:

boolean quit = false;
while (!quit) {   
  System.out.print("Do you want to enter gamer's data? Yes/No => ");
  String y_n= sc.nextLine();
  if (y_n.toLowerCase().equals("yes") ) {
    System.out.print("Enter gamer's name: ");
    name = sc.next();
  
    System.out.println("Enter gamer's Level XP scores separated by space: L1 L2 L3 ES => ");
    L1 = sc.nextDouble();
    L2 = sc.nextDouble();
    L3 = sc.nextDouble();
    ES = sc.nextDouble();
    sc.nextLine(); // clear out line feed
    
    //  calculating total score
    double score = L1+L1*0.20+L2+L2*0.30+L3+L3*0.50+ES+ES*0.60;
  
    //  printing informations.                
    System.out.println("Gamer's name: "+name + " L1 = "+L1 + " L2 = " +L2 + " L3 = " +L3 + " ES = " +ES)  ;              
    System.out.println("Final XP score with bonuses= "+score) ;                                   
  }
  else if (y_n.toLowerCase().equals("no")) {                
    quit = true;
  }
  else {
    System.out.println("Invalid response!");
  }
}
System.out.println("Thank you for using the Total XP calculator!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM