【问题标题】:What am I doing wrong in this program? [duplicate]我在这个程序中做错了什么? [复制]
【发布时间】:2016-10-23 19:57:09
【问题描述】:

我不知道我在这里有什么问题,但它不会编译。我得到的错误信息是 /"else" without "if"/ /not a statement/ /"else" without "if"/

我完全按照我作业中的示例显示的方式进行操作,但我仍然收到此错误。请有人帮助我使用 Math.PI。我完全迷路了。

import java.util.Scanner;

import java.text.DecimalFormat;

public class CircleCalc

{

   public static void main(String[]args)

   {
      Scanner keyboard = new Scanner(System.in);
      double radius;
      double area = Math.PI * radius * radius;
      double circum = 2 * radius * Math.PI;
      DecimalFormat formatter = new DecimalFormat("#0.0000");
      int choice;

      System.out.println("CIRCLE CALCULATOR MENU");
      System.out.println("1) Calculate the Area of a Circle");
      System.out.println("2) Calculate the CIrcumference of a Circle");
      System.out.println("3) Quit the Program");
      System.out.println("Make a selection by choosing a number:");
      choice = keyboard.nextInt();

      if (choice == 1);
      {
         radius = 105.7;
         System.out.println(" The Area of the Circle with radius 105.7 is " + area);
      }

      else if (choice == 2);
      {
         radius = 62.7;
         System.out.println("The Circumference of the Circle with radius 62.7 is " + circum);
      }

      else if (choice == 3);
      {
      System.out.println("You have chosen to quit the program.");
      }

   }
}

【问题讨论】:

  • 去掉if (choice == 1);{ ... }等的分号
  • 语句使用大括号包裹代码后没有分号if {}

标签: java


【解决方案1】:

if (choice == 1); 后面不要加分号,正确的做法是……

if (choice == 1)
{
    radius = 105.7;
    System.out.println(" The Area of the Circle with radius 105.7 is " + area);
}

else if (choice == 2)
{
    radius = 62.7;
    System.out.println("The Circumference of the Circle with radius 62.7 is " + circum);
 }

 else if (choice == 3)
 {
     System.out.println("You have chosen to quit the program.");
 }

【讨论】:

  • 谢谢!但是第一个半径是它没有编译的原因。
  • 另外,它并没有像我想的那样四舍五入到小数点后四位。
  • @Ani:使用货币格式化程序。
猜你喜欢
  • 2022-01-16
  • 2018-10-23
  • 2016-06-02
  • 2021-09-17
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多