java连续输入_java – 要求用户进行多次输入
我正在嘗試編寫一個程序,它會一直詢問用戶一個整數(shù),直到他們輸入一個非整數(shù)的值,此時程序停止.
這是我到目前為止:
import java.util.Scanner;
import java.util.ArrayList;
public class InputStats {
private static Scanner a;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList InputArray = new ArrayList();
Boolean Running = true;
System.out.println("Please type an integer. To stop enter a non integer value");
while (Running) {
a = Scanner.nextLine();
if (!a.hasNextInt()) {
Running = false;
}
else {
InputArray.add(input.nextLine());
}
System.out.println(InputArray);
}
}
}
但是,使用此代碼我收到錯誤:
error: non-static method nextLine() cannot be referenced from a static context (for the line a = Scanner.nextLine();)
和
error: incompatible types (for the line a = Scanner.nextLine();)
可能是什么問題?
總結(jié)
以上是生活随笔為你收集整理的java连续输入_java – 要求用户进行多次输入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 反查域名_C段查询雏形之在Ja
- 下一篇: java frame button_ja