I really need help with this problem involving loops

+1 Owen Coutts · November 8, 2015
What I am trying to do is replace all the || in my if statement with a for loop. I want my program to look something like this:

year 1
month 1: (data here)
month 2:
month3 :

and so on until the month reaches 12 and then start over but with year 2. any help would be greatly appreciated, thanks! (Problem in code is the only comment)

package test;

import java.util.Scanner;

?

public class bannanas {
public static void main(String[] args){

Scanner p = new Scanner(System.in);
Scanner r = new Scanner(System.in);
Scanner c = new Scanner(System.in);

double amount;
double amountRounded;
double rate;
int years = 1;
int year = 1;
double principal;
double monthlyRate;
int counter;
int counter2;
int monthc = 1;


System.out.println("Enter current account balance:");
principal = p.nextDouble();

System.out.println("Enter yearly interst rate:");
rate = r.nextDouble();
monthlyRate = rate / 12;

System.out.println("Enter years calculated:");
counter2 = c.nextInt();
counter2 = counter2*12;

for(int month =1; month<= counter2;month++){


//this line if( years == ){



// to replace this line: if(years == 1 || years == 13 || years == 25 || years == 37 || years == 49 || years == 61 || years == 73 || years == 85 ){

System.out.println("Year " + year );
year++;
monthc= 1;

}
years++;
amount = principal*Math.pow(1 + monthlyRate, month);
amountRounded = Math.round(amount * 100.0) /100.0;
System.out.println(" - Month " + monthc + " " + amountRounded);

monthc++;
}

}

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Owen Coutts · November 11, 2015
yes that would be great thank you so much!
0 axel vincent · November 12, 2015
I made this. See if that is correct to you


import java.util.Scanner;

public class Bannanas {

    private static double month;

    public static void main(String[] args) {
        
        @SuppressWarnings("resource")
        Scanner p = new Scanner(System.in);
        @SuppressWarnings("resource")
        Scanner r = new Scanner(System.in);
        @SuppressWarnings("resource")
        Scanner c = new Scanner(System.in);

        double amount;
        double amountRounded;
        double rate;
        @SuppressWarnings("unused")
        int years = 1;
        int year = 1;
        double principal;
        double monthlyRate;
        @SuppressWarnings("unused")
        int counter;
        int counter2;
        int monthc = 1;


        System.out.println("Enter current account balance:");
        principal = p.nextDouble();

        System.out.println("Enter yearly interst rate:");
        rate = r.nextDouble();
        monthlyRate = rate / 12;

        System.out.println("Enter years calculated:");
        counter2 = c.nextInt();
        counter2 = counter2*12;

        for(int month =1; month<= counter2;month++){


        //this line if( years == ){



        // to replace this line: if(years == 1 || years == 13 || years == 25 || years == 37 || years == 49 || years == 61 || years == 73 || years == 85 ){

        System.out.println("Year " + year );
        year++;
        monthc= 1;

        }
        years++;
        amount = principal*Math.pow(1 + monthlyRate, month);
        amountRounded = Math.round(amount * 100.0) /100.0;
        System.out.println(" - Month " + monthc + " " + amountRounded);

        monthc++;
        }

}
0 Owen Coutts · November 14, 2015
I just want this but an easy way to replace the line in comments with a for loop or something also how could i only use 1 scanner when i need multiple different inputs? Thanks!
0 Owen Coutts · November 14, 2015
Thank you axel but it does not work, months do not display and years are out of control.
  • 1

Java / Android Development

106,948 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator