我正在为我的计算机科学课做一个项目。我正在制作一个数学工具,可以做一些事情,比如计算分数,找到区域和周长等。我一直在为我的分数计算器和区域制作函数但是我一直得到一个错误,上面写着“float outputDecimal可能没有被初始化。
import java.util.Scanner;
import static java.lang.System.*;
import javax.swing.*;
import javax.swing.JOptionPane;
public class fractionCalculator
{
static float fracCalc(float num1, float den1, float num2, float den2, char operator)
{
float outputDecimal;
if (operator == 'a') //for fraction addition
{
outputDecimal = (num1/den1) + (num2/den2);
}
if (operator == 's') //for fraction subtraction
{
outputDecimal = (num1/den1) - (num2/den2);
}
if (operator == 'd') //for fraction division
{
outputDecimal = (num1/den1) / (num2/den2);
}
if (operator == 'm') //for fraction multiplication
{
outputDecimal = (num1/den1) * (num2/den2);
}
return outputDecimal;
}
public static void main(String[] args)...
...System.out.printf("\nType in the first numerator: ");
nume1 = read.nextFloat(); //reads the first numerator
read.nextLine();
System.out.printf("\ntype in the first denominator: ");
dene1 = read.nextFloat(); //reads the first denominator
read.nextLine();
System.out.printf("\ntype in the second numerator: ");
nume2 = read.nextFloat(); //reads the second numerator
read.nextLine();
System.out.printf("\ntype in the second denominator: ");
dene2 = read.nextFloat(); //reads the second denominator
read.nextLine();
finalDecimal = fracCalc(nume1, dene1, nume2, dene2, fracMode);
System.out.printf(" = " + finalDecimal);
}
有人知道为什么fracCalc函数中if语句内的初始化不起作用吗?