2017-09-21 105 views
0

的結果這是我的第一篇文章在這裏,所以請原諒我對任何格式錯誤。彙總不同的計算

因此,大家可以看到我的程序要求性別,事故和汽車的年#顯示虛構保險報價。

基於所有這些信息,我需要保險費用的小計添加到末尾。

我有我的代碼工作,直到總成本評論(貼吧所有以供參考)。我被困在那裏,因爲性別有不同的基數。我試圖找出一種方法來只做一個if語句,如果它匹配用戶輸入的性別。

任何想法?

import java.util.*; 
public class Insurance { 
    public static void main(String [] args) { 
    Scanner scanner = new Scanner(System.in); 

    int currentYear = 2017; //used for calculating the age of the users car 
    int maleGender = 1000; 
    int femaleGender = 500; 

    //Letting user know they are inputting data for car insurance purposes 
    System.out.println("Car insurance questionnaire. Please input correct information when prompted."); 

    // gender information from user 
    System.out.println("What is your gender? m/f"); 
    String gender = scanner.next(); 

    // accident quantity information from user 
    System.out.println("How many accidents have you had?"); 
    int acc = scanner.nextInt(); 

    // car year information from user 
    System.out.println("What year was your car manufactured?"); 
    int carAge = scanner.nextInt(); 

    //if statements which refer to the users data input 
    if (gender.equals("m")) { 
    System.out.println("You are a male.\nThe base cost is $1000."); 
    } else { 
    System.out.println("You are a female.\nThe base cost is $500."); 
    } 


    if (acc == 0) { 
    System.out.println("You have no accidents. Insurance increase is $0."); 
    } else if (acc >= 1) { 
    System.out.println("You have " + acc + " accidents. Insurance increase is $" + acc * 100 + "."); 
    } 


    if (carAge >= 2007) { 
     System.out.println("Your car is " + (currentYear - carAge) + " years old.\nYour car is still in warranty, no savings added."); 
     } else 
     System.out.println("Your car is out of warranty, final cost is halved."); 

     //Total cost 
    /* 
     if (carAge <= 2007) { 
     System.out.println("Your total price is $" + ((acc * 100 + femaleGender)/2) + "."); 
     } else 
     System.out.println("Your total price is $" + (acc * 100 + femaleGender) + "."); 
     */ 


} 
} 
+0

這將是更容易爲Java編譯器來了解你想要什麼,如果你寫下你的意思(「基本費用爲$ 1000」),不僅在自然語言,但在代碼('INT小計;小計= 1000;' )。 – blafasel

回答

0

我不能完全確定要如何計算你的結果,但如果不想使用femaleGender所有的時間,但在性別不同值的依賴性那麼也許這樣的事情可以幫助:

int baseAmount = gender.equals("m") ? maleGender : femaleGender; 
if (carAge <= 2007) { 
    System.out.println("Your total price is $" + ((acc * 100 + baseAmount)/2) + "."); 
} else 
    System.out.println("Your total price is $" + (acc * 100 + baseAmount) + "."); 
} 
+0

完美運作。非常感謝你! – GChittick

0
int genderCost; 

... 

if (gender.equals("m")) { 
    System.out.println("You are a male.\nThe base cost is $1000."); 
    genderCost = maleGender; 
} else { 
    System.out.println("You are a female.\nThe base cost is $500."); 
    genderCost = femaleGender; 
} 

... 

if (carAge <= 2007) { 
    System.out.println("Your total price is $" + ((acc * 100 + genderCost)/2) + "."); 
} else 
    System.out.println("Your total price is $" + (acc * 100 + genderCost) + "."); 
} 

將性別的量在一個變量genderCost當性別輸入變量進行評估,並使用genderCost當計算總。

+0

這也工作,非常感謝你,我感謝你的時間! :) – GChittick