是否可以通过将条件写入一个if-指令或类似的东西来减少写入。我的观点是切割源代码并找到更好的方法来对if指令中的信息求和。
import java.util.*;
public class LinGlj4u {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Bitte geben sie den Wert für a ein : ");
double a = input.nextDouble();
System.out.print("Bitte geben sie den Wert für b ein : ");
double b = input.nextDouble();
double rechnung;
if (a + b > 0) {
rechnung = -b / a;
System.out.println(rechnung);
} else if (a + b < 0) {
rechnung = +b / a;
System.out.println(rechnung);
} else if (a + b == 0 || a - b == 0) {
System.out.println("x = 0");
}
}
}
答案 0 :(得分:2)
这是一个简短的版本
import java.util.Scanner;
public class LinGlj4u {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Bitte geben sie den Wert für a ein : ");
double a = input.nextDouble();
System.out.print("Bitte geben sie den Wert für b ein : ");
double b = input.nextDouble();
if(a != 0) {
System.out.println("result is " +(-b/a));
} else {
System.out.println("result is infinity");
}
}
}
答案 1 :(得分:0)
我会将其简化为以下
import java.util.*;
public class LinGlj4u {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Bitte geben sie den Wert für a ein : ");
double a = input.nextDouble();
System.out.print("Bitte geben sie den Wert für b ein : ");
double b = input.nextDouble();
if (a == 0) {
System.out.println("x = 0");
}
else{
System.out.println(-b/a);
}
}
答案 2 :(得分:0)
希望这对你来说足够短。使用正则表达式更好的方式可能就像Jossef在他的例子中所表现的那样。如果条件不重要,那么最后的其他条件也是如此,只需要制作它也可以做到这一点。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// No need to ask twice, seperate inputs by space
System.out.print("Bitte geben sie den Wert für a ein : ");
Double a = input.nextDouble();
Double b = input.nextDouble();
Double rechnung;
if (a != 0)
rechnung = -b/a;
else {
System.out.println("x goes to infinity");
return;
}
System.out.println(rechnung);
}
答案 3 :(得分:-1)
您可以使用以下代码
import java.util.Scanner;
公共类方程{
Product
}