2015-11-07 557 views
0

我創建了一個抽象類Employee,它顯示並計算每週和每小時Employee的詳細信息。在主要方法中,我使用了菜單的開關盒,並且只要用戶需要,就可以繼續使用該程序。但是,我「M編譯代碼:next()和next().CharAt(0)之間的區別;

*javac "AbstractDemo.java" (in directory: /home/user/Desktop) 
AbstractDemo.java:52: error: incompatible types 
    ch=s.next(); 
      ^
    required: char 
    found: String 
1 error 
Compilation failed.* 

這是源代碼時收到錯誤:

import java.util.*; 
abstract class Employee{ 
    Employee(float amt,int n){ 
     float sal; 
     System.out.println("The amount paid to Employee is:"+amt); 
     sal=amt*n; 
     System.out.println("Total Salary is:"+sal); 
    } 

} 
class WeeklyEmployee extends Employee{ 
     WeeklyEmployee(float a,int n){ 
     super(a,n); 
    } 
} 
class HourlyEmployee extends Employee{ 
     HourlyEmployee(float amt,int hrs){ 
     super(amt,hrs); 
    } 
}   

public class AbstractDemo { 

    public static void main (String args[]) { 
     int a; 
     char ch; 
     float amount; 
     int hours; 
     int weeks; 
     Scanner s=new Scanner(System.in); 
     do{ 

     System.out.println("Enter the choice"); 
     a=s.nextInt(); 
     switch (a) { 
      case 1 : 
     System.out.println("Enter the salary of weekly employee(Per Week):"); 
     amount=s.nextFloat(); 
     System.out.println("Enter the total no of week"); 
     weeks=s.nextInt(); 
      Employee W=new WeeklyEmployee(amount,weeks);break; 
      case 2: 

     System.out.println("Enter the salary of hourly employee(Per hour):"); 
     amount=s.nextFloat(); 
     System.out.println("Enter the total no of hours"); 
     hours=s.nextInt(); 
     Employee H=new HourlyEmployee(amount,hours);break; 
      default:System.out.println("Error invalid Choice"); 
    } 
    System.out.println("Do you wish to continue?(Y/N)"); 
    ch=s.next(); 
}while (ch== 'y'||ch== 'Y'); 

} 
} 

但是當我使用s.next().ChatAt(0);代碼編譯successfully.Could有人解釋爲什麼發生這種情況是將字符串作爲字符串輸入?或者如果它是一個字符串,爲什麼它顯示一個錯誤whe我編輯的條件爲while(ch=="y"||ch=="Y");

+3

編譯器跟你說話,男人!聽他的。他給你你想要的答案。 – Nagh

回答

3

chchars.next()返回String。您無法將String指定給char。您可以將String的第一個字符指定爲char,這就是您在ch = s.next().charAt(0);中所做的操作。

這是你沒有嘗試while(ch=="y"||ch=="Y"),因爲這即使你改變ch是失敗一件好事String(這將使其能夠通過編譯,但不會產生預期的結果),因爲你可以」將Java中的String==進行比較(您應該使用equals代替)。

+0

謝謝你的回答。 – PSN

0

s.next()返回下一個String對象。但是s.next().charAt(0)返回下一個String對象的第一個字符。因此其預期字符和投擲錯誤

1

Scanner對象的方法next()返回String。因此,爲了讓您的分配工作,您需要使用charAt()方法從該字符串中提取第一個字符。

而且由於ch是字符而不是字符串,所以while(ch == "y" || ch == "Y")將不起作用。

改爲使用while(ch == 'y' || ch == 'Y')

(單引號字符=,雙引號=字符串)

0

當您使用s.next(),它被理解爲一個字符串。這就是爲什麼你得到一個錯誤與ch = s.next()

但是,s.next()。charAt(0)將返回一個可以由您的變量使用的char值。

同樣,當您將char ch與'y'進行比較時,它會比較兩個字符,但是「y」會讀作字符串並且會導致類型不匹配。

0

這是因爲CH焦炭和你的想法是輸入單個字母Y或N停止或繼續你的程序。但是,您使用返回String的Scanner的next()方法。這是最主要的原因,當您嘗試使用s.next()得到這個

*javac "AbstractDemo.java" (in directory: /home/user/Desktop) AbstractDemo.java:52: error: incompatible types ch=s.next(); ^ required: char found: String 1 error Compilation failed.*

。ChatAt(0)。這意味着你試圖從輸入中獲得第一個字符。
- >當您嘗試將字符串分配給字符而不轉換它時,這只是不兼容的類型錯誤。

相關問題