gpt4 book ai didi

java - 找不到符号,我做错了什么?

转载 作者:行者123 更新时间:2023-12-01 12:32:54 28 4
gpt4 key购买 nike

所以基本上我在我的 main 中调用一个类,然后尝试打印底部的最后一行,但它一直说找不到 int x 和 int y 的符号。我很确定我将它们相应地分配给了正确的控制台输入。有谁知道我做错了什么。我是java新手。

import java.util.*;

public class mainRational {
public static void main(String[] args) {

Scanner console = new Scanner(System.in);
Scanner console2 = new Scanner(System.in);

System.out.println("Enter a number for the numerator and denominator: ");

//creates the first class object
rationalNumbers rational = new rationalNumbers(console.nextInt(), console2.nextInt());

int x = console.nextInt;
int y = console2.nextInt;

System.out.println("Rational Number is: " +x+ "/" +y);
}
}

公开课

public class rationalNumbers {

private int Numerator;
private int Denominator;
private String String;



public rationalNumbers(int Numerator, int Denominator){

if (Denominator==0)
throw new IllegalArgumentException();
this.Numerator=Numerator;
this.Denominator=Denominator;

}

public void rationalNumber(){

}

public int getDenominator(){
return Denominator;

}

public int getNumerator(){
return Numerator;
}

public String toString(){
return String;
}

}

最佳答案

而不是console.nextInt,它应该是console.nextInt()

您不必要创建两个 Scanner 对象这应该更有意义

System.out.println("Enter a number for the numerator"); 
int x = console.nextInt();
System.out.println("Enter a number for the denominator");
int y = console.nextInt();
rationalNumbers rational = new rationalNumbers(x,y); //follow class naming convention
System.out.println("Rational Number is: " +x+ "/" +y);

关于java - 找不到符号,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25803529/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com