错误:类型不匹配:无法从java.lang.String转换为int

时间:2016-10-14 01:10:03

标签: java string

我是Java的新手,这是关于我必须为我的班级做的功课。我不确定这里有什么问题或者如何解决它。如果有人可以帮助我,我感激不尽。

 import java.util.Scanner;

 public class TravelAgent
 {
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter the amount per night");
       int lodging = input.nextInt();
       System.out.print("Enter your age");
       int age = input.nextInt();
    }

    public static int getLodging(int lodging, int age)
    {
       String message;

       if(lodging <= 30)
          message="campaign"; 
       else if(lodging <=45  && lodging > 30)
       {
          if(age <= 30)
              message="youth hotel";
          else if(age>30)
              message="adult hotel";
       }
       else if(lodging <=100 && lodging >45)
          message =  "hotel";
       else if(lodging <=200 && lodging >100)
          message="Grand hotel";
       else
          message="Exclusive suite";
      return message;
   }
}

3 个答案:

答案 0 :(得分:0)

方法的返回类型是int,而您返回的是“message”,它是一个字符串。这导致编译器无法从String转换为int

答案 1 :(得分:0)

您的方法返回 int ,但您返回字符串

的消息

public static int getLodging(int accommodation,int age)

字符串 消息;

答案 2 :(得分:0)

您需要更改 getLodging 方法,如下所示

public static String getLodging(int lodging, int age)
{
    String message = null;

    if(lodging <= 30)
    .
    .
    .
    return message;
}

此外,您还必须在主要中调用此方法才能获得结果。

public static void main(String[] args)
{
    // Get inputs lodging and age
    String result = getLodging(lodging, age);
    System.out.println("You're eligible for : "+result); // Print the result
}
相关问题