我不知道如何介绍我的年龄不是整数,而不让它显示在输出中。

huangapple go评论72阅读模式
英文:

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(&quot;What is your name? &quot;);
    userName = user_input.next();
    String userAge;
    System.out.print(&quot;How old are you? &quot;);
    userAge = user_input.next();
    System.out.println(&quot;-----------------------&quot;);
    
    System.out.print(&quot;Your name is: &quot; + userName);
    System.out.println();
    System.out.print(&quot;You are &quot; + userAge + &quot; years old.&quot;);
    System.out.println();
    System.out.println(&quot;You are older than me: &quot; + (myAge &lt; userAge));
    
    
  }  
}

答案1

得分: 1

你不能将 Stringint 进行比较,你需要从用户那里获取一个整数而不是字符串,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(&quot;What is your name? &quot;);
	    userName = user_input.next();
	    int userAge;
	    System.out.print(&quot;How old are you? &quot;);
	    userAge = user_input.nextInt();
	    System.out.println(&quot;-----------------------&quot;);
	    
	    System.out.print(&quot;Your name is: &quot; + userName);
	    System.out.println();
	    System.out.print(&quot;You are &quot; + userAge + &quot; years old.&quot;);
	    System.out.println();
	    System.out.println(&quot;You are older than me: &quot; + (myAge &lt; 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(&quot;What is your name? &quot;);
    userName = user_input.next();
    **int userAge;**
    System.out.print(&quot;How old are you? &quot;);
    **userAge = Integer.parseInt(user_input.next());**
    System.out.println(&quot;-----------------------&quot;);
    
    System.out.print(&quot;Your name is: &quot; + userName);
    System.out.println();
    System.out.print(&quot;You are &quot; + userAge + &quot; years old.&quot;);
    System.out.println();
    System.out.println(&quot;You are older than me: &quot; + (myAge &lt; userAge));
    
    
  }  
}

</details>



huangapple
  • 本文由 发表于 2020年10月23日 05:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定