简体   繁体   中英

If else statements not working- why?

After working on this code for a while and coming very close to the end, I've run into an unexpected problem. The if and else statements for choices 'B' and 'C' display nothing on the screen when they are typed in as user input. I've tried a few things but I cannot seem to figure it out.

import java.util.Scanner;
public class Internet_Service_Provider
{
    public static void main(String[] args)
    {
        double hours;
        char choice;

        Scanner input = new Scanner(System.in);

        System.out.println ("Enter the letter of your package: ");
        choice = input.nextLine().charAt(0);

        System.out.println ("Enter the number of hours used: ");
        hours = input.nextDouble();

        double chargesa, chargesb, chargesc;
        chargesa = 9.95;
        chargesb = 13.95;
        chargesc = 19.95;

        double chargesa2, chargesb2;
        chargesa2 = (hours - 10) * 2;
        chargesb2 = (hours - 20);

        double extrafeesA, extrafeesB;
        extrafeesA = chargesa + chargesa2;
        extrafeesB = chargesb + chargesb2;

        if (choice == 'A')
            if (hours <= 10) { 
                System.out.println ("Your total charges are: " + chargesa);
            }
        else if (choice == 'A')
            if (hours > 10){
                 System.out.println ("your total charges are: " + extrafeesA);
             }
        else if (choice == 'B')
            if (hours <= 20) {
                 System.out.println ("Your total charges are: " + chargesb);
            }
        else if (choice == 'B')
            if (hours > 20){
                 System.out.println ("Your total charges are: " + extrafeesB);
            }
        else if (choice == 'C'){
             System.out.println ("Your total charges are: " + chargesc);
        }
    }
}

When the user types 'A' and then types the hours, the program runs perfectly and gives me the desired output. If typing 'B' or 'C' and then the hours, there is no output onto the screen. What is causing this?

-EDIT- I messed around with the code before checking the responses and discovered that if removing the else and creating this code:

  if (choice == 'A')
        if (hours <= 10) { 
              System.out.println ("Your total charges are: " + chargesa);
        }

  if (choice == 'A')
        if (hours > 10){
              System.out.println ("your total charges are: " + extrafeesA);
        }

  if (choice == 'B')
        if (hours <= 20) {
              System.out.println ("Your total charges are: " + chargesb);
        }

  if (choice == 'B')
        if (hours > 20){
              System.out.println ("Your total charges are: " + extrafeesB);
        }

  if (choice == 'C'){
        System.out.println ("Your total charges are: " + chargesc);}

... the program runs properly. I guess the else statements were unnecessary and caused the problem.

if (choice == 'A'){
    if (hours <= 10) { 
      System.out.println ("Your total charges are: " + chargesa);}
    else if (choice == 'A')
      if (hours > 10){
      System.out.println ("your total charges are: " + extrafeesA);}
    else if (choice == 'B')
      if (hours <= 20) {
      System.out.println ("Your total charges are: " + chargesb);}
    else if (choice == 'B')
      if (hours > 20){
      System.out.println ("Your total charges are: " + extrafeesB);}
    else if (choice == 'C'){
      System.out.println ("Your total charges are: " + chargesc);}
    }
}

Just adding some proper tabulation reveals, that you do not actually cover any other case than "A". Tabulation makes the code look much clearer, and therefore it is easier to find an error. You need to use your braces properly, as for now you only check if choice == 'A'. If it does not - your code doesn't do anything.

You have if(choice == 'A') that is giving trouble you. Also check another braces that are not correct indented. It's difficult to read. Code should be readable by human beings

See:

if (choice == 'A')// this is giving trouble to you
if (hours <= 10) { 
  .
  .

This code is equivalent to,(braces added)

if (choice == 'A'){
    if (hours <= 10) {
     .
     . 
}

So you have to remove it. What you want to do to be more clear is this. You can use switch

switch(choice){
 case 'A':
       System.out.println ("Your total charges are: " + (hours <=10) ?chargesa:extrafeesA);
       break;
 case 'B':
       System.out.println ("Your total charges are: " + (hours <=10) ?chargesB:extrafeesB);
       break;
 case 'C':
       System.out.println ("Your total charges are: " + chargesc);
       break; 
 }

Try to use braces correctly :)

if (choice == 'A') {
   if (hours <= 10) 
      System.out.println ("Your total charges are: " + chargesa);
   if (hours > 10)
       System.out.println ("your total charges are: " + extrafeesA);
}
else if (choice == 'B') {
   if (hours <= 20)
      System.out.println ("Your total charges are: " + chargesb);
   if (hours > 20)
      System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C')
   System.out.println ("Your total charges are: " + chargesc);

You could simply use 'AND' in order to get your conditions work correctly.

if (choice == 'A' && hours <= 10) { 
  System.out.println ("Your total charges are: " + chargesa);
}
else if (choice == 'A' && hours > 10) {
  System.out.println ("your total charges are: " + extrafeesA);
}
else if (choice == 'B' && hours <= 20)  {
  System.out.println ("Your total charges are: " + chargesb);
}
else if (choice == 'B' && hours > 20) {
  System.out.println ("Your total charges are: " + extrafeesB);
}
else if (choice == 'C'){
  System.out.println ("Your total charges are: " + chargesc);
}

change this:

if (choice == 'A')
if (hours <= 10) { 
  System.out.println ("Your total charges are: " + chargesa);}

to

if (hours <= 10) { 
  System.out.println ("Your total charges are: " + chargesa);}

router.post('/login', (req, res)=>{ var user = req.body;

query= "select email, password, status, role from user where email= ? ";

connection.query(query, [user.email], (err, results)=>{
    if(!err){
        if(results.length<=0 || results[0].password != user.password){
            return res.status(401).json({message:"username and password mis-match."})
        }
    }

    else if(results[0].status === 'false'){
        return res.status(401).json({message: "wait for admin approval. "})
    }

    else if(results[0].password == user.password){
        // const response = {email: results[0].email, role: results[0].role};
        // const accessToken = jwt.sign(response, process.env.ACCESS_TOKEN, {expiresIn:'8h'});
        // return res.status(200).json({token : accessToken});
        return res.status(200).json({message : "user varyfied"});
    }

    else{
        return res.status(500).json({message:"something went wrong. Please try again. "});
    }
})

})

From looking at it,

if (choice == 'A')
if (hours <= 10) { 

The two are not indented, meaning if (hours <= 10) { will run regardless of the outcome of the first statement. So when the user types in B , and then the hours (less than 10 ), the first statement reads false , and the second reads true , therefore running that one.

Try putting {} around all the if statements and try running it again.

I may be incorrect though.

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