簡體   English   中英

java-從一個void方法中獲取值,然后轉移到另一個方法中

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

我想創建一個Java項目,該項目從一種方法接受值,然后將這些值轉移到另一種方法,然后打印出這些值。如果我將掃描的值放在(void主)中並使用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);

     }

 }



}

應該是這樣

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

            }


    }



}

您無需在Read()方法中聲明vars a1 (等)。 他們用相同的名稱遮蓋了類級var。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM