- 允許用戶輸入兩個整數a和b。有兩種方法稱爲sum和product,當它們與輸入a和b一起使用時,它們返回它們的總和和產品。
- 另一部分允許用戶輸入3個實數a,b和c。使用Discriminant方法(輸入a(!= 0),b,c)返回D = b^2-4ac的值。如果a = 0,則判別式和根不存在。計劃應該輸出如下:
- 你已經進入了一個= ......,B = ...,C = ...
- 二次方程是AXX + BX + C = 0
- 判別是d = {...使用下式d = b * b-4計算出的d *一個* C}
程序的主體的內部,利用條件語句檢查以下內容:Java中的方法有/無返回用戶輸入,簡單但是新編程
- 如果d是正的呼叫的方法TwoRoots到兩個值返回根根部
輸出是 X1 = -b + SQR d/2a和X 1 = -b-SQR d/2A
- 如果d爲負輸出是沒有真正的根
我做的第一部分,但它無法正常工作。這是我第一次使用方法,它有點混亂,我只知道如何在沒有用戶輸入的情況下使用它們。但我需要用戶輸入。 這些是我嘗試過的代碼:第一個代碼是使用返回方法,第二個代碼是無返回方法。
class quadratics1 {
int a,b;
String input1=JOptionPane.showInputDialog("Please enter an integer");
a=Integer.parseInt(input1);
String input2=JOptionPane.showInputDialog("Please enter another integer");
b=Integer.parseInt(input2);
int sum1(int L, int W){
int sum=(L+W);
return sum;
}
int product1(int L, int W){
int product=2*(L+W);
return product;
}
public static void main(String str[]){
quadratics1 m = new quadratics1();
System.out.println(a+" " + b+ " " + m.sum1(a,b));
System.out.println(a+" " + b+ " " + m.product1(a,b));
}
}
我得到的錯誤是:標識預期的變量A和B線4和6
我也試過這樣:
import javax.swing.*;
class quadratics1{
void sum(int L, int W){
int sum1=(L+W);
System.out.println(a+" " + b+ " " + m.sum1(L,W));
}
void product(int L, int W){
int product1=2*(L+W);
System.out.println(a+" " + b+ " " + m.product1(L,W));
}
public static void main(String str[]){
int a,b;
quadratics1 m = new quadratics1();
String input1=JOptionPane.showInputDialog("Please enter an integer");
a=Integer.parseInt(input1);
String input2=JOptionPane.showInputDialog("Please enter another integer");
b=Integer.parseInt(input2);
}
}
我不知道我是什麼做錯了,我得到了變量的錯誤。任何幫助將不勝感激。我一直在試圖弄清楚這幾天。我是新來的編碼,我必須在明天完成這項工作
對於第二部分,我爲方程式編寫了這段代碼,但是我不知道如何使它成爲方法。
double a, b, c,d,r,rr;
String input1 = JOptionPane.showInputDialog("Please, enter the first real number");
a = Double.valueOf(input1).doubleValue();
String input2 = JOptionPane.showInputDialog("Please, enter the second real number");
b = Double.valueOf(input2).doubleValue();
String input3 = JOptionPane.showInputDialog("Please, enter the third real number");
c = Double.valueOf(input3).doubleValue();
d= Math.pow(b,2) - 4*a*c;
d = Math.round(d*100)/100.0;
r= (-b + Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
r = Math.round(r*100)/100.0;
rr= (-b - Math.sqrt(Math.pow(b,2) - (4*a*c)))/(2*a);
rr = Math.round(rr*100)/100.0;
System.out.println("You have entered a = " + a + " b = " + b + " c = " + c);
System.out.println("The quadratic equation is " + a + "x^2 " + b + "x " + c + " = 0");
System.out.println("The discriminant is D= " + d + " D is calculated using the formula D= " + b + "^2 " + " - 4" + "("+ a + ")" + "("+ c + ")");
System.out.println("The roots are " + r + " and " + rr);
@David他在sysout中主要調用它們。 –
什麼不是在你的第一次嘗試?你的第二次嘗試看起來像方法是無限遞歸的(儘管,謝天謝地,從未最初調用過)。 – David
「我得到的變量錯誤」並不是特定的。請向我們展示您的期望以及您所看到的內容。 – BadZen