我正在尝试将数组的值传递给我的第二种方法而不是使用switch语句,但我不知道我做错了什么。它一直告诉我标识符是预期的,但我不认为如果我将它们从一个方法传递给另一个方法,我必须再次声明变量。
public static int infoObtainer()
{
int g1, g2, ng1, ng2;
String sgg, sgng, sngg, sngng;
sgg=JOptionPane.showInputDialog("Enter number of years if you both plead guilty: ");
g1=Integer.parseInt(sgg);
sgng=JOptionPane.showInputDialog("Enter number of years if you plead guilty and he pleads innocent: ");
g2=Integer.parseInt(sgng);
sngg=JOptionPane.showInputDialog("Enter number of years if you plead innocent and he pleads guilty: ");
ng1=Integer.parseInt(sngg);
sngng=JOptionPane.showInputDialog("Enter numner of years if you both plead innocent: ");
ng2=Integer.parseInt(sngng);
int[] jailtime = {g1,g2,ng1,ng2};
return jailtime;
}
public static void decisionMaker(jailtime[])
{
if (g1<=ng1)
{
if (g2<=ng2)
{
if (g1<=g2)
{
Sytem.out.println(" Plead Guilty");
}
else
{
System.out.println("Plead Gulity");
}
}
else
if (g1<=ng2)
{
System.out.println("Can't help");
}
}
else
{
if (g2<=ng2)
{
if (ng1<=g2)
{
System.out.println("Can't help");
}
else
{
System.out.println("Plead Guilty");
}
}
else
if (ng1<=ng2)
{
System.out.println("Can't help");
}
else
{
System.out.println("Plead Guilty");
}
}
}
答案 0 :(得分:3)
变化
public static void decisionMaker( jailtime[])
到
public static void decisionMaker(int[] jailtime)
答案 1 :(得分:2)
检查如何在Java here中定义函数。
再次检查方法的签名。 你应该阅读一些基础教程,它快速而有用:)
public static void myFunction (Object myObj) {}
或在您的具体情况
public static void decisionMaker (int[] jailtime) { ... }
请注意,方法中传递的数组的修改值也会因main
中的数组而改变。