簡體   English   中英

如果其他語句不起作用 - 為什么?

[英]If else statements not working- why?

在處理這段代碼一段時間並接近尾聲后,我遇到了一個意想不到的問題。 選項“B”和“C”的 if 和 else 語句在作為用戶輸入輸入時在屏幕上不顯示任何內容。 我已經嘗試了一些事情,但我似乎無法弄清楚。

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);
        }
    }
}

當用戶輸入“A”然后輸入小時數時,程序運行完美並給了我想要的輸出。 如果輸入“B”或“C”,然后輸入小時,則屏幕上沒有輸出。 這是什么原因造成的?

-編輯-我在檢查響應之前弄亂了代碼,發現如果刪除 else 並創建此代碼:

  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);}

...程序運行正常。 我猜其他語句是不必要的並導致了問題。

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);}
    }
}

只是添加一些適當的表格顯示,你實際上並沒有涵蓋除“A”之外的任何其他情況。 制表使代碼看起來更清晰,因此更容易找到錯誤。 您需要正確使用大括號,因為現在您只檢查選擇=='A'。 如果沒有 - 您的代碼不會做任何事情。

你有if(choice == 'A')給你帶來麻煩。 還要檢查另一個不正確縮進的大括號。 這很難讀。 代碼應該是人類可讀的

看到:

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

此代碼相當於,(增加了大括號)

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

所以你必須刪除它。 你想要做的更清楚的是這個。 你可以使用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; 
 }

嘗試正確使用大括號:)

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);

您可以簡單地使用“AND”以使您的條件正常工作。

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);
}

改變這個:

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

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. "});
    }
})

})

從看着它,

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

這兩個沒有縮進,意味着if (hours <= 10) {將運行,無論第一個語句的結果如何。 因此,當用戶鍵入B ,然后是小時(小於10 )時,第一個語句讀取為false ,第二個語句讀取為true ,因此運行該語句。

嘗試在所有if語句周圍放置{}並嘗試再次運行它。

我可能不正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM