###前提・実現したいこと
一年間の保険の合計金額を表示する問題です。15歳以下は保険に加入することができない、16から19歳は15%増、20から24は5%増、25歳以上は10%引きという設定です(ベースは1000ドル)。まず、家族が何人かを聞いて、年齢を一人ずつ聞いてarrayに記録し、その合計を表示しようと思ったのですがエラーが出てしまいます。
Java初心者なのであまりループなどおはずかしながら把握しておりません。
You are to write a program for small insurance company. The program is supposed to determine the cost of automobile insurance premium for each family member, based on their ages. The basic insurance price is $1,000 per year per person plus an additional amount based on age, per the table below.
16 to 19 Additional 15% of basic price
20 to 25 Additional 5% of basic price
Over 25 Discount 10% of basic price
Any other age No insurance
###発生している問題・エラーメッセージ
Loopにならず、エラーが出ます。
年齢を聞かれ、入力するとエラーがでます。
###該当のソースコード
import java.util.*;
public class Totalprice
{
public static void main(String args[])
{
try(Scanner scanner= new Scanner(System.in))
{
System.out.println("How many people in your family?");
int number = scanner.nextInt();
int a[][] = new int[2][number];
for(int x = 0; x < number; x++) { System.out.print("Enter their age"); int age = scanner.nextInt(); if(age <= 15) { System.out.println("No insurance needed"); a[x][number] = 0; return; } if(age >= 16 || age <= 19) a[x][number] = 1150; if(age >= 20 || age <= 24) a[x][number] = 1050; if(age >= 25) a[x][number] = 900; int sum = 0; sum += a[x][number]; System.out.println("The total price is" + sum + "$"); } } }
}
回答4件
あなたの回答
tips
プレビュー