+ 1

Help! How to code this problem?

Enter mortgage term in years (1, 2, 3, 5, 10): 7 7 is an invalid term, please re-enter. Enter mortgage term (1, 2, 3, 5, 10): 1 Enter mortgage amortization period (5, 10, 15, 20, 25): 30 30 is an invalid amortization period Enter mortgage amortization period (5, 10, 15, 20, 25): 25

27th Jun 2019, 6:34 PM
czarina smsn
czarina smsn - avatar
10 Réponses
+ 7
Hy czarina smsn Can you explain what we need to do in this ?
27th Jun 2019, 6:52 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
import java.util.Scanner; public class Program{ public static void main(String[] args){ int validTerms[]={1,2,3,5,10}; int validPeriod[]={5,10,15,20,25}; //for taking term & period as input from user Scanner sc=new Scanner(System.in); int term=sc.nextInt(); int period=sc.nextInt(); if(check(validTerms, term)){ //now need to check Period if(check(validPeriod, period)){ System.out.print("you entered valid montgage term & amortization period"); } else{ System.out.print("enter valid amortization period" ); } } else{ System.out.print ("enter valid montgage term"); } } //method for checking term or period is valid or not static boolean check(int array[],int n){ for(int a:array){ if(n==a) return true; } return false; } }
27th Jun 2019, 7:37 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
czarina smsn looks correcrt function(you can write its starting letter with small, and you need to add 1 return statement more as return type is int) but in SL we can take whole input only 1 time only
27th Jun 2019, 9:33 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
I readed about mortgage term & amortization period but still don't understand what you want to do in this ? if I understand correctly, you only want to take 2 inputs, one for mortage term & another for amortization period, and then check if they are available in set of values specified for them respectively ? if that is the case then you need to make 2 array for valid values of mortgage terms & amortization period , then take 2 integer inputs(one for mortgage term & another for amortization period) and store them in 2 different variables, and for checking them you can use if() statements along with || operator and == operator.
27th Jun 2019, 7:23 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
czarina smsn Welcome ☺ //I thought something related to economics here
27th Jun 2019, 7:49 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
source above but in loop: public class Program { static Scanner sc=new Scanner(System.in); static int EnterInt (String msg, int values[]){ int n; while (true) { System.out.println(msg); n = sc.nextInt(); if (check(values, n) ) return n; else System.out.println("Incorrect value "+n); } } //... main()
27th Jun 2019, 9:20 PM
zemiak
+ 2
System.out.print(“Enter mortgage term in years (1, 2, 3, 5, 10): “ If i enter “7” as the output it should say “7 is an invalid term, please re-enter.” because it is invalid it will then ask again to “Enter mortgage term in years (1, 2, 3, 5, 10): “ and if I put 1 for example it will then stop asking that question and ask a new question which is “Enter mortgage amortization period (5, 10, 15, 20, 25): “
27th Jun 2019, 7:29 PM
czarina smsn
czarina smsn - avatar
+ 2
yes exactly i am using if statements along with || and == operator but how do i loop the question to ask again once i put an output that is not valid?
27th Jun 2019, 7:35 PM
czarina smsn
czarina smsn - avatar
+ 2
Thank you very much. I truly appreciate it.
27th Jun 2019, 7:39 PM
czarina smsn
czarina smsn - avatar
+ 1
do i use do-while loop to get two different outputs for mortage term and amortization period?
27th Jun 2019, 6:57 PM
czarina smsn
czarina smsn - avatar