“如果”的多重含义陈述

发布于 2024-12-11 02:36:08 字数 1412 浏览 0 评论 0原文

我正在尝试实现如果斜率一为正(大于零)并且斜率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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是偏爱你 2024-12-18 02:36:08

您想要使用 && 运算符来表示“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).

疏忽 2024-12-18 02:36:08

在 Java 中,您正在为 if 语句寻找 AND 运算符,以组合斜率 > 的两个布尔结果。 0且斜率1> 0 转换为 1 布尔值。 AND 运算符是 &&所以尝试一下:

if(scope > 0 && scope1 > 0) {
    scope *= -1;
    scope1 *= -1;
}

其他布尔逻辑运算符是 | (或),& (与), ^ (异或), ! (不),|| (短路或),&& (短路 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:

if(scope > 0 && scope1 > 0) {
    scope *= -1;
    scope1 *= -1;
}

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.

江南月 2024-12-18 02:36:08

; 替换为 &&

if(斜率>0&&斜率1>0)

Replace ; with &&.

if( slope > 0 && slope1 > 0)

能否归途做我良人 2024-12-18 02:36:08

这是错误的:

if ( slope > 0 ; slope1 > 0 ){

您的意思是:

if ( slope > 0 && slope1 > 0 ){

This is wrong:

if ( slope > 0 ; slope1 > 0 ){

Do you mean:

if ( slope > 0 && slope1 > 0 ){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文