[英]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.