“String cannot be converted to int” 红线错误

huangapple go评论202阅读模式
英文:

"String cannot be converted to int" red line error

问题

以下是翻译好的内容:

我在这里是个新手对Java也不太熟悉从我的校园开始进行面向对象编程课程已经过去了3个星期在我的编程作业中我参考了一个同学的照片但由于某种原因我在第25行和第33行遇到了一个红线错误显示无法将String转换为int”。当我检查时照片中的所有行都没有红线但在我的代码中却有所以我感到困惑所以如果你能指出我在这里漏掉了什么我会非常感谢你的帮助

package usecourse01102020;

import java.util.Scanner;

public class UseCourse01102020 {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入系别");
        String dept = s.next();
        System.out.println("请输入科目");
        String subject = s.next();
        System.out.println("请输入学分");
        int credit = s.nextInt();
        if (dept.equals("ISC") || dept.equals("ENT") ||
                dept.equals("MSG") || dept.equals("SESS")) {
            LabSubject l = new LabSubject(dept, subject, credit);
            System.out.println(l.calculationfee());
        } else {
            MSUCourse m = new MSUCourse(dept, subject, credit);
            System.out.println(m.calculationfee());
        }
    }
}

错误出现在第25行的LabSubject l和第33行的MSUCourse m

package usecourse01102020;

public class MSUCourse extends UseCourse01102020 {

    String d, s;
    int c;

    MSUCourse(int a, String dept, String subject) {
        c = a;
        d = dept;
        s = subject;
    }

    int calculationfee() {
        return c * 120;
    }
}

package usecourse01102020;

public class LabSubject extends MSUCourse {

    int c;

    LabSubject(int a, String dept, String subject) {
        super(a, dept, subject);
        c = a;
    }

    int calculationfee() {
        return (c * 120) + 50;
    }
}

注意:由于我只返回了代码部分的翻译,因此可能存在一些上下文的缺失。如果您有任何关于翻译后代码的问题,请随时提问。

英文:

I'm new here and at Java. It's been 3 weeks in since my class Object Oriented Programming began at my campus. I was referring to a pic of my classmate for my coding assignment but for some reason, I got a red line error stating "String cannot be converted to int" for line 25 and 33. When I checked, all of the lines in the pic has no red lines but for mine, there is so I'm puzzled. So if you can point out what I'm missing here, I'd appreciate your help.

package usecourse01102020;

import java.util.Scanner;

public class UseCourse01102020 {

public static void main(String[] args) {
Scanner s= new Scanner (System.in);
System.out.println("Enter department");
String dept = s.next();
System.out.println("Enter subject");
String subject = s.next();
System.out.println("Enter credit");
int credit = s.nextInt();
if (dept.equals("ISC")||dept.equals("ENT")||
dept.equals("MSG")||dept.equals("SESS"))
{
LabSubject l = new LabSubject(dept,subject,credit);
System.out.println(l.calculationfee());
// fee = (credit*120)+50;
}
else
{
MSUCourse m = new MSUCourse(dept,subject,credit);
System.out.println(m.calculationfee());
// fee = credit*120
}
}

}

The errors are at line 25 LabSubject l and line MSUCourse m

package usecourse01102020;

public class MSUCourse extends UseCourse01102020{

String d,s;
int c;
MSUCourse(int a, String dept, String subject){
c = a;
d = dept;
s = subject;
}
int calculationfee()
{
return c*120;
}

}

package usecourse01102020;

public class LabSubject extends MSUCourse{

int c;
LabSubject(int a, String dept, String subject){
super(a,dept,subject);
c = a;
}
int calculationfee()
{
return (c*120)+50;
}

}

答案1

得分: 2


```java
LabSubject(int a, String dept, String subject){
    
    super(a, dept, subject);
    c = a;
}

你预期部门应作为第二个参数给出。我猜想你的 a(你应该给一个更好的名字)是学分,应该作为第一个参数提供。

当你创建对象

LabSubject l = new LabSubject(dept, subject, credit);

你没有遵守这个顺序!


<details>
<summary>英文:</summary>
In
```java
LabSubject(int a, String dept, String subject){
super(a,dept,subject);
c = a;
}

You expect department to be given as 2nd parameter. I suppose your a (you should give a better name) is the credit, which should be provided as 1st parameter.

When you create the object

LabSubject l = new LabSubject(dept,subject,credit);

You don't respect this order!

答案2

得分: 0

你正在传递整数值,而不是字符串值。

在 lab 科目的参数中,将 int a 更改为 String a,

LabSubject(String a, String dept, String subject)

还要更改这个

MSUCourse(String a, String dept, String subject)
英文:

You are passing int value instead of string value

In the arguments of lab subject change int a ,into String a,

LabSubject(String a, String dept, String subject)

Also change this

MSUCourse(String a, String dept, String subject)

答案3

得分: 0

你应该进行数据转换,将字符串转换为整数,使用解析函数。

英文:

you should casting the data, strig cast to int using parse function

答案4

得分: 0

你需要按照构造函数参数的顺序进行操作。

必须是 LabSubject(int, string, string);

你正在传递 LabSubject(string, string, int);

在两个构造函数中进行检查。

尝试使用 LabSubject(credit, dept, subject);

英文:

you need to follow the order of that constructor parameters

required is LabSubject(int,string,string);

you are passing LabSubject(string,string,int);

check in both constructor.

try like LabSubject(credit,dept,subject);

huangapple
  • 本文由 发表于 2020年10月5日 13:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64202767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定