-1

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;
            }
        }
    }
    
}
  • well that is because you are breaking out of the switch case. You arent breaking out of the while loop. Consider [this](https://stackoverflow.com/a/22823427/10671013) or using a boolean variable + if-statement – Tan Yu Hau Sean Sep 13 '22 at 17:12
  • Right. Change `while (true)` so that it uses a boolean variable instead of a hard-coded value. Change the boolean variable when they select "no" to make it exit... – Idle_Mind Sep 13 '22 at 17:19
  • @TanYuHauSean When I do that, even if I enter "yes" it exits the program. Instead, it must prompt the user again for the information. What am I doing wrong? – Humera Butool Sep 13 '22 at 17:19
  • @Idle_Mind I am still confused with changing the boolean variable part. Can you please explain it a little? I am new to coding and still learning. Thank you – Humera Butool Sep 13 '22 at 17:31

1 Answers1

0

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!");
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40