Java中的孤立案例

时间:2015-10-01 06:20:06

标签: java

孤立案例错误 以下是将在该计划中完成的任务: -

  1. 接受客户存款并更新余额。
  2. 显示余额。
  3. 计算并存入复利。
  4. 允许退出并更新余额。
  5. 检查最低余额,必要时处以罚款并更新余额
  6. 我收到

    的“孤立案例”错误

    case1:S1.Display();

    case1:S2.Display();

    请帮忙。这是代码:

    import java.util.*;
    
    class Account
    
    {
     String custname;
    
     double accno;
    
     double bal;
    
    
     Account(String c, double a, double b)
    
     {custname = c;
    
      accno = a;
    
      bal = b;
     }
    
    
    
     void Display()
    
     {System.out.println("Account Holder: "+custname);
    
      System.out.println("Account Number "+accno);
      System.out.println("Balance : "+bal); 
     }
    
    
    
     void Deposit()
    
     {double dep;
    
      Scanner sc = new Scanner(System.in);
    
      System.out.println("Please Enter the amount your want to deposit:");
    
      dep = sc.nextDouble();
    
      bal = bal + dep;
    
      System.out.println("Updated Details....");
    
      Display();
     }
    
    
     void Withdraw()
    
     {double wth;
    
      Scanner sc = new Scanner(System.in);
    
      System.out.println("Please enter the amount you want to withdraw");
    
      wth = sc.NextDouble();
    
      bal = bal - wth;
    
      System.out.println("Updated details....");
    
      Display();
    
     }
    
    }
    
    
    
    class SavAccount extends Account
    
    { String acctype;
    
    
      SavAccount(String c, double a, double b)
     {Super(c, a, b);
    
      acctype = "Savings";
    
     }
    
    
    
     void ComInt()
    
     {int months;
    
      Scanner sc = new Scanner(System.in);
    
      System.out.println("Please enter the duration of the account in months");
    
      months = sc.NextInt();
    
      double rate, inte;
      rate = 0.04;
    
      inte = months *rate * bal;
    
      bal = bal + inte;
    
      System.out.println("Compund Interest : "+inte);
     }
    
    
    
     void Display()
    
     {System.out.println("Account Holder: "+custname);
    
      System.out.println("Account Number "+accno);
    
      ComInt();
    
     System.out.println("Balance : "+bal);
    
      System.out.println("Account Type: "+acctype);
    
     }
    
    }
    
    
    
    class CurAccount extends Account
    
    {String acctype;
    
     CurAccount(String c, double a, double b)
    
     {Super(c, a, b);
    
      acctype = "Current";
    
     } 
    
    
     void Penalty()
     { 
      if(bal<5000.00)
    
      {bal = bal - 100.00;
       System.out.println("Fine deducted Rs.100/-");
    
      }
     }
    
     void Display()
    
     {System.out.println("Account Holder: "+custname);
    
      System.out.println("Account Number "+accno);
    
      Penalty();
    
     System.out.println("Balance : "+bal);
    
      System.out.println("Account Type: "+acctype);
    
      if(bal<=5000.00)
    
      {System.out.println("Warning!! Please maintain balance above Rs.5000/-");
    
      }
     }
    
    
    }
    class Accmain
    {public static void main(Strings args[])
    
     {SavAccount S1 = new SavAccount("Aniruddha", 134.00, 15000.00)
    ;
      CurAccount S2 = new CurAccount("Tyrel" , 135.00, 6000.00);
    
      int num = 2;
    
    
      String c = "y";
    
      int n;
    
      double temp;
      double accs[] = new double[10];
    
      accs[0] = S1.accno;
    
      accs[1] = S2.accno;
    
      Scanner sc = new Scanner(System.in);
    
      while (c == "y");
    
      {System.out.println("Please enter your Account number:");
    
       temp = sc.nextDouble();
    
       if(accs[0] == temp)
    
       {System.out.println("Welcome "+ S1.custname);
    
        System.out.println("Account Type: "+ S1.acctype);
    
        System.out.println("1.Display");
    
        System.out.println("2.Withdraw");
    
        System.out.println("3.Deposit");
    
        n = sc.nextInt();
    
        Switch(n)
    ;
        {
         case 1 : S1.Display();
    
         case 2 : S1.Withdraw();
    
         case 3 : S1.Deposit();
    
        default :System.out.println("Bad Choice ");
    
         c = "n";
    
       }
    
       }
       else if(accs[1] == temp)
    
       {System.out.println("Welcome "+ S2.custname);
    
        System.out.println("Account Type: "+ S2.acctype);
    
        System.out.println("1.Display");
    
        System.out.println("2.Withdraw");
    
        System.out.println("3.Deposit");
    
        n = sc.nextInt()
    ;
        Switch(n);
        {
         case 1 : S2.Display();
    
         case 2 : S2.Withdraw();
    
         case 3 : S2.Deposit();
         default :System.out.println("Bad Choice ");
    
         c = "n";
        }
    
       }
    
      } 
    
     }
    
    
    
    } 
    

3 个答案:

答案 0 :(得分:2)

有几个问题。

1)最后额外的;使得开关在这次加注时立即完成。

Switch(n); <--

显然所有案件都成了孤儿。

如果你在Switch(n)之后删除了冒号,你就可以了。

 Switch(n)
    {
     case 1 : S1.Display();

     case 2 : S1.Withdraw();

2)之后你还有另外一个问题。每个案例后你需要打破。否则,即使匹配找到,您的开关也会执行所有情况。要停止在每个案例

之后添加中断
  Switch(n)
        {
         case 1 : S1.Display(); break;

         case 2 : S1.Withdraw();break;
          ...

3)当您编写c == "y"时,比较引用不是相等的。

您需要使用equals()方法来检查字符串相等性

阅读How do I compare strings in Java?

4)那条线Super(c,a,b);不会编译,因为S必须是小写的。 Java区分大小写。

答案 1 :(得分:1)

你遇到的两个问题是:

Switch(n) // <-- switch is not spelled with a capital 's'
; // <-- remove the semicolon
    {
     case 1 : S1.Display();

     case 2 : S1.Withdraw();

     case 3 : S1.Deposit();

    default :System.out.println("Bad Choice ");

     c = "n";

   }

由于caseswitch没有任何关系,因此您会收到“孤立案例错误”。此外,当您处理完每个案例时,您应该break;否则代码将继续执行以下案例!

我还建议您使用一个好的IDE并自动缩进代码,因为它当前缩进的方式使得很难看到ifelse子句的结束位置。

一个好的IDE(IntelliJ / Eclipse / Netbeans)也会显示你所有的编译错误,例如Super(c, a, b) - 's'不应该大写,等等。

答案 2 :(得分:1)

此错误通常会导致您尝试在case声明之外使用switch关键字。

    Switch(n)
;

switch(n)之后发送;

在每个案例操作后也使用break语句。否则它将继续执行下一个案例操作。

Sures的答案中提到了其他一些小问题