Java 更改程序提示用户输入的方式
该程序提示用户输入列表中的数字,然后提示用户输入列表中的实际数字。然后决定列表中的数字是否已排序。它工作完全正常,但我希望程序能够立即提示用户列表中的数字数量,而不是提示用户输入数字,然后再提示用户,但提示次数与用户以前输入的数字相同。抱歉,太长了,下面是一个输出与我正在查看的内容的示例: 电流输出: 我想要的是: 代码:Java 更改程序提示用户输入的方式,java,list,sorting,Java,List,Sorting,该程序提示用户输入列表中的数字,然后提示用户输入列表中的实际数字。然后决定列表中的数字是否已排序。它工作完全正常,但我希望程序能够立即提示用户列表中的数字数量,而不是提示用户输入数字,然后再提示用户,但提示次数与用户以前输入的数字相同。抱歉,太长了,下面是一个输出与我正在查看的内容的示例: 电流输出: 我想要的是: 代码: 您可以使用StringTokenizer来标记输入。这将允许在一行中输入阵列号。这还根据用户提供的令牌数量确定数组的大小 public static void main(St
您可以使用StringTokenizer来标记输入。这将允许在一行中输入阵列号。这还根据用户提供的令牌数量确定数组的大小
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a numbers: ");
String input = scanner.nextLine();
StringTokenizer s = new StringTokenizer(input);
int[] number = new int[s.countTokens()];
int index = 0;
while(s.hasMoreTokens()){
String item = s.nextToken();
try{
number[index] = Integer.parseInt(item);
index++;
}catch(Exception e){
e.printStackTrace();
}
}
if (isSorted(number)) {
System.out.println("The list is sorted.");
} else {
System.out.println("The list is not sorted.");
}
scanner.close();
}
public static boolean isSorted(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (list[i] > list[i + 1]) {
return false;
}
}
return true;
}
编辑:您还可以拆分字符串并循环返回的数组元素
String[] splitInput = input.split(" ");
int[] number = new int[splitInput.length];
for(int i = 0 ; i < splitInput.length ; i++){
String item = splitInput[i];
try{
number[i] = Integer.parseInt(item);
}catch(Exception e){
e.printStackTrace();
i--;
}
}
我们还没有了解到这一点,但非常感谢您的帮助!我非常感谢您能借此机会学习这两种方法并扩展您的知识库祝你好运
import java.util.Scanner;
public class Question7_19 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for the length of the list: ");
int listLength = input.nextInt();
int[] number = new int[listLength];
for (int i = 0; i < number.length; i++) {
System.out.println("Enter list: ");
number[i] = input.nextInt();
}
if (isSorted(number)) {
System.out.println("The list is sorted.");
} else {
System.out.println("The list is not sorted.");
}
}
public static boolean isSorted(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (list[i] > list[i + 1]) {
return false;
}
}
return true;
}
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a numbers: ");
String input = scanner.nextLine();
StringTokenizer s = new StringTokenizer(input);
int[] number = new int[s.countTokens()];
int index = 0;
while(s.hasMoreTokens()){
String item = s.nextToken();
try{
number[index] = Integer.parseInt(item);
index++;
}catch(Exception e){
e.printStackTrace();
}
}
if (isSorted(number)) {
System.out.println("The list is sorted.");
} else {
System.out.println("The list is not sorted.");
}
scanner.close();
}
public static boolean isSorted(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (list[i] > list[i + 1]) {
return false;
}
}
return true;
}
String[] splitInput = input.split(" ");
int[] number = new int[splitInput.length];
for(int i = 0 ; i < splitInput.length ; i++){
String item = splitInput[i];
try{
number[i] = Integer.parseInt(item);
}catch(Exception e){
e.printStackTrace();
i--;
}
}