我有这段代码,可根据用户输入的里氏标度值确定发生地震时的免赔额和赔付额。这是我的代码当前的样子。

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


我希望最后一部分显示的内容与用户输入的信息一样多。很抱歉,如果我不知道使这更有意义的必要术语。我正在上第一门编程课程,而且只有一个月的学习时间。任何帮助找出这一点将不胜感激。

最佳答案

我认为您希望将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);
    }
}

关于java - 前哨控制回路以及如何打印出多个字符串。初学者Java编程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15016173/

10-13 07:33