Question

I have this code that determines the deductible and payout in case of a earthquake based on the Richter scale value entered by the user. Here is what my code currently looks like.

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);
  }

  }
}

I still have some code I've yet to fill in, and I'm sorry if this is too much, I'm very new to the site. If a user enters Y, they get to enter another value of another home, along with the damage scale that home took. Right now, my output is only displaying the latest value that got entered, and not all the other values beforehand. I've read through my book, and I am pretty perplexed as to how I can get my output to display more than just the last user entry. Here is an example of the output!

Please enter your name:  Name

Please enter the insured value of your home:   1000000

Richter Scale   Description of Effect
      8.0       Most structures fall
      7.0       Many buildings destroyed
      6.0       Many buildings considerably damaged, some collapse
      4.5       Damage to poorly constructed buildings
      3.5       Felt by many people, no destruction
       0        Generally not felt by people


Please enter the Richter scale value for the earthquake:  5


Enter 'Y' to continue with another calculation or 'N' to exit:   y

Please enter the insured value of your home:  349999

Richter Scale   Description of Effect
      8.0       Most structures fall
      7.0       Many buildings destroyed
      6.0       Many buildings considerably damaged, some collapse
      4.5       Damage to poorly constructed buildings
      3.5       Felt by many people, no destruction
       0        Generally not felt by people


Please enter the Richter scale value for the earthquake: 6


Enter 'Y' to continue with another calculation or 'N' to exit: n

Damage to poorly constructed buildings                   $          262,499.25
Deductable                                                           87,499.75
                                              TOTAL      $          349,999.00

I want the last part to show up as many as the user enters info. Sorry if I don't know the terminology necessary to make this make more sense. I'm in my first programming course, and it has only been 1 month of learning. Any help to figure this out would be appreciated.

Was it helpful?

Solution

I think you want to move closing brace for the while loop until after your conditions and only print the result after the loop terminates:

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top