Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java—从一个void方法中获取值,然后将值传递到另一个方法_Java - Fatal编程技术网

java—从一个void方法中获取值,然后将值传递到另一个方法

java—从一个void方法中获取值,然后将值传递到另一个方法,java,Java,我想创建一个java项目,从一个方法中获取值,然后将这些值传输到另一个方法中,然后打印出值。如果我将扫描的值放入(void main)中,并使用checkLines方法,它会起作用。但是我想创建一个(Read)方法来获取这些值,然后将这些值转移到一个方法(checkLines)来给出这些值。 问题是第二种方法不读取扫描仪值 import java.util.Scanner; public class Somethin { static double a1,b1,a2,b

我想创建一个java项目,从一个方法中获取值,然后将这些值传输到另一个方法中,然后打印出值。如果我将扫描的值放入(void main)中,并使用
checkLines
方法,它会起作用。但是我想创建一个(Read)方法来获取这些值,然后将这些值转移到一个方法(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);

     }

 }



}

您不需要在
Read()
方法中声明变量
a1
(etc)。它们正在隐藏同名的类级变量。

您不需要在
Read()
方法中声明变量
a1
(etc)。它们对同名的类级别变量进行跟踪。

应该是这样的

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

应该是这样的

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

应该是这样的,但这是一个坏习惯,您需要为您正在讨论的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);

            }


    }



}

应该是这样的,但这是一个坏习惯,您需要为您正在讨论的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);

            }


    }



}

您的方法需要作废有什么原因吗?甚至仅仅从ReadYour
Read
方法的末尾调用检查行也会声明它自己的局部变量,因此没有为您的字段赋值。您的方法为什么需要为空?或者甚至仅仅从ReadYour
Read
方法的末尾调用checkLines就声明了它自己的局部变量,因此没有为您的字段赋值。谢谢,任何好书的建议都会很好。我是一个极端的傻瓜在Java我建议搜索一本关于“Java编程乔伊斯·法雷尔”的电子书她是一个非常有才华的Java程序员老师,她的书是对的,如果这个回答你的问题,请接受它作为一个答案检查代码和它的工作,谢谢。我也会看看这本书。再次感谢汉克先生,任何好书的建议都会很好。我是一个极端的傻瓜在Java我建议搜索一本关于“Java编程乔伊斯·法雷尔”的电子书她是一个非常有才华的Java程序员老师,她的书是对的,如果这个回答你的问题,请接受它作为一个答案检查代码和它的工作,谢谢。我也会看看这本书。再次感谢