1

我有这个代码,它根据用户输入的里氏刻度值确定地震时的免赔额和支出。这是我的代码目前的样子。

import java.util.Scanner;

public class RichterScaleDamage
{
  public static void main(String[] args)
  {
  Scanner userInput = new Scanner(System.in);
  String name = "";
  char answer = 'Y';
  double homeValue = 0;
  double richterScale = 0;
  double payout = 0;
  double deductible = 0;
  String coverage = "";

  System.out.printf("\nPlease enter your name:  ");
  name = userInput.nextLine();


  while(Character.toUpperCase(answer) == 'Y')
  {
    System.out.printf("\nPlease enter the insured value of your home:  ");
    homeValue = userInput.nextDouble();
    userInput.nextLine();

    System.out.printf("\nRichter Scale   Description of Effect"
                 +"\n      8.0       Most structures fall"
                 +"\n      7.0       Many buildings destroyed"
                 +"\n      6.0       Many buildings considerably damaged, some collapse"
                 +"\n      4.5       Damage to poorly constructed buildings"
                 +"\n      3.5       Felt by many people, no destruction"
                 +"\n       0        Generally not felt by people\n\n");

System.out.printf("\nPlease enter the Richter scale value for the earthquake:  ");
richterScale = userInput.nextDouble();
userInput.nextLine();

if(richterScale < 0)
{
  System.out.printf("\nInvalid! Cannot enter negative values");
}
System.out.printf("\n\nEnter \'Y\' to continue with another calculation or \'N\' to exit:  ");
answer = userInput.nextLine().charAt(0);


  }


   if(richterScale >= 8)
  {
    String message = "Most structures fall";
    payout = homeValue * .85;
    deductible = homeValue * .15;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);

    System.out.printf("%s", coverage);
  }
  if(richterScale < 8 && richterScale >= 7)
  {
    String message = "Many buildings destroyed";
    payout = homeValue * .75;
    deductible = homeValue * .25;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);

    System.out.printf("%s", coverage);
  }
  if(richterScale < 7 && richterScale >= 6)
  {
    String message = "Damage to poorly constructed buildings";
    payout = homeValue * .75;
    deductible = homeValue * .25;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);
    System.out.printf("%s", coverage);
  }

  if(richterScale < 6 && richterScale >= 4.5)
  {
    String message = "Many buildings considerably damaged, some collapse";
    payout = homeValue * .65;
    deductible = homeValue * .35;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);
    System.out.printf("%s", coverage);
  }

  if(richterScale < 4.5 && richterScale >= 3.5)
  {
    String message = "Felt by many people, no destruction";
    payout = homeValue * .55;
    deductible = homeValue * .45;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);
    System.out.printf("%s", coverage);
  }

  if(richterScale < 3.5 && richterScale >= 0)
  {
    String message = "Generally not felt by people";
    payout = homeValue * .0;
    deductible = homeValue * .25;
    coverage += String.format("\n%-50s %6s$%,20.2f"
                         +"\nDeductable%47s %,20.2f"
                         +"\n%46sTOTAL %4s $%,20.2f\n",
                          message, " ", payout, " ", deductible, " ", " ", payout + deductible);
    System.out.printf("%s", coverage);
  }

  }
}

我还有一些代码要填写,如果这太多了,我很抱歉,我对这个网站很陌生。如果用户输入 Y,他们可以输入另一个房屋的另一个值,以及该房屋遭受的损坏程度。现在,我的输出只显示输入的最新值,而不是预先显示所有其他值。我已经通读了我的书,对于如何让我的输出显示的不仅仅是最后一个用户条目,我感到非常困惑。这是一个输出示例!

请输入您的姓名:姓名

请输入您家的保险价值:1000000

效果的里氏标度描述
      8.0 大多数建筑物倒塌
      7.0 许多建筑物被毁
      6.0 许多建筑物严重受损,有些倒塌
      4.5 对结构不良的建筑物的损坏
      3.5 多人感受,无破坏
       0 一般人感觉不到


请输入地震的里氏标度值:5


输入“Y”继续另一个计算或“N”退出:y

请输入您家的保险价值:349999

效果的里氏标度描述
      8.0 大多数建筑物倒塌
      7.0 许多建筑物被毁
      6.0 许多建筑物严重受损,有些倒塌
      4.5 对结构不良的建筑物的损坏
      3.5 多人感受,无破坏
       0 一般人感觉不到


请输入地震的里氏标度值:6


输入“Y”继续另一个计算或“N”退出:n

对结构不良的建筑物造成的损害 $262,499.25
免赔额 87,499.75
                                              总计 349,999.00 美元

我希望最后一部分显示与用户输入信息一样多。抱歉,如果我不知道使这更有意义所需的术语。我正在上我的第一个编程课程,而且只有 1 个月的学习时间。任何帮助解决这个问题将不胜感激。

4

1 回答 1

2

我认为您想将 while 循环的右括号移动到您的条件之后,并且仅在循环终止后打印结果:

import java.util.Scanner;

public class RichterScaleDamage {

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        String name = "";
        char answer = 'Y';
        double homeValue = 0;
        double richterScale = 0;
        double payout = 0;
        double deductible = 0;
        String coverage = "";

        System.out.printf("\nPlease enter your name:  ");
        name = userInput.nextLine();


        while (Character.toUpperCase(answer) == 'Y') {
            System.out.printf("\nPlease enter the insured value of your home:  ");
            homeValue = userInput.nextDouble();
            userInput.nextLine();

            System.out.printf("\nRichter Scale   Description of Effect"
                    + "\n      8.0       Most structures fall"
                    + "\n      7.0       Many buildings destroyed"
                    + "\n      6.0       Many buildings considerably damaged, some collapse"
                    + "\n      4.5       Damage to poorly constructed buildings"
                    + "\n      3.5       Felt by many people, no destruction"
                    + "\n       0        Generally not felt by people\n\n");

            System.out.printf("\nPlease enter the Richter scale value for the earthquake:  ");
            richterScale = userInput.nextDouble();
            userInput.nextLine();

            if (richterScale < 0) {
                System.out.printf("\nInvalid! Cannot enter negative values");
            }
            System.out.printf("\n\nEnter \'Y\' to continue with another calculation or \'N\' to exit:  ");
            answer = userInput.nextLine().charAt(0);




            if (richterScale >= 8) {
                String message = "Most structures fall";
                payout = homeValue * .85;
                deductible = homeValue * .15;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);

            }
            if (richterScale < 8 && richterScale >= 7) {
                String message = "Many buildings destroyed";
                payout = homeValue * .75;
                deductible = homeValue * .25;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);

            }
            if (richterScale < 7 && richterScale >= 6) {
                String message = "Damage to poorly constructed buildings";
                payout = homeValue * .75;
                deductible = homeValue * .25;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);
            }

            if (richterScale < 6 && richterScale >= 4.5) {
                String message = "Many buildings considerably damaged, some collapse";
                payout = homeValue * .65;
                deductible = homeValue * .35;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);
            }

            if (richterScale < 4.5 && richterScale >= 3.5) {
                String message = "Felt by many people, no destruction";
                payout = homeValue * .55;
                deductible = homeValue * .45;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);
            }

            if (richterScale < 3.5 && richterScale >= 0) {
                String message = "Generally not felt by people";
                payout = homeValue * .0;
                deductible = homeValue * .25;
                coverage += String.format("\n%-50s %6s$%,20.2f"
                        + "\nDeductable%47s %,20.2f"
                        + "\n%46sTOTAL %4s $%,20.2f\n",
                        message, " ", payout, " ", deductible, " ", " ", payout + deductible);
            }

        }
        System.out.printf("%s", coverage);
    }
}
于 2013-02-22T04:07:22.443 回答