Java 如何让代码比较数组中的两个值?
我有以下代码:Java 如何让代码比较数组中的两个值?,java,Java,我有以下代码: Scanner sc=new Scanner(System.in); System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): "); String input=sc.nextLine(); for(int i=0;i<input.length(); i++) { String[] b=input.split(" "); if(b
Scanner sc=new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input=sc.nextLine();
for(int i=0;i<input.length(); i++) {
String[] b=input.split(" ");
if(b[i] < b[i+1]) { // Condition of "int input"
System.out.println("Acsending");
}
else {
System.out.println("Mixed");
}
}
Scanner sc=新扫描仪(System.in);
System.out.println(“在您的订单中键入(例如5 7 4 6 8 3 9 2 0 1-需要空格):”;
字符串输入=sc.nextLine();
对于(int i=0;i,应首先将其转换为整数:
String sentence = "1234";
int number = Integer.parseInt(sentence);
应首先将其转换为整数:
String sentence = "1234";
int number = Integer.parseInt(sentence);
比较两个字符串时,需要先将字符串转换为整数,然后再进行比较
if(Integer.valueOf(b[i]) < Integer.valueOf(b[i+1]))
比较两个字符串时,需要先将字符串转换为整数,然后再进行比较
if(Integer.valueOf(b[i]) < Integer.valueOf(b[i+1]))
正如编译器所说,您无法比较那样的字符串。请使用Integer.parseInt()将字符串转换为整数,然后进行比较。正如编译器所说,您无法比较那样的字符串。请使用Integer.parseInt()将字符串转换为整数,然后进行比较。按如下操作:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
// Instead of Integer::parseInt, you can use Integer::valueOf
// Check https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html for more details
if (Integer.parseInt(b[i]) > Integer.parseInt(b[i + 1])) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
if (b[i].compareTo(b[i + 1]) > 0) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
样本运行-2:
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
或者,您可以按如下方式执行:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
// Instead of Integer::parseInt, you can use Integer::valueOf
// Check https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html for more details
if (Integer.parseInt(b[i]) > Integer.parseInt(b[i + 1])) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
if (b[i].compareTo(b[i + 1]) > 0) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
样本运行-2:
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
按如下方式操作:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
// Instead of Integer::parseInt, you can use Integer::valueOf
// Check https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html for more details
if (Integer.parseInt(b[i]) > Integer.parseInt(b[i + 1])) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
if (b[i].compareTo(b[i + 1]) > 0) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
样本运行-2:
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
或者,您可以按如下方式执行:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
// Instead of Integer::parseInt, you can use Integer::valueOf
// Check https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html for more details
if (Integer.parseInt(b[i]) > Integer.parseInt(b[i + 1])) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
String input = sc.nextLine();
String[] b = input.split(" ");
int i;
for (i = 0; i < b.length - 1; i++) {
if (b[i].compareTo(b[i + 1]) > 0) {
System.out.println("Mixed");
break;
}
}
if (i == b.length - 1) {
System.out.println("Ascending");
}
}
}
样本运行-2:
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
5 7 4 6 8 3 9 2 0 1
Mixed
Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED):
1 2 3 4 5
Ascending
通常使用索引来访问i点处的数组
然后,您只需转换这两个变量,使其具有可比较性,然后就可以进行比较了通常您使用索引来访问i点处的数组
然后,您只需转换这两个变量,使其具有可比较性,就可以了b
是一个字符串数组,String[]
。它不包含数字。它包含文本。您不能将文本与进行比较。对于此特定问题,您可以使用String::compareTo
或Integer::parseInt
或Integer::valueOf
。有关详细信息,请查看我的答案。b
是字符串数组,String[]
。它不包含数字。它包含文本。您不能将文本与进行比较。对于此特定问题,您可以使用String::compareTo
或Integer::parseInt
或Integer::valueOf
。查看我的答案以了解详细信息。注意Integer::valueOf
内部调用Integer::parseInt
>因此,如果你只需要int
而不是Integer
,你应该使用Integer::parseInt
。请阅读更多关于它的信息,注意Integer::valueOf
内部调用Integer::parseInt
。因此,如果你只需要int
而不是Integer
,你应该使用Integer::pa>rseInt
。请访问网站了解更多信息