The application should print the first n
odd numbers, where n
is between 1 and 25, starting at start
. start
is between 100 and 200 and it says where the odd number sequence should start. If start
is an even number then the sequence should start at the next odd number.
Here is and example with n = 4
and start = 193
:
193, 195, 197, 199
The application must allow the user to enter values for n
and start
. It must then verify that n
is between 1 and 25 and start
is between 100 and 200.
My code so far:
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
Scanner sc2= new Scanner(System.in);
int start = sc2.nextInt();
for (int i=start; i=n; i=start 1 =2)
if ((n>=1) && (n<=25)) {
System.out.println("Valid Input!");
}
else
System.out.println ("Invalid Input!!!");
if ((start>=100) && (start<=200)){
System.out.println ("Valid Input!");
}
else
System.out.println("Invalid Input!!!");
if (start%2==0) {
System.out.println ("Even Number Inputted! Next Odd Number displayed.");
}
}
It keeps telling me that int
cannot be converted to boolean
. I understand what it says just not sure how to rectify it.
CodePudding user response:
First, prompt for the values
int minN = 1;
int maxN = 25;
int minStart = 100;
int maxStart = 200;
int start = getInput("Please enter starting value: ", minStart, maxStart);
int n = getInput("Please enter number of odd numbers to print: ", minN, maxN);
Now compute and print the results for start = 190 and n = 4
for (int i = start | 1; i <= maxStart && n-- > 0; i = 2) {
System.out.println(i);
}
prints
191
193
195
197
Explanation
First, the user is prompted for input using a method.
start
- the starting point selected by the usern
- the number of values to generate also selected by the user
Then a loop is used to generate the values. This works by first, ensuring the start is odd
by setting the low order bit to a 1. If the value is already odd, this has no effect. The loop begins with the first odd from start and increments by 2
. The loop terminates if either i
exceeds maxStart
or n
reaches 0
.
The prompt method accepts a prompt message that is appropriate for the type of input. It also accepts a range to provide a helpful reprompt if the choice is out of range. As long as the user enters out of range values, the method will keep prompting. Otherwise, it returns the accepted value.
static Scanner input = new Scanner(System.in);
public static int getInput(String prompt, int min, int max) {
int value;
while (true) {
System.out.print(prompt);
if ((value = input.nextInt()) >= min && value <= max) {
break;
}
System.out.printf(
"Invalid entry, must be between %d and %d inclusive", min, max);
}
return value;
}
CodePudding user response:
You only need one Scanner
. You should perform input validation before your display loop. And your for
loop syntax is incorrect, the second part should resolve to a boolean
test (not an assignment) and i=start 1 =2
is just wrong. Fixing it might look something like
Scanner sc = new Scanner(System.in);
int n;
do {
System.out.println("Print how many odd numbers (n)? Enter a value between 1 and 25.");
n = sc.nextInt();
} while (n < 1 || n > 25);
int start;
do {
System.out.println("Enter starting value? Enter a value between 100 and 200.");
start = sc.nextInt();
} while (start < 100 || start > 200);
if (start % 2 != 1) {
System.out.println("Even Number Inputted! Next Odd Number displayed.");
start ;
}
for (int i = start; i < start (2 * n); i = 2) {
System.out.println(i);
}
CodePudding user response:
The problem is in the for loop condition, you must change i=n into i<=n
CodePudding user response:
This is my first time posting so bare with me.
I believe that the reason you are getting int cannot be converted to boolean
is because in your for loop, the second statement says i=n
. This statement of the for loop needs to be a logic block.
I am a firm believer in giving resources rather than the answer, so check out this write up done by w3schools (killer resource btw) on for loops in java: https://www.w3schools.com/java/java_for_loop.asp
CodePudding user response:
I believe problem is for loop at the condition part, i cannot be equal n like i=n. You can change it i<=n