java-从一个void方法获取值然后到另一个方法

时间:2017-02-20 15:47:21

标签: java

我想创建一个java项目,它从一个方法中获取值,然后将这些值传输到另一个方法,然后打印出值,如果我在(void main)中放入扫描值并使用checkLines方法,它会起作用。但我想创建一个(读取)方法来获取值,并将这些值传递给方法(checkLines)以提供值。 问题是第二种方法没有读取扫描仪值。

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);

     }

 }



}

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

它应该是这样的,但这是一个坏习惯,你需要为你正在谈论的get和set方法创建新的类,你需要了解更多关于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);

            }


    }



}

答案 2 :(得分:0)

您无需在a1方法中声明变量Read()(等)。他们正在使用相同的名称遮蔽类级变量。