+ 1
Something is wrong with my loop
I tried making a code where the user is able to choose which numbers should be included in the loop but for some reason that code doesnt understand what I'm trying to do. https://code.sololearn.com/c126hCzRL0sB/?ref=app
29 odpowiedzi
+ 5
It is working you have to input 3 times with a line space like this:
loop 1
20
40
+ 4
line 14 delete the ; at the end
+ 3
I get output with:
loop 1
5 20
+ 2
I suspect the same thing.
+ 1
@Jalen - It's working for me as well. What are you entering into the input dialog box when you run the code?
You must enter 3 separate values, each on a separate line:
------------
loop 1
5
20
------------
+ 1
Actual only 'loop 1' must be on a line by itself because nextLine reads the entire line. nextInt stops when it hits whitespace and skips whitespace to find the start of a number so the two numbers can be on the same line or even:
5
20
+ 1
@John... Good point. But, for some reason, when I attempted with my two integers on the second input line, I got "No Output" message. Not sure what was going on.
+ 1
did u input them like this?
loop
1
20
+ 1
No... I originally entered this:
------------
loop 1
5 20
------------
+ 1
Most likely it had an extra space on first line to prevent match. I got 2 between it once I'm guessing, since it failed once for me. After which, I double checked and caught it once before hitting submit. Huge fingers, tiny keys and if you hover near the screen it is the same as touching. I get double key presses occasionally, when I think too much and type too slow.
+ 1
If you want them forced to be on the same line, you are not going to like the programming. If you don't care about spacing and you wish to force two words, use two next calls each getting a word. If the second word is optional, call hasNextInt after the first word. If it is true, there is a number next so no second word. If it false, call next to get the second word.
0
I fixed line 14 but it says theres no output
0
I'm entering this:
loop 1
1
20
0
You have 'loop 1 ' which fails your case test.
0
In the future, add debugging. If you added,
default:
cout<<"'"<<type<<"'"<<endl; // two " around a '
break;
You would have seen your issue immediately.
0
import java.util.Scanner;
import java.util.Random;
public class Program
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String type = input.next();
switch(type) {
case "loop":
int a = input.nextInt();
int b = input.nextInt();
for(int x=a; x<=b; x++)
System.out.println(x);
}
}
}
try this input should in single line o changed "loop 1" to "loop" the line break is issue.
eg input:loop 1 20
0
thanks I fixed the problem by changing nextLine() to next() and got it to work.
0
if we solved the previous problem is there a way to write multiple words for the first line with multiple inputs?
for example:
first loop
1
20
0
so if I wanted the code to do what I said previously I need to add a second next(); ?
0
Yes, as long as all commands require two words. I'm assuming you plan on making a second command. Note:
first
loop
first loop
first
loop
are all the same using next because whitespace (spaces, tabs, and new lines) are skipped.