2017-02-20 23 views
0

1つのメソッドから値を取り込むJavaプロジェクトを作成し、これらの値を別のメソッドに転送してから値を出力します。 (ボイドメイン)のスキャンした値でcheckLinesメソッドを使用すると正常に動作します。しかし、私は値を取る(Read)メソッドを作成したい、そして、これらの値は値を与えるためにメソッド(checkLines)に転送されます。 問題点は、2番目の方法ではスキャナの値を読み取れないことです。1つのvoidメソッドから別のメソッドへの値の取得

import java.util.Scanner; 

public class Somethin { 
      static double a1,b1,a2,b2; 
    public static void main(String[] args) { 
    Read(a1,b1,a2,b2); 
    checkLines(a1,b1,a2,b2); 
    } 

    public static void Read(double a, double b, double c , double d){ 

     Scanner scan = new Scanner(System.in); 

     System.out.print("Please enter a1: "); 
     double a1 = scan.nextDouble(); 

     System.out.print("Please enter b1: "); 
     double b1 = scan.nextDouble(); 

     System.out.print("Please enter a2: "); 
     double a2 = scan.nextDouble(); 

     System.out.print("Please enter b2: "); 
     double b2 = scan.nextDouble(); 

     scan.close(); 
    } 

    public static void checkLines (double a1 , double b1, double a2, double b2){ 

    if (a1 == a2) { 

     System.out.println("Line 1 and Line 2 are parallel"); 
    } 

    if (a1 * a2 == -1){ 
     System.out.println("Line 1 and Line 2 are perpendicular"); 
    } else { 
     System.out.println(" The lines are intersecting"); 
     System.out.println(" There points of intersection are"); 
     double x = ((b2 - b1)/(a2-a1)); 
     double y = (a1*(x) + b1); 
     System.out.println(" X = " +x); 
     System.out.println(" y = " + y); 

    } 

} 



} 
+0

無効にする必要がある理由はありますか?またはReadの末尾からcheckLinesを呼び出すだけでも – Eabryt

+0

あなたの 'Read'メソッドはそれ自身のローカル変数を宣言しているので、あなたのフィールドは割り当てられません。 –

答えて

1

それはこのようにする必要がありますが、これはあなたがあなたについて話しているの取得と設定方法のための新しいクラスを作成する必要が悪い癖である私は、あなたが本

を買うことを提案するJavaについての詳細を学ぶ必要があります
import java.util.Scanner; 
public class Somethin 
{ 
    //Declare data fields 
    //Outside of every method to prevent creating local variables 
    private static double a1,b1,a2,b2; 
    public static void main(String[] args) 
    { 
     //setRead() method call 
     setRead(); 
     //checkLines() method call 
     checkLines(); 

    } 
    public static void setRead() 
    { 
     //This method ask the user for input 
     Scanner scan = new Scanner(System.in); 
     System.out.print("Please enter a1: "); 
     a1 = scan.nextDouble(); 
     System.out.print("Please enter b1: "); 
      b1 = scan.nextDouble(); 

      System.out.print("Please enter a2: "); 
      a2 = scan.nextDouble(); 

      System.out.print("Please enter b2: "); 
      b2 = scan.nextDouble(); 
    } 
    public static Double getA1() 
    { 
     //get method return a double value not void 
     return a1; 

    } 
    public static Double getB1() 
    { 
     //get method return a double value not void 
     return b1; 

    } 
    public static Double getA2() 
    { 
     //get method return a double value not void 
     return a2; 

    } 
    public static Double getB2() 
    { 
     //get method return a double value not void 
     return b2; 

    } 
    public static void checkLines() 
    { 
     //This method display the inputted values 
     if(getA1()==getA2()) 
     { 
      System.out.println("Line 1 and Line 2 are parallel"); 
     } 
     if(getA1()*getA2()==-1) 
     { 
      System.out.println("Line 1 and Line 2 are perpendicular"); 
      } 
     else 
     { 
       System.out.println(" The lines are intersecting"); 
       System.out.println(" There points of intersection are"); 
       double x = ((getB2() - getB1())/(getA2()-getA1())); 
       double y = (getA1()*(x) + getB1()); 
       System.out.println(" X = " +x); 
       System.out.println(" y = " + y); 

      } 


    } 



} 
+0

ありがとう、良い本の提案は素晴らしいだろう。私はJavaの極端な暗記です – Fawaz

+0

"Java Programming Joyce Farrell"についての電子ブックを検索することをお勧めします。これはあなたの質問に答えがある場合は – abcOfJavaAndCPP

+0

コードをチェックしてください、彼女は非常に才能のあるJavaプログラマーの先生です。ありがとうございました。また、本をチェックアウトします。再度、感謝します – Fawaz

0

あなたはRead()方法でVARS a1(など)を宣言しないでする必要があります。彼らは同じレベルのクラスレベルのヴァールをシャドーイングしています。

1

このようにする必要があります。

Scanner scan = new Scanner(System.in); System.out.print("Please enter a1: "); 
a1 = scan.nextDouble(); System.out.print("Please enter b1: "); 
b1 = scan.nextDouble(); System.out.print("Please enter a2: "); 
a2 = scan.nextDouble(); System.out.print("Please enter b2: "); 
b2 = scan.nextDouble(); 
関連する問題