Need help with this question: Write Java code which reads numbers from the keyboard
Write Java code which reads numbers from the keyboard until zero is entered. Only the positive numbers entered are to be added to a variable-sized collection.
This is what I have so far:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Demo1App extends Object
{
public static void main(String[] argStrings) throws Exception
{
ArrayList myArrayList = new ArrayList();
Scanner input = new Scanner(System.in);
System.out.println ("NUMBERS:");
while (int input > 0)
{
myArrayList.add(input);
}
while (int input < 0)
{
System.out.println ("ENTER NUMBERS GREATER 开发者_运维百科THAN 0!");
}
}
}
This code doesn't work, I'm not sure why but any help with it would be appreciated.
You have to explicitly call the scanner to get input like: scanner.getInt() and it will give you the next integer. Then you can try to see if the integer is positive or not
You are never even trying to read the input from the keyboard.
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Demo1App extends Object
{
public static void main(String[] argStrings) throws Exception
{
ArrayList myArrayList = new ArrayList();
Scanner input = new Scanner(System.in);
System.out.println ("NUMBERS:");
int numberFromUser = input.nextInt();
while (numberFromUser > 0)
{
myArrayList.add(numberFromUser);
System.out.println("Enter Next Number");
numberFromUser = input.nextInt();
}
}
}
while (int input > 0)
This is the error. What you're doing here is you're declaring a new variable and compare it to 0. This is wrong for several reasons:
- There already is a variable named input and you're creating another one with the same name.
- You can't use operators on a variable in the same line as you're declaring it.
- You never give a value to the variable
- You can't declare a variable inside a while-condition.
What I think you're trying to do there is to cast the input to object to type int. You can't do this because a) you can't cast objects to primitives and b) it isn't clear what the integer equivalent of a Scanner object would be.
What you should do instead is create a new variable of type int and use the method nextInt() of the Scanner class to give it a value.
Another problem with your code is that you have two while-loops, one for i>0
and one for i<0
. However this does not quite model the desired behavior. Consider the case where the user enters a positive number, then a negative one and then another positive one. Once the user enters a negative number, the loop for positive numbers will never be reached again.
What you need is one while loop that just checks the exit condition (i.e. that runs until the entered number is 0) and inside you should check whether the number is negative or positive using if
.
Try something like:
int i = input.nextInt();
while(i != 0) {
// do stuff with i
i = input.nextInt();
}
Your redclaring input each time. Also, read the Scanner javadoc to see what method you need to read in the number.
Your nearly there - it'll be worth spending a little more time on it
Make sure that you check that input.hasNextInt()
is true
before reading next number or you'll get an exception if there is no number in input:
while(input.hasNextInt()) {
i = input.nextInt(); //now we know that there is a number, so read it
if (i == 0) break; //stop if i is 0
// do stuff with i
}
And use ArrayList<Integer>
instead of just ArrayList
.
精彩评论