简体   繁体   中英

java- taking values from one void method then to an another method

I want to create a java project that takes in values from one method and then these values are transferred to an another method and then printing out the values, If i put in the scanned values in the (void main) and use the checkLines method, it would work. But I want to create a ( Read ) Method that takes the values and these values are transferred to a method (checkLines) to give the values. Problem is that The second method doesn't read the scanner values.

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

     }

 }



}

It should be like this.

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

It should be like this but this is a bad habit you need to create new Class for the get and set method that you are talking about you need to learn more about java I suggest you buy a book

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

            }


    }



}

You need to NOT declare the vars a1 (etc) in the Read() method. They are shadowing the class-level vars with the same name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM