Java 执行while循环和数组
有人能帮我做这个循环吗?我希望用户可以选择在程序结束时继续Y/N以重新运行或终止程序。最好是做一个while循环 代码(Java):Java 执行while循环和数组,java,loops,do-while,do-loops,Java,Loops,Do While,Do Loops,有人能帮我做这个循环吗?我希望用户可以选择在程序结束时继续Y/N以重新运行或终止程序。最好是做一个while循环 代码(Java): publicstaticvoidmain(字符串[]args){ int size=10;//数组的大小 Scanner scan=新扫描仪(System.in);//创建扫描仪 while(true) { System.out.println(“输入值:”); int[]myArray=new int[size];//声明数组 对于(int i=0;i
publicstaticvoidmain(字符串[]args){
int size=10;//数组的大小
Scanner scan=新扫描仪(System.in);//创建扫描仪
while(true)
{
System.out.println(“输入值:”);
int[]myArray=new int[size];//声明数组
对于(int i=0;i 对于(int i=0;i您可以执行以下操作:
public static void main(String[] args) {
int size = 10; //size of array
String nextInput;
Scanner scan = new Scanner(System.in); //create scanner
do {
System.out.println("Enter values: ");
int[] myArray = new int[size]; //declare array
for (int i = 0; i < size; i++) {
int userInput = scan.nextInt(); //receive user input
myArray[i] = userInput;
}
System.out.print("You entered these values: "); //print all values entered by user
for (int i = 0; i < myArray.length; i++) {
System.out.print(" " + myArray[i]);
}
System.out.println("");
IndexOfLargestBatista largeIndex = new IndexOfLargestBatista(); //object of IndexOfLargest class
System.out.println("Index of Largest value: " + largeIndex.findIndex(myArray)); //pass array as parameter and get index number in return
System.out.println("Do you want to continue?: Y/N ");
nextInput = scan.nextLine();
} while (nextInput.equalsIgnoreCase("Y"));
}
publicstaticvoidmain(字符串[]args){
int size=10;//数组的大小
字符串输入;
Scanner scan=新扫描仪(System.in);//创建扫描仪
做{
System.out.println(“输入值:”);
int[]myArray=new int[size];//声明数组
对于(int i=0;i
您可以使用while(true)
循环,一旦用户进入N
您就可以中断
循环,否则继续。非常感谢!!!
public static void main(String[] args) {
int size = 10; //size of array
String nextInput;
Scanner scan = new Scanner(System.in); //create scanner
do {
System.out.println("Enter values: ");
int[] myArray = new int[size]; //declare array
for (int i = 0; i < size; i++) {
int userInput = scan.nextInt(); //receive user input
myArray[i] = userInput;
}
System.out.print("You entered these values: "); //print all values entered by user
for (int i = 0; i < myArray.length; i++) {
System.out.print(" " + myArray[i]);
}
System.out.println("");
IndexOfLargestBatista largeIndex = new IndexOfLargestBatista(); //object of IndexOfLargest class
System.out.println("Index of Largest value: " + largeIndex.findIndex(myArray)); //pass array as parameter and get index number in return
System.out.println("Do you want to continue?: Y/N ");
nextInput = scan.nextLine();
} while (nextInput.equalsIgnoreCase("Y"));
}