我創建了一個抽象類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");
?
編譯器跟你說話,男人!聽他的。他給你你想要的答案。 – Nagh