簡體   English   中英

需要幫助-Java Else / If語句

[英]Need Help - Java Else/If Statement

我目前正在嘗試完成這個Java編程實踐項目,並且陷入困境...我不確定我的問題是什么,所以我可以尋求幫助嗎?

這是完整的代碼:

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

該程序的目的是要求用戶輸入1到4之間的數字,並且每個數字都分配給一個形狀。 如果單擊該形狀的編號,它將詢問您幾個問題,以計算該形狀的面積。 該程序的重​​點是使用else / if。

我得到的錯誤只是“(input = 1,2,3,4 ...)” input =#的行都帶有紅色下划線。

錯誤的措辭是這樣的:“類型不匹配。無法從int轉換為Scanner”“類型不匹配。無法從Scanner轉換為boolean”

我不明白這些是什么意思,希望在此方面有所幫助。

問題的第一部分是您在每個if / else語句中都使用了賦值運算符= 要測試是否相等,請使用==運算符,因為它會返回布爾值(如果相等則為true,否則為false)。

問題的后半部分是您針對int值對掃描儀進行了測試,您應該在其中對來自掃描儀的結果進行了測試(在這種情況下,看起來就像object檢索了用戶的int)。

最終,您的if應該看起來像:

if(object == 1) { ...

而不是做

if (input = 1)

你需要做

if (object == 1)

在您的代碼中, input是一個Scanner對象,您正在嘗試為其分配( = )整數。 相反,您想要比較從掃描儀獲得的整數輸入( object ),並將其( == )與整數值進行比較。

問題在這里, if (input = 1)

首先,您嘗試將輸入設置為1,不比較值,其次,您的輸入是掃描儀,而不是整數

要解決此問題,請更改為if (object == 1) ,它將作為object是您從掃描儀讀取的整數,而==是comparison。

另外,您可以使用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.");       
    }

Java中的相等比較器是== ,不是= 您要檢查的int變量名為object ,而不是input

更換

if (input =

if (object ==

基本上你有

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

1 /您不能針對int測試輸入。 對象是一個整數

2 /測試是用'=='而不是'='來完成的

if (object == 1) {

應該解決你的問題

對於整數之間的相等比較,請使用“ ==” if(input == 1){..true here ..}“ =”是賦值運算符,==對於所有變量的行為相同:它將測試這些變量的值是否為等於。 對於對象obj,obj是對對象的引用。 由於==測試兩個對象引用是否具有相同的值,因此它正在測試它們是否引用相同的對象(即,引用相等)。 對於您的應用程序,我建議您使用switch語句http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

干杯,伊德里斯。

已經有很多答案可以指導您進行更改

if(input = 1)

if (object == 1)

我想提出另一個解決方案: switch()語句。 為什么? Switch()已針對兩個以上的分支進行了優化,並且通常更易於閱讀(我認為)。 使用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

請注意,在此代碼中,您不需要(int對象)變量。

嘗試將input更改為if語句中的object ===

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM