“如果”的多重含义陈述
我正在尝试实现如果斜率一为正(大于零)并且斜率1为正乘以-1
'线程“main”java.lang.Error中的异常:未解决的编译问题: 标记“;”上的语法错误,。预期的 lope1 无法解析或者不是
LinearSlopeFinder.main(LinearSlopeFinder.java:25) 中的 字段 ;
我尝试过使用“,”代替,但没有骰子
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
double x1, y1, x2, y2, n1, equation, constant = 0 ;
double slope, slope1, slopeAns;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? example: x,y ... ");
String coordinate1 = myScanner.nextLine();
String coordinates[] = coordinate1.split(",");
x1 = Integer.parseInt(coordinates[0]);
y1 = Integer.parseInt(coordinates[1]);
System.out.print(" What is the second set of cordinants? example: x,y ... ");
String coordinate2 = myScanner.nextLine();
String coordinates1[] = coordinate2.split(",");
x2 = Integer.parseInt(coordinates1[0]);
y2 = Integer.parseInt(coordinates1[1]);
//remember it is Rise over Run Y's over X's
slope = (y1-y2);
slope1= (x1-x2);
slopeAns= slope / slope1 ;
//below is the part that is not compiling but I am trying to insert
if ( slope > 0 ; slope1 > 0 ){
slope = slope * -1;
slope1 = slope1 * -1;
}
I am trying to implement if slope one is positive(greater than zero) and slope1 is positive multiply by -1
'Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token ";", . expected
slope1 cannot be resolved or is not a field
at LinearSlopeFinder.main(LinearSlopeFinder.java:25)
;
i have tried using an "," instead but no dice
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
double x1, y1, x2, y2, n1, equation, constant = 0 ;
double slope, slope1, slopeAns;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? example: x,y ... ");
String coordinate1 = myScanner.nextLine();
String coordinates[] = coordinate1.split(",");
x1 = Integer.parseInt(coordinates[0]);
y1 = Integer.parseInt(coordinates[1]);
System.out.print(" What is the second set of cordinants? example: x,y ... ");
String coordinate2 = myScanner.nextLine();
String coordinates1[] = coordinate2.split(",");
x2 = Integer.parseInt(coordinates1[0]);
y2 = Integer.parseInt(coordinates1[1]);
//remember it is Rise over Run Y's over X's
slope = (y1-y2);
slope1= (x1-x2);
slopeAns= slope / slope1 ;
//below is the part that is not compiling but I am trying to insert
if ( slope > 0 ; slope1 > 0 ){
slope = slope * -1;
slope1 = slope1 * -1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要使用
&&
运算符来表示“and”。我建议您阅读 Java 教程的运算符部分(休息也很有价值)。You want to use the
&&
operator for 'and'. I recommend you read the operators section of the Java tutorial (the rest is valuable too).在 Java 中,您正在为 if 语句寻找 AND 运算符,以组合斜率 > 的两个布尔结果。 0且斜率1> 0 转换为 1 布尔值。 AND 运算符是 &&所以尝试一下:
其他布尔逻辑运算符是 | (或),& (与), ^ (异或), ! (不),|| (短路或),&& (短路 AND)、==(等于)、!=(不等于)、?:(IF-THEN-ELSE)。
| 之间的区别和 ||在 Java 中,如果第一个语句被证明为 true,那么它不会使用 || 计算第二个或更多语句。但它会与 | 一起使用。 && 也是如此。如果第一个语句等于 false。
In Java you are looking for an AND operator for the if statement to combine the two Boolean results from slope > 0 and slope1 > 0 into one Boolean. The AND operator is && so try:
Other Boolean logical operators are | (OR), & (AND), ^ (XOR), ! (NOT), || (short-circuit OR), && (short-circuit AND), == (EQUAL TO), != (NOT EQUAL TO), ?: (IF-THEN-ELSE).
The difference between | and || is that in Java if the first statement turns out to be true then it will not evaluate the second or more statement with || but it will with |. The same goes for && if the first statement is equal to false.
将
;
替换为&&
。if(斜率>0&&斜率1>0)
Replace
;
with&&
.if( slope > 0 && slope1 > 0)
这是错误的:
您的意思是:
This is wrong:
Do you mean: