sr24 25 Share Posted January 13, 2019 So i'm trying to understand why I had to use an int at the very begging of of my for loop. start was already declared an int. As I was typing this, I remembered hearing once that int in the for loop is solely contained to just that block and can't pull/distribute to the rest of the code. Is that correct? public static int sumOdd (int start, int end){ int sum = 0; if ((start > 0) && (end >= start)) { for (int i = start; start <= end; start++) { if (isOdd(start)) { sum += start; System.out.println("Number " + start + " is odd"); } } System.out.println("Sum of odd numbers is " + sum); } else { System.out.println("Invalid entry -1"); return -1; } return sum; } this is the rest of the code if anyone wants to see it and give any input. it's all random and meaningless just practice. public class Main { public static void main(String[] args) { // System.out.println("10,000 at 2% Interest = " + calculateInterest(10000.0, 2.0)); // System.out.println("10,000 at 2% Interest = " + calculateInterest(10000.0, 3.0)); // System.out.println("10,000 at 2% Interest = " + calculateInterest(10000.0, 4.0)); // System.out.println("10,000 at 2% Interest = " + calculateInterest(10000.0, 5.0)); sumOdd(1, 10); for (int i = 0; i < 5; i++) { System.out.println("Loop " + i + " ran this many times"); } for (int i=2; i<9; i++){ System.out.println("10,000 at " + i + "% interest is " + String.format("%.2f", calculateInterest(10000.0, i))); } for (int i=8; i>1; i--){ System.out.println("10,000 at " + i + "% interest is " + String.format("%.2f", calculateInterest(10000.0, i))); // System.out.println("results " + i + " number of loops"); } int numbers = 0; int sum = 0; for (int i=1; i<=1000; i++){ if ((i % 3 == 0) && (i % 5 == 0)){ numbers ++; sum += i; System.out.println("Found number = " + i); } if (numbers == 5){ break; } } System.out.println("Sum = " + sum); System.out.println(isOdd(7)); int count = 0; for(int i=10; i<50; i++) { if (isPrime(i)) { count++; System.out.println("Number " + i + " is prime"); if (count == 3) { System.out.println("Exiting for loop"); break; } } } } // for (int i=1; i<=1000; i++){ // if ((i % 3 == 0) && (i % 5 == 0)){ // numbers ++; // sum += i; // System.out.println("Found number = " + i); // } // if (numbers == 5){ // break; // } // } // System.out.println("Sum = " + sum); // System.out.println(isOdd(7)); public static double sumOdd (int start, int end){ int sum = 0; for (int i=start; start <= 10; start ++){ if (isOdd(start)){ sum += start; System.out.println("Number " + start + " is odd"); } } System.out.println("Sum of odd numbers is " + sum); return sum; } public static boolean isOdd (int number) { if ((number > 0) && (number % 2 != 0)) { return true; } return false; } public static boolean isPrime(int n){ if(n ==1){ return false; } for (int i=2; i<= Math.sqrt(n); i++){ if (n % i == 0){ return false; } } return true; } public static double calculateInterest(double amount, double interestRate){ return(amount* (interestRate/100)); } } Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 13, 2019 I can't edit my previous code but this is what i mean't to put. I simplified the boolean isOdd. Also changed the method sumOdd from a double to an int and returned -1. public static int sumOdd (int start, int end){ int sum = 0; if ((start > 0) && (end >= start)) { for (int i = start; start <= end; start++) { if (isOdd(start)) { sum += start; System.out.println("Number " + start + " is odd"); } } System.out.println("Sum of odd numbers is " + sum); } else { System.out.println("Invalid entry -1"); return -1; } return sum; } public static boolean isOdd (int number) { return(number > 0 && number % 2 != 0); } Link to comment Share on other sites More sharing options...
aopsdjiqw 1 Share Posted January 14, 2019 How can it store any value on 'i' if it doesn't know what 'i' is? You have to declare the 'Int i' before, otherwise it will declare only for the loop, and so you need to declare every loop you make. Instead of this: public static int sumOdd (int start, int end){ int sum = 0; if ((start > 0) && (end >= start)) { for (int i = start; start <= end; start++) { Try this: public static int sumOdd (int start, int end){ int sum = 0; int i; if ((start > 0) && (end >= start)) { for (i = start; start <= end; start++) { Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 14, 2019 14 minutes ago, joaomolin said: How can it store any value on 'i' if it doesn't know what 'i' is? You have to declare the 'Int i' before, otherwise it will declare only for the loop, and so you need to declare every loop you make. Instead of this: public static int sumOdd (int start, int end){ int sum = 0; if ((start > 0) && (end >= start)) { for (int i = start; start <= end; start++) { Try this: public static int sumOdd (int start, int end){ int sum = 0; int i; if ((start > 0) && (end >= start)) { for (i = start; start <= end; start++) { Yeah that's just another way of rephrasing where i'm declaring int i. I was wondering why I had to declare it in the first place instead of just being able to do for(start; start <= end; start++). But I think I understand it clearly now. Thank you for the help Link to comment Share on other sites More sharing options...
Notorious 342 Share Posted January 21, 2019 On 1/13/2019 at 7:11 PM, sr24 said: Yeah that's just another way of rephrasing where i'm declaring int i. I was wondering why I had to declare it in the first place instead of just being able to do for(start; start <= end; start++). But I think I understand it clearly now. Thank you for the help Without getting too complicated, by declaring a variable with a field type, you instantiate the of type data the variable can hold. As per your example, let's assume i is a String. This would cause a syntax error, as the for loop is expecting a Number type. So by declaring i as a int, you have told the compiler that the value being held inside of the i variable is of integer type. Otherwise, if you don't declare what type it is, the compiler has no way of knowing what type of data that variable is allowed to hold. Hope this clarifies things a bit; also if I didn't explain it well enough, or if you have more questions feel free to ask. Link to comment Share on other sites More sharing options...
sr24 25 Author Share Posted January 21, 2019 1 hour ago, Notorious said: Without getting too complicated, by declaring a variable with a field type, you instantiate the of type data the variable can hold. As per your example, let's assume i is a String. This would cause a syntax error, as the for loop is expecting a Number type. So by declaring i as a int, you have told the compiler that the value being held inside of the i variable is of integer type. Otherwise, if you don't declare what type it is, the compiler has no way of knowing what type of data that variable is allowed to hold. Hope this clarifies things a bit; also if I didn't explain it well enough, or if you have more questions feel free to ask. That did help, I was just not understanding why i had to declare it in the first place instead of using one outside of the loop. I am now working on multiple classes and using super and overrides. Constructors, getters, setters. I'm sure i'm going to have more questions lol Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.