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 に答える