【问题标题】:Need Help - Java Else/If Statement需要帮助 - Java Else/If 语句
【发布时间】:2014-09-17 08:32:33
【问题描述】:

我目前正在尝试完成这个 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 来完成它。

我得到的错误只是“(输入= 1,2,3,4 ...)”的行 input = # 都是红色下划线。

错误的措辞如下: “类型不匹配。无法从 int 转换为 Scanner” “类型不匹配。无法从 Scanner 转换为布尔值”

我不明白这些是什么意思,希望得到一些帮助。

【问题讨论】:

  • Ahhhhhhhh 我刚刚发现了我的问题。再次感谢大家的帮助。我现在完全明白了。

标签: java eclipse if-statement ide


【解决方案1】:

问题的第一部分是您在每个 if/else 语句中都使用了赋值运算符 =。要测试是否相等,请使用 == 运算符,因为它返回一个布尔值(如果相等,则返回 true,否则返回 false)。

问题的后半部分是您针对您的 int 值测试了您的扫描仪,您应该测试扫描仪的结果(在您的情况下,它看起来像 object 检索用户的 int)。

最终你的 if 应该是这样的:

if(object == 1) { ...

【讨论】:

    【解决方案2】:

    而不是做

    if (input = 1)
    

    你需要做的

    if (object == 1)
    

    在您的代码中,input 是一个 Scanner 对象,而您正尝试为其分配一个整数 (=)。相反,您想比较从扫描仪获得的整数输入 (object),并将 (==) 与整数值进行比较。

    【讨论】:

      【解决方案3】:

      问题在这里if (input = 1)

      首先您尝试将输入设置为 1,而不是比较值,其次您的输入是扫描仪而不是整数

      要修复它,请更改为if (object == 1),它会工作,因为object 是您从扫描仪读取的整数,而== 是比较。

      通过加法,您可以使用 switch/case 语句对简单问题进行排序,但这是不同的故事

      【讨论】:

        【解决方案4】:
            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.");       
            }
        

        【讨论】:

        • 他的输入是扫描仪,所以它永远不会等于任何整数,
        【解决方案5】:

        Java 中的相等比较器是==,而不是=。 而您要检查的int 变量名为object,而不是input

        替换

        if (input =
        

        if (object ==
        

        【讨论】:

          【解决方案6】:

          基本上你有

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

          1/ 您不能针对 int 测试输入。 object 是一个 int

          2/ 测试是用 '==' 完成的,而不是用 '=' 做的,这是一种矫揉造作

          if (object == 1) {
          

          应该解决你的问题

          【讨论】:

            【解决方案7】:

            对于整数之间的相等比较,请使用 "==" if (input == 1) { ..true here..} “=”是一个赋值运算符, == 对所有变量的行为相同:它测试这些变量的值是否相等。在 Object obj 的情况下,obj 是对对象的引用。由于 == 测试两个对象引用是否具有相同的值,它正在测试它们是否引用相同的对象(即,引用是否相等)。 对于你的应用,我建议你使用 switch 语句http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

            干杯, 伊德里斯。

            【讨论】:

              【解决方案8】:

              已经有很多答案可以引导你改变

              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 object) 变量。

              【讨论】:

              • 虽然我确实更喜欢 switch() 语句,但 OP 确实说“这个程序的重点是使用 else/if 来完成它。”
              • 哎呀,没错@Codesmith 我错过了。谢谢指正。
              【解决方案9】:

              尝试在 if 语句中将 input 更改为 object。和===

              【讨论】:

                猜你喜欢
                • 2015-03-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多