在用户输入0 JAVA之前,需要获取对数组的输入
我需要帮助了解如何编写一个for循环,该循环接受一定数量的整数(必须是1到10),一旦输入0,它就停止接受数字(0将是最后一个数字)。到目前为止,我的代码是:在用户输入0 JAVA之前,需要获取对数组的输入,java,arrays,user-input,Java,Arrays,User Input,我需要帮助了解如何编写一个for循环,该循环接受一定数量的整数(必须是1到10),一旦输入0,它就停止接受数字(0将是最后一个数字)。到目前为止,我的代码是: import java.util.Scanner; public class countNum { public static void main(String[] args) { int[] array; Scanner input = new Scanner(System.
import java.util.Scanner;
public class countNum {
public static void main(String[] args) {
int[] array;
Scanner input = new Scanner(System.in);
System.out.println ("Enter in numbers (1-10) enter 0 when finished:");
int x = input.nextInt();
while (x != 0) {
if (x > 2 && x < 10) {
//Don't know what to put here to make array[] take in the values
}
else
//Can I just put break? How do I get it to go back to the top of the while loop?
}
}
}
import java.util.Scanner;
公共类countNum{
公共静态void main(字符串[]args){
int[]数组;
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入数字(1-10)完成时输入0:”;
int x=input.nextInt();
而(x!=0){
如果(x>2&&x<10){
//不知道在这里放置什么使数组[]接受值
}
其他的
//我能不能放一个中断?我如何让它回到while循环的顶端?
}
}
}
}
我不明白如何在让扫描器读取该未知长度的一定数量的数字时,同时初始化具有设置长度的数组,直到输入0,然后循环停止接收该数组的输入
谢谢你的帮助 好的,这里有更多的细节:-
- 如果想要动态增加数组,则需要使用
。你是这样做的:-ArrayList
List<Integer> numbers = new ArrayList<Integer>();
- 此外,你可以自己行动。只需检查数字是否为
。如果它不是0
,则将其添加到0
:-ArrayList
numbers.add(num);
- 如果其
,则中断while循环0
- 你不需要这个
while循环中的条件,因为您已经在循环中检查它x!=0
不过,我建议您坚持使用第一种解决方案,因为它更易于实现。我强烈建议您着手使用Java集合 您可以根据需要修复程序
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class arrayQuestion {
public static void main(String[] args) {
List<Integer> userInputArray = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
userInputArray.add(x);
} else {
System.out
.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);
System.out.println("Numbers from USER : " + userInputArray);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}
// Perform Anything you want to do from this Array List
}
}
import java.util.ArrayList;
导入java.util.InputMismatchException;
导入java.util.List;
导入java.util.Scanner;
公开课问题{
公共静态void main(字符串[]args){
List userInputArray=new ArrayList();
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入10个数字”);
整数计数=0;
int x;
试一试{
做{
x=input.nextInt();
如果(x!=0){
System.out.println(“给定数字为”+x);
userInputArray.add(x);
}否则{
系统输出
.println(“程序将停止从用户获取输入”);
打破
}
计数++;
}而(x!=0&&count<10);
System.out.println(“来自用户的数字:“+userInputArray”);
}捕获(输入不匹配异常iex){
System.out.println(“很抱歉,您输入了无效的输入”);
}捕获(例外e){
System.out.println(“出了问题:-(”);
}
//从该数组列表中执行任何要执行的操作
}
}
我希望这能解决你的疑问。。
除此之外,如果用户输入任何字符或上述无效输入,您需要处理异常您可能需要使用ArrayList,它不需要您设置大小,而是动态地
使用设置的长度初始化数组
->您不需要。只需使用动态增加其大小的ArrayList
。我明白了在你的情况下,你使用了两个不同的变量来表示你的数字。你们能更详细一点吗?将int[]数组更改为List数组
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class arrayQuestion {
public static void main(String[] args) {
List<Integer> userInputArray = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
userInputArray.add(x);
} else {
System.out
.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);
System.out.println("Numbers from USER : " + userInputArray);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}
// Perform Anything you want to do from this Array List
}
}