2017-02-20 26 views
0

我想創建一個java項目,它接受來自一個方法的值,然後這些值被轉移到另一個方法,然後打印出值,如果我把在(void main)中的掃描值並使用checkLines方法,它將起作用。但是我想創建一個(讀取)方法,它將這些值傳遞給一個方法(checkLines)以提供這些值。 問題是第二種方法不讀取掃描儀值。java-從一個無效方法,然後到另一個方法的值

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

是否有任何理由你的方法需要是無效的?或者甚至只是從讀取 – Eabryt

+0

的末尾調用checkLines你的'Read'方法聲明瞭它自己的局部變量 - 所以你的字段沒有被賦值。 –

回答

1

它應該是這樣的,但這是一個壞習慣,你需要創建get和新類,你在談論你需要了解更多關於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編程喬伊斯法瑞爾」的電子書,她是一位非常有才華的java程序員老師,她的書很重要,如果這回答你的問題,請接受它作爲答案 – abcOfJavaAndCPP

+0

檢查代碼它的工作,謝謝。也將檢查出這本書。再次感謝 – Fawaz

0

您需要在Read()方法未聲明瓦爾a1(ETC)。他們正在遮蓋着同名的班級變量。

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(); 
相關問題