为什么我的java代码在我';我在试着在试块里打字?
我以这种方式编写了代码,其中方法除法在除掉两个整数后返回双值。如果我不包含try-catch块,它可以正常工作。但是当我在try块中使用类型转换将整数除法括起来时,如下图所示,它会导致编译问题。。。请解决我的问题为什么我的java代码在我';我在试着在试块里打字?,java,try-catch,division,typecasting-operator,Java,Try Catch,Division,Typecasting Operator,我以这种方式编写了代码,其中方法除法在除掉两个整数后返回双值。如果我不包含try-catch块,它可以正常工作。但是当我在try块中使用类型转换将整数除法括起来时,如下图所示,它会导致编译问题。。。请解决我的问题 import java.util.Scanner; class Division { /** * @param args */ public static void main(String[] args) { Scanner sc
import java.util.Scanner;
class Division {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Division div=new Division();
System.out.println("Enter the two numbers to perform division operation:");
int x=sc.nextInt();
int y=sc.nextInt();
div.division(x,y);
}
public double division(int x,int y){
try{
double z=(double)(x/y);
return z;
}
catch(ArithmeticException ae){
ae.printStackTrace();
}
}
}
您的方法必须有一个
return
语句
如果当前代码进入catch
块,则它没有return
语句
试试这个代码
import java.util.Scanner;
public class Division {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Division div = new Division();
System.out.println("Enter the two numbers to perform division operation:");
int x = sc.nextInt();
int y = sc.nextInt();
div.division(x, y);
}
public double division(int x, int y) {
double z = 0;
try {
z = (double) (x / y);
} catch (ArithmeticException ae) {
ae.printStackTrace();
}
return z;
}
}
现场演示
另外,我猜在您进行类型转换的方式中可能会丢失数据。如果
17/3=5.6666666
是您想要的,那么您的代码是错误的,因为x
和y
是int
。当前代码的输出为17/3=5
而不是
z=(双精度)(x/y)代码>,您需要z=(双)x/(双)y
您的方法必须有一个return
语句
如果当前代码进入catch
块,则它没有return
语句
试试这个代码
import java.util.Scanner;
public class Division {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Division div = new Division();
System.out.println("Enter the two numbers to perform division operation:");
int x = sc.nextInt();
int y = sc.nextInt();
div.division(x, y);
}
public double division(int x, int y) {
double z = 0;
try {
z = (double) (x / y);
} catch (ArithmeticException ae) {
ae.printStackTrace();
}
return z;
}
}
现场演示
另外,我猜在您进行类型转换的方式中可能会丢失数据。
如果17/3=5.6666666
是您想要的,那么您的代码是错误的,因为x
和y
是int
。当前代码的输出为17/3=5
而不是z=(双精度)(x/y)代码>,您需要z=(双)x/(双)y代码>
您的除法功能中缺少一个返回值。通过捕获异常,您表示您将在出现问题的情况下采取措施来处理它,并且在出现问题后将继续执行
最好在这里抛出异常,因为如果除以零,就不会返回任何内容。你也可以选择返回一些无意义的东西,比如-1
public double division(int x,int y){
try{
double z=(double)(x/y);
return z;
}
catch(ArithmeticException ae){
ae.printStackTrace();
}
return -1;
}
更好的解决方案是,如果除数为0,则抛出一个异常,然后在使用它的任何地方处理它
public double division(int x, int y) throws ArithmeticException {
if (y == 0)
throw new ArithmeticException("Cannot divide by 0");
double z = (double) (x / y);
return z;
}
您的除法函数中缺少返回值。通过捕获异常,您表示您将在出现问题的情况下采取措施来处理它,并且在出现问题后将继续执行
最好在这里抛出异常,因为如果除以零,就不会返回任何内容。你也可以选择返回一些无意义的东西,比如-1
public double division(int x,int y){
try{
double z=(double)(x/y);
return z;
}
catch(ArithmeticException ae){
ae.printStackTrace();
}
return -1;
}
更好的解决方案是,如果除数为0,则抛出一个异常,然后在使用它的任何地方处理它
public double division(int x, int y) throws ArithmeticException {
if (y == 0)
throw new ArithmeticException("Cannot divide by 0");
double z = (double) (x / y);
return z;
}
您的方法缺少return
语句。试试这个
public double division(int x,int y){
double z=0.0;
try{
z=(double)(x/y);
}
catch(ArithmeticException ae){
ae.printStackTrace();
}
return z;
}
您的方法缺少return
语句。试试这个
public double division(int x,int y){
double z=0.0;
try{
z=(double)(x/y);
}
catch(ArithmeticException ae){
ae.printStackTrace();
}
return z;
}
问题是,如果try块失败,则找不到return语句(因为您提供的返回仅在try块本地)
因此,正如其他人所指出的,您可以在try和catch块(但仍在方法内部)之外返回一些东西(例如-1),
或者,您也可以在catch块中有一个return语句,因此即使try抛出异常,您的方法仍然返回double。问题是,如果try块失败,则找不到return语句(因为您提供的返回仅在try块的本地)
因此,正如其他人所指出的,您可以在try和catch块(但仍在方法内部)之外返回一些东西(例如-1),
或者,您也可以在catch块中使用return语句,因此即使try抛出异常,您的方法仍然会返回double。您必须在try-catch块之后添加return
语句以返回默认值,如果发生异常,则必须在try-catch块之后添加return
语句以返回默认值。