2015-11-19 31 views
-1
  1. 允許用戶輸入兩個整數a和b。有兩種方法稱爲sum和product,當它們與輸入a和b一起使用時,它們返回它們的總和和產品。
  2. 另一部分允許用戶輸入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); 
+0

@David他在sysout中主要調用它們。 –

+0

什麼不是在你的第一次嘗試?你的第二次嘗試看起來像方法是無限遞歸的(儘管,謝天謝地,從未最初調用過)。 – David

+0

「我得到的變量錯誤」並不是特定的。請向我們展示您的期望以及您所看到的內容。 – BadZen

回答

0

對於第二部分,我選擇在類的方法中使用您的代碼,並從另一個類中調用實例。 下面的代碼

package StackOverFlow; 


import javax.swing.JOptionPane; 

class Discriminant{ 

    public double Discriminant(){ 
double a, b, c,d; 
float e,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; 
    e=(float) Math.sqrt(d); 
    r= (float) ((b*(-1) + e)/(2*a)); 
    r = (float) (Math.round(r*100)/100.0); 
    rr= (float) ((b*(-1) - e)/(2*a)); 
    rr = (float) (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 + ")"); 
    if (d>0){ 
     System.out.println("The roots are " + r + " and " + rr); 
    } 
    else if (d<0){ 
     System.out.print("There r not roots."); 
    } 


    return d; 
} 
} 

與主類,調用該實例:

package StackOverFlow; 


    public class test { 


    public static void main(String[] args) { 
     double dis=0; 
Discriminant a=new Discriminant(); 
dis=a.Discriminant(); 
System.out.print("\n\n\n The discriminant is" + dis); 
    }} 

我希望通過「我不知道如何將它做成一個方法,」你的意思是像那。 這是我的Java幫助。現在我必須修復根x1 = [b *( - 1)+ sqrD]/2 * a和x2 = [b *( - 1)-sqrD]/2 * a

另外我們忘記了當Discriminant是消極的,沒有根。 我也修復了根(你需要d,r,rr作爲浮點數),並且我添加了if語句。現在如果D < 0,用戶得到一條消息,說明如果D> 0,那麼根就會被打印。

+0

你想從實例中返回什麼以進一步計算? (這裏我只返回判別式) –

+0

另一個意見是,你用-b代替b *( - 1) –

+0

我該如何結合這兩個代碼?我知道主要的課程在頂部,其餘的在中間,但我不斷收到錯誤。非常感謝 –

0

這裏是第一部分。這裏有一些改動。

package StackOverFlow; 

import javax.swing.JOptionPane; 

class quadratics { 



    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[]){ 
    String input1=JOptionPane.showInputDialog("Please enter an integer"); 
    int a=Integer.parseInt(input1); //Declaration and put value in the same statement. 
    String input2=JOptionPane.showInputDialog("Please enter another integer"); 
    int b=Integer.parseInt(input2);//Declaration and put value in the same statement. 
     quadratics m = new quadratics(); 
     System.out.println(a+" " + b+ " " + m.sum1(a,b)); 
     System.out.println(a+" " + b+ " " + m.product1(a,b)); 
    } 
} 

請記住,您必須根據要保存它們的文件夾或包來修復第一行(包)。

相關問題