2016-10-14 113 views
0

我是Java新手,這是關於我必須爲我的課所做的功課。我不知道這裏有什麼問題或如何解決。我很感激,如果有人能幫助我。錯誤:類型不匹配:無法從java.lang.String轉換爲int

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; 
    } 
} 

回答

0

該方法的返回類型是int,而您返回的是作爲字符串的「message」。這導致編譯器無法從字符串轉換爲int

0

你的方法返回INT,但返回的消息是字符串

公共靜態INT getLodging(INT住宿,詮釋時代)

字符串消息;

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 
} 
相關問題