gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:01:01 29 4
gpt4 key购买 nike

我想创建一个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);

}

}



}

最佳答案

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

}


}



}

关于java-从一个 void 方法获取值然后到另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348818/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com