简体   繁体   English

需要帮助-Java Else / If语句

[英]Need Help - Java Else/If Statement

I am currently trying to finish up this Java programming practice project and am stuck... I am not sure what my problem is so can I get some help? 我目前正在尝试完成这个Java编程实践项目,并且陷入困境...我不确定我的问题是什么,所以我可以寻求帮助吗?

Here is the code in full: 这是完整的代码:

package AreaElseIf;

import java.io. *;
import java.util. *;

public class AreaElseIf {

    public static void main(String[] args) {

        final double PI = 3.14;
        Scanner input = new Scanner(System.in);

        System.out.println("This program uses an else/if statement.");

        System.out.println("1 = circle, 2 = square, 3 = rectangle, 4 = triangle.");
        int object = input.nextInt();

        if (input = 1) {
            System.out.println("Enter circle's radius: ");
            double radius = input.nextDouble();
            double cArea = radius * radius * PI;
            System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
        } 
        else if (input = 2) {
            System.out.println("Enter length of square's sides: ");
            double sSide = input.nextDouble();
            double sArea = sSide * sSide;
            System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
        } 
        else if (input = 3) {
            System.out.println("Enter length of rectangle's base: ");
            double base = input.nextDouble();
            System.out.println("Enter length of rectangle's height: ");
            double height = input.nextDouble();
            double rArea = base * height;
            System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
        }
        else if (input = 4) {
            System.out.println("Enter traingle's side length: ");
            double tSide = input.nextDouble();
            double tArea = tSide * tSide * tSide;
            System.out.println("The area of a triangle with a side length of " + tSide + " is " +  tArea + ".");
        }
        else {
        System.out.println("Error: Please enter a number between 1-4 only.");       
        }

    }//end main
}//end class

The purpose of the program is to ask the user to input a number between 1 and 4, and each number is assigned to a shape. 该程序的目的是要求用户输入1到4之间的数字,并且每个数字都分配给一个形状。 If you click on that shape's number, it asked you a couple question in order to calculate the area of said shape. 如果单击该形状的编号,它将询问您几个问题,以计算该形状的面积。 And the point of this program is to do it using else/if. 该程序的重​​点是使用else / if。

The errors that I am getting are only the lines that are "(input = 1,2,3,4...)" input = # are all underlined red. 我得到的错误只是“(input = 1,2,3,4 ...)” input =#的行都带有红色下划线。

The error is worded like this: "Type mismatch. Cannot convert from int to Scanner" "Type mismatch. Cannot convert from Scanner to boolean" 错误的措辞是这样的:“类型不匹配。无法从int转换为Scanner”“类型不匹配。无法从Scanner转换为boolean”

I do not understand what those mean and would love some help with this. 我不明白这些是什么意思,希望在此方面有所帮助。

The first part of your problem is that you used the assignment operator = in each of your if/else statements. 问题的第一部分是您在每个if / else语句中都使用了赋值运算符= To test for equality, use the == operator, as it returns a bool (true if equal, false if not). 要测试是否相等,请使用==运算符,因为它会返回布尔值(如果相等则为true,否则为false)。

The second half of the problem is that you tested your scanner against your int values, where you should've tested the result from the scanner (in your case it looks like object retrieves the user's int). 问题的后半部分是您针对int值对扫描仪进行了测试,您应该在其中对来自扫描仪的结果进行了测试(在这种情况下,看起来就像object检索了用户的int)。

Ultimately your if's should look like: 最终,您的if应该看起来像:

if(object == 1) { ...

Instead of doing 而不是做

if (input = 1)

you need to do 你需要做

if (object == 1)

In your code, input is a Scanner object and you're trying to assign ( = ) an integer to it. 在您的代码中, input是一个Scanner对象,您正在尝试为其分配( = )整数。 Instead, you want to compare the integer input ( object ) you got from your scanner, and compare ( == ) that to an integer value. 相反,您想要比较从扫描仪获得的整数输入( object ),并将其( == )与整数值进行比较。

issue is here if (input = 1) 问题在这里, if (input = 1)

first you trying set input to be 1, not comparing values, second your input is scanner not integer 首先,您尝试将输入设置为1,不比较值,其次,您的输入是扫描仪,而不是整数

to fix it, change to if (object == 1) and it will work as object is integer which you read from scanner, and == is comparison . 要解决此问题,请更改为if (object == 1) ,它将作为object是您从扫描仪读取的整数,而==是comparison。

By addition you could sort your simple problem by using switch/case statement, but that is different story 另外,您可以使用switch / case语句对简单的问题进行排序,但这是另一回事

    if (input == 1) {
        System.out.println("Enter circle's radius: ");
        double radius = input.nextDouble();
        double cArea = radius * radius * PI;
        System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
    } 
    else if (input == 2) {
        System.out.println("Enter length of square's sides: ");
        double sSide = input.nextDouble();
        double sArea = sSide * sSide;
        System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
    } 
    else if (input == 3) {
        System.out.println("Enter length of rectangle's base: ");
        double base = input.nextDouble();
        System.out.println("Enter length of rectangle's height: ");
        double height = input.nextDouble();
        double rArea = base * height;
        System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
    }
    else if (input == 4) {
        System.out.println("Enter traingle's side length: ");
        double tSide = input.nextDouble();
        double tArea = tSide * tSide * tSide;
        System.out.println("The area of a triangle with a side length of " + tSide + " is " +  tArea + ".");
    }
    else {
    System.out.println("Error: Please enter a number between 1-4 only.");       
    }

The equality comparator in Java is == , not = . Java中的相等比较器是== ,不是= And the int variable you want to check is named object , not input 您要检查的int变量名为object ,而不是input

Replace 更换

if (input =

with

if (object ==

Basically you have 基本上你有

Scanner input = new Scanner(System.in);
int object = input.nextInt();
if (input = 1) {

1/ you cannot test input against an int. 1 /您不能针对int测试输入。 object is an int 对象是一个整数

2/ a test is done with '==' not with '=' which is an affectation 2 /测试是用'=='而不是'='来完成的

if (object == 1) {

should fix your problem 应该解决你的问题

For equal comparaison between ints use "==" if (input == 1) { ..true here..} "=" is an assignement operator, == behaves identically for all variables: it tests whether the values of those variables are equal. 对于整数之间的相等比较,请使用“ ==” if(input == 1){..true here ..}“ =”是赋值运算符,==对于所有变量的行为相同:它将测试这些变量的值是否为等于。 In the case of Object obj, obj is a reference to an object. 对于对象obj,obj是对对象的引用。 Since == tests whether two object references have the same value, it is testing whether they refer to the identical object (ie, that the references are equal). 由于==测试两个对象引用是否具有相同的值,因此它正在测试它们是否引用相同的对象(即,引用相等)。 For your application, I advise you to use the switch statement http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 对于您的应用程序,我建议您使用switch语句http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Cheers, Idris. 干杯,伊德里斯。

There are already many answers that guide you to change 已经有很多答案可以指导您进行更改

if(input = 1)

to

if (object == 1)

I would like to suggest another solution: switch() statement. 我想提出另一个解决方案: switch()语句。 Why? 为什么? Switch() is optimized for more than two branches and is generally easier to read (in my opinion). Switch()已针对两个以上的分支进行了优化,并且通常更易于阅读(我认为)。 The code using switch() would look like this: 使用switch()的代码如下所示:

package AreaElseIf;

import java.io. *;
import java.util. *;

public class AreaElseIf {

public static void main(String[] args) {

    final double PI = 3.14;
    Scanner input = new Scanner(System.in);

    System.out.println("This program uses an else/if statement.");

    System.out.println("1 = circle, 2 = square, 3 = rectangle, 4 = triangle.");
    switch( input.nextInt())
    {
        case 1:
            System.out.println("Enter circle's radius: ");
            double radius = input.nextDouble();
            double cArea = radius * radius * PI;
            System.out.println("The area of a circle with the radius of " + radius + " is " + cArea + ".");
            break;

        case 2:
            System.out.println("Enter length of square's sides: ");
            double sSide = input.nextDouble();
            double sArea = sSide * sSide;
            System.out.println("The area of a square with a side length of " + sSide + " is " + sArea + ".");
            break;        

        case 3:
            System.out.println("Enter length of rectangle's base: ");
            double base = input.nextDouble();
            System.out.println("Enter length of rectangle's height: ");
            double height = input.nextDouble();
            double rArea = base * height;
            System.out.println("The area of a rectangle with a base length of " + base + " and a height of " + height + " is " + rArea + ".");
            break;

        case 4: 
            System.out.println("Enter traingle's side length: ");
            double tSide = input.nextDouble();
            double tArea = tSide * tSide * tSide;
            System.out.println("The area of a triangle with a side length of " + tSide + " is " +  tArea + ".");
            break;

        default:
             System.out.println("Error: Please enter a number between 1-4 only.");       
    }
}//end main

note that in this code you don't need (int object) variable. 请注意,在此代码中,您不需要(int对象)变量。

try changing input to object in your if statement. 尝试将input更改为if语句中的object and = to == ===

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM