简体   繁体   中英

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?

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. 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.

The errors that I am getting are only the lines that are "(input = 1,2,3,4...)" input = # are all underlined red.

The error is worded like this: "Type mismatch. Cannot convert from int to Scanner" "Type mismatch. Cannot convert from Scanner to 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. To test for equality, use the == operator, as it returns a bool (true if equal, false if not).

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).

Ultimately your if's should look like:

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. Instead, you want to compare the integer input ( object ) you got from your scanner, and compare ( == ) that to an integer value.

issue is here if (input = 1)

first you trying set input to be 1, not comparing values, second your input is scanner not integer

to fix it, change to if (object == 1) and it will work as object is integer which you read from scanner, and == is comparison .

By addition you could sort your simple problem by using switch/case statement, but that is different story

    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 = . And the int variable you want to check is named object , not 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. object is an int

2/ a test is done with '==' not with '=' which is an affectation

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. In the case of Object obj, obj is a reference to an object. 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

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. Why? Switch() is optimized for more than two branches and is generally easier to read (in my opinion). The code using switch() would look like this:

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.

try changing input to object in your if statement. and = to ==

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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