简体   繁体   English

另一个作业问题错误意外类型是必需的:变量已找到:值

[英]another homework question error unexpected type required:variable found:value

I have another error, unexpected type required:variable found:value and I can't figure out why. 我还有另一个错误,意外类型为required:variable found:value,我不知道为什么。 The code is as follows: 代码如下:

public class ISBNText extends JTextField
{  
    protected static final int ISBN_NUM=10;
    protected static String bookNum;
    protected JTextField  bookText; 
    protected String valid;
    public ISBNText() 
    {
        super(20);
    }   

    public String getISBN()
    {           
        bookNum = getText();
        return bookNum;
    }

    private String validateISBN(String bookNum)throws ISBNException
    {
        boolean check=false;
        bookNum.replaceAll("-","");
        if (bookNum.length()!=ISBN_NUM)
            throw new ISBNException ("ISBN "+ bookNum + " must be 10 characters");
        for (int i=0;i<bookNum.length()-1;i++)
        {
            if (Character.isDigit(bookNum.charAt(i)))
                check=true;
        }  
        if (bookNum.charAt(9)=='X') check=true;
        if (Character.isDigit(bookNum.charAt(9))) check=true;
        if (check=false) throw new ISBNException ("ISBN " + bookNum + " must contain all digits" + 
                "or 'X' in the last position");
        if (checkDigit(bookNum)=false)      //////////COMPILER   ERROR    HERE////////
            throw new ISBNException ("ISBN " + bookNum + " is invalid.\n" + 
                "ISBN " + bookNum + " should be " + validnum);
        if (check=true) return bookNum;
    }                             

    public boolean checkDigit (String bookNum)
    {
        boolean status;
        double total=0.0;
        char[] check   = {0,1,2,3,4,5,6,7,8,9,X};
        int[] checkNums= {0,1,2,3,4,5,6,7,8,9,10};
        for (int i=0;i<bookNum.length;i++)
        {
            check(i)=bookNum[i];
            total+=check[i]*checkNums[i];
        }    
        if ((checkNums[9] % 11)==check[9])
            status = true;
        else 
            status=false;
    }
}

This program is being used to check the validity of an ISBN number entered by a user. 该程序用于检查用户输入的ISBN号的有效性。 I don't understand why it says it finds value and it expects a variable, as bookNum is a variable. 我不明白为什么它说它找到价值并且期望一个变量,因为bookNum是一个变量。 I have no doubt that this is due to my limited knowledge. 我毫不怀疑这是由于我的有限知识。 Hoping you guys can help me out one more time. 希望你们能再帮我一次。

This assignment (which doesn't make sense) 这项作业(没有意义)

if (checkDigit(bookNum)=false)

should rather have been an equation 应该是一个方程式

if (checkDigit(bookNum)==false)

or, better , just an expression 或者更好 ,只是一个表达

if (!checkDigit(bookNum))

See also: 也可以看看:

checkDigit(bookNum)=false should be !checkDigit(bookNum) checkDigit(bookNum)=false应该是!checkDigit(bookNum)

Same thing for if (check=false) which should be if (!check) Another with if (check=true) if (check=false)应该是if(!check)的另一件事if(check = true)

Also, 也,

for (int i=0;i<bookNum.length;i++)
{
    check(i)=bookNum[i];
    total+=check[i]*checkNums[i];
}    

Should be : 应该 :

for (int i=0;i<bookNum.length();i++)
{
    (something, but definitely not a function result)=bookNum.charAt(i);
    total+=check[i]*checkNums[i];
}

Plus, 加,

char[] check   = {0,1,2,3,4,5,6,7,8,9,X}; // X won't compile

Plus, 加,

checkDigit has no return. checkDigit没有回报。

Plus, 加,

validnum is not declared 没有声明validnum

You need to use charAt , not the array index notation on the bookNum variable in the checkDigit method. 您需要使用charAt ,而不是checkDigit方法中bookNum变量上的数组索引符号。 Strings are not arrays. 字符串不是数组。

You need to use == , not = . 您需要使用== ,而不是= The former checks for equality, the latter is an assignment operator. 前者检查是否相等,后者是赋值运算符。 Alternatively, you could just do if (!checkDigit(bookNum)) , rather than an explicit comparison to false . 或者,您可以只执行if (!checkDigit(bookNum)) ,而不是显式比较false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM