英文:
I don't know how to introduce myAge as not an integer without it showing up in the output
问题
我遇到了二元运算符'<'的错误,错误类型是不良操作数类型。我不确定如何修复它。我对编程非常新手,所以我只真正了解基础知识。
import java.util.Scanner;
public class HowOldAreYou {
public static void main(String args[]) {
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
String userAge;
System.out.print("How old are you? ");
userAge = user_input.next();
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < Integer.parseInt(userAge)));
}
}
英文:
I have the error of bad operand types for binary operator '<' I'm not sure how to fix it. I'm very new to coding so I only really know the basics.
import java.util.Scanner;
public class HowOldAreYou
{
public static void main(String args[])
{
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
String userAge;
System.out.print("How old are you? ");
userAge = user_input.next();
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < userAge));
}
}
答案1
得分: 1
你不能将 String
与 int
进行比较,你需要从用户那里获取一个整数而不是字符串,int
用于表示不以 0 开头的数字,例如年龄 / 价格 / 年份:
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
int userAge;
System.out.print("How old are you? ");
userAge = user_input.nextInt();
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < userAge));
输出结果:
What is your name? Pluto
How old are you? 20
-----------------------
Your name is: Pluto
You are 20 years old.
You are older than me: true
英文:
You cant compare String
to int
, you need to get a int from the user and not a string , int is used for numbers that do not start with 0, for example age / price / year:
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
int userAge;
System.out.print("How old are you? ");
userAge = user_input.nextInt();
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < userAge));
Output :
What is your name? Pluto
How old are you? 20
-----------------------
Your name is: Pluto
You are 20 years old.
You are older than me: true
答案2
得分: 0
所以你的问题在于,你正在将一个整数与一个字符串进行比较,这不能直接比较。我所做的更改应该可以解决这个问题。
public class HowOldAreYou
{
public static void main(String args[])
{
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
int userAge;
System.out.print("How old are you? ");
userAge = Integer.parseInt(user_input.next());
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < userAge));
}
}
英文:
So your issue if you are comparing an integer with a string, which cannot compare directly. The change I made should resolve this
public class HowOldAreYou
{
public static void main(String args[])
{
int myAge = 16;
Scanner user_input = new Scanner(System.in);
String userName;
System.out.print("What is your name? ");
userName = user_input.next();
**int userAge;**
System.out.print("How old are you? ");
**userAge = Integer.parseInt(user_input.next());**
System.out.println("-----------------------");
System.out.print("Your name is: " + userName);
System.out.println();
System.out.print("You are " + userAge + " years old.");
System.out.println();
System.out.println("You are older than me: " + (myAge < userAge));
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论