比较字符串和整数 (java)
Comparing a string and integer (java)
我在一些 Java 编程方面遇到了问题,我对输入之间的比较操作数如何工作感到很困惑。
我想要的是让我的程序接受输入、分析输入、检查它等于什么,并根据他们的输入显示一条消息。我如何将输入与我设置的 option1
变量进行比较?
这是一个文件,名为 loadfile.java
package subsets;
import java.io.*;
import java.util.Scanner;
public class loadfile {
public static void main(String args[]) {
}
public static void choose(String[] args) {
// Set an option
int option1 = 1;
// Start grabbing input
System.out.print("Choose one: ");
Scanner scanner = new Scanner( System.in );
String input = scanner.nextLine();
// Parse input into an integer (expecting int)
int selection = Integer.parseInt(input);
// If input == option1, let's go with this code.
if(input.equals(option1)){
System.out.println("You choose option 1.");
}
}
}
我在另一个文件中有 loadfile.choose(args);
,该文件处理我将要实现的其余文件。
文件如下:otherfile.java
package subsets;
public class otherfile {
public static void main(String args[]){
System.out.println("Please select an option (using number keys): \n");
System.out.println(
"1. Option 1 \n"
+ "2. Option 2 \n"
+ "3. Option 3 \n"
+ "4. option 4 \n");
// Start analyzing their choice
loadfile.choose(args);
}
}
您可以将 int option1 = 1;
更改为 String option1 = "1";
或将用户输入解析为整数,或者执行 scanner.nextInt()
而不是 nextLine
有很多选项可以解决此问题这里。 Integer.parseInt(String)
.
equals()
只在相同类型的对象之间有效。
int
甚至不是对象,但是 Java 在将其作为参数传递给需要 [=13= 的方法时将其隐式转换为 Integer
对象](Integer
是 Object
- 所有 类 扩展 Object
)。
但现在 equals
只剩下将 String
对象与 Integer
对象进行比较。由于它们不是同一类型,因此这将始终为 false。
要正确进行比较,您必须通过解析 将字符串int
从字符串中获取一个整数。例如,使用 Integer.parseInt()
.
并且...您确实做到了,但是您忘记了使用您的操作结果。您有 selection
变量,只需比较 selection
而不是 input
.
当然,在这种情况下,您只需使用 selection == option1
而不是使用 equals
,因为 int
是原始的,没有方法。
一般来说,equals
方法在比较不同类型的对象时 not return 为真。这是 String#equals(Object)
:
的实现
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
注意检查 if (anObject instanceof String)
。
对于你的情况,我不明白为什么不简单地做:
if (selection == option1) {
因为您已经获得了 selection
变量。
我在一些 Java 编程方面遇到了问题,我对输入之间的比较操作数如何工作感到很困惑。
我想要的是让我的程序接受输入、分析输入、检查它等于什么,并根据他们的输入显示一条消息。我如何将输入与我设置的 option1
变量进行比较?
这是一个文件,名为 loadfile.java
package subsets;
import java.io.*;
import java.util.Scanner;
public class loadfile {
public static void main(String args[]) {
}
public static void choose(String[] args) {
// Set an option
int option1 = 1;
// Start grabbing input
System.out.print("Choose one: ");
Scanner scanner = new Scanner( System.in );
String input = scanner.nextLine();
// Parse input into an integer (expecting int)
int selection = Integer.parseInt(input);
// If input == option1, let's go with this code.
if(input.equals(option1)){
System.out.println("You choose option 1.");
}
}
}
我在另一个文件中有 loadfile.choose(args);
,该文件处理我将要实现的其余文件。
文件如下:otherfile.java
package subsets;
public class otherfile {
public static void main(String args[]){
System.out.println("Please select an option (using number keys): \n");
System.out.println(
"1. Option 1 \n"
+ "2. Option 2 \n"
+ "3. Option 3 \n"
+ "4. option 4 \n");
// Start analyzing their choice
loadfile.choose(args);
}
}
您可以将 int option1 = 1;
更改为 String option1 = "1";
或将用户输入解析为整数,或者执行 scanner.nextInt()
而不是 nextLine
有很多选项可以解决此问题这里。 Integer.parseInt(String)
.
equals()
只在相同类型的对象之间有效。
int
甚至不是对象,但是 Java 在将其作为参数传递给需要 [=13= 的方法时将其隐式转换为 Integer
对象](Integer
是 Object
- 所有 类 扩展 Object
)。
但现在 equals
只剩下将 String
对象与 Integer
对象进行比较。由于它们不是同一类型,因此这将始终为 false。
要正确进行比较,您必须通过解析 将字符串int
从字符串中获取一个整数。例如,使用 Integer.parseInt()
.
并且...您确实做到了,但是您忘记了使用您的操作结果。您有 selection
变量,只需比较 selection
而不是 input
.
当然,在这种情况下,您只需使用 selection == option1
而不是使用 equals
,因为 int
是原始的,没有方法。
一般来说,equals
方法在比较不同类型的对象时 not return 为真。这是 String#equals(Object)
:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
注意检查 if (anObject instanceof String)
。
对于你的情况,我不明白为什么不简单地做:
if (selection == option1) {
因为您已经获得了 selection
变量。