【问题标题】:why do it says its already defined using constructor?为什么它说它已经使用构造函数定义了?
【发布时间】:2021-06-23 00:44:36
【问题描述】:
    import java.util.Scanner;
    import java.util.Random;
    import java.util.Arrays;
    
    public class midterm {
        static Scanner scanner = new Scanner(System.in);
        public static void main(String[] args) {
            Account myAccount = new Account();
            int user_choice;
    
            do{
                System.out.println();
                System.out.println("1) New Billing");
                System.out.println("2) Add Existing Billing");
                System.out.println("3) View Billing Account ID");
                System.out.println("4) View By Date");
                System.out.println("5) Update Existing Billing");
                System.out.println("6) Delete Billing Account");
                System.out.println("7) Display All Account");
                System.out.println("8) Exit");
                System.out.println();
                System.out.print("Enter choice [1-8]: ");
                user_choice = scanner.nextInt();
                switch (user_choice) {
                
                case 1:
                    String id;
                    System.out.print("Enter Billing Code: ");
                    String bc = scanner.next();
                    System.out.print("Enter Billing Date(dd/mm/yyyy): ");
                    String bd = scanner.next();
                    System.out.print("Enter Customer Name: ");
                    String customerName = scanner.nextLine();
                    System.out.print("Enter Customer Address: ");
                    String customerAddress = scanner.nextLine();
                    System.out.print("Enter Customer Number: ");
                    String customerNumber = scanner.nextLine();
                    System.out.print("Enter Period To: ");
                    String periodT = scanner.nextLine();
                    System.out.print("Enter Period From: ");
                    String periodF = scanner.nextLine();
                    System.out.print("Enter Present Reading: ");
                    double presentR = scanner.nextDouble();
                    System.out.print("Enter Previous Reading: ");
                    double previousR = scanner.nextDouble();
                    System.out.print("Enter Previous Balance: ");
                    double previousB = scanner.nextDouble();
                    //myAccount.openNewAccount(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB);
                    System.out.println("Account was created and it has the following number: " + myAccount.openNewAccount(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB));
                    break;
    
                case 7:
                    System.out.println("Enter a account number");
                    int anum = scanner.nextInt();
    
                    
    
    
                }
            }while(user_choice!='6');
        }
  
        static class Account{
            private userAccount[] account;
            private int numOfAccounts = 10;
        
        
        public Account(){
            account = new userAccount[10];
            numOfAccounts = 0;
        }
 
 ```

这是存在问题的部分,它第一次工作但现在它不工作,我对构造函数和类是如何工作的新手。它说需要:未找到参数:字符串,字符串,字符串,字符串,字符串,字符串,字符串,字符串,双精度,双精度,双原因:实际和形式参数列表的长度不同,我只是希望它传递输入的数据randomStr 给出一个随机帐户号 ```

        public int openNewAccount(String id,String bc,String bd,String customerName,String customerAddress,String customerNumber,String periodT,String periodF,double presentR,double previousR,double previousB){
            
            int min = 1000;
            int max = 9999;
            int randomStr = (int)(Math.random() * (max - min + 1) + min);
    
            userAccount randomStr = new userAccount(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB);
            account[numOfAccounts] = randomStr;
            numOfAccounts++;
            return randomStr.getAccountNum();
        }
    
    }

   
        this is my userAccount class where the user is stored, it worked the first time but now it doesn't
 static class userAccount{
            private int accountNum;
            private String id;
            private String bc;
            private String bd;
            private String customerName;
            private String customerAddress;
            private String customerNumber;
            private String periodT;
            private String periodF;
            private double presentR;
            private double previousR;
            private double previousB;
            private  static int noOfAccounts=0;
    
            public int userAccount(String idN,String bcN,String bdN,String customerName1,String customerAddress1,String customerNumber1,String periodT1,String periodF1,double presentR1,double previousR1,double previousB1){
                
                id = idN;
                bc = bcN;
                bd = bdN;
                customerName = customerName1;
                customerAddress = customerAddress1;
                customerNumber = customerNumber1;
                periodT = periodT1;
                periodF = periodF1;
                presentR = presentR1;
                previousR = previousR1;
                previousB = previousB1;
                noOfAccounts++;
                accountNum = noOfAccounts;
            }
    
            public int getAccountNum(){
                return accountNum;
            } 
    
    }
       
    }   
    
  
    this is not yet finished just checking if it works, ive been tweaking this but cant really understand why it says my randomStr is already defined.

这是它在编译器中显示的错误。

midterm.java:80: error: variable randomStr is already defined in method openNewAccount(String,String,String,String,String,String,String,String,double,double,double)
        userAccount randomStr = new userAccount(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB);
                    ^
midterm.java:80: error: constructor userAccount in class userAccount cannot be applied to given types;
        userAccount randomStr = new userAccount(id,bc,bd,customerName,customerAddress,customerNumber,periodT,periodF,presentR,previousR,previousB);
                                ^
  required: no arguments
  found:    String,String,String,String,String,String,String,String,double,double,double
  reason: actual and formal argument lists differ in length
2 errors

【问题讨论】:

  • public int userAccount 不是构造函数,它是一个返回int 的方法。你有两个randomStr,一个是int,一个是userAccount,也不是顾名思义的String。并且请以大写字母开头类(和一般类型),否则它们很难与变量区分开来。因不理解两个非常明显的编译器错误而投反对票。

标签: java arrays class methods constructor


【解决方案1】:

这是说“变量 randomStr 已定义”,因为该变量被定义了两次,如(简化):

public void someMethod() {  // or constructor, or any block

    int randomStr;
    String randomStr;    // SAME name as previous line

}

代码错误!只是为了让错误更明显,它不是一个解决方案

你不能在同一个块中定义一个局部变量两次(即使是相同的类型)——这是Java Language Spcification 6.4不允许的:

如果使用局部变量 v 的名称在 v 的范围内声明一个新变量,则会出现编译时错误, ...

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2022-12-12
    相关资源
    最近更新 更多