当我执行下面的代码时,它会正确地为inputCar1
方法输入输入,但在inputCar2
方法中它会执行除第一个扫描程序命令之外的所有操作。如果我使用这个命令两次然后它就像我期望的那样工作,但它不应该。为了更好地理解我的问题,我附上了我的代码和输出。
PrintClass:
package TestPackage;
/**
* Created by km.muzahid on 03-May-15.
*/
public class PrintClass {
public static void print(Object value){
System.out.print(value);
}
public static void println(Object value)
{
System.out.println(value);
}
}
CarClass:
package TestPackage;
/**
* Created by km.muzahid on 03-May-15.
*/
public class CarClass {
public String manufacturerName;
public int CarCC;
public float CarWeight;
public float FuealAmount;
public CarClass()//constructor
{
CarCC=0;
CarWeight=0;
FuealAmount=0;
}
public CarClass(String CarNumber,String Name,int cc,float fuel,float weight)//constructor
{
PrintClass ShowMassage=new PrintClass();
ShowMassage.println(CarNumber +"Car Manufacturer Name : "+Name+"\nCC of Car : "
+cc+"\nAmount of fuel of car : "+fuel+"\nWeight of car : "+weight);
}
}
问题代码:
package TestPackage;
import java.util.Scanner;
/**
* Created by km.muzahid on 03-May-15.
*/
public class testclass {
static private PrintClass ShowMassage;
public static Scanner MyScanner=new Scanner(System.in);
public static CarClass car=new CarClass();
public static CarClass car2nd=new CarClass();//create new object from carclass for 2nd car
public static void main(String[] args)
{
//1st car
inputCar1("Enter for first car: ");
CarClass SeeCar1=new CarClass("1st car details: \n",car.manufacturerName,car.CarCC,car.FuealAmount,car.CarWeight);
// //2nd car
CarClass See=new CarClass("2nd car details: \n",car2nd.manufacturerName,car2nd.CarCC,car2nd.FuealAmount,car2nd.CarWeight);
}
protected static void inputCar1(String Massege)
{
ShowMassage.println(Massege);
ShowMassage.println("Enter Name of car manufactueing : ");
car.manufacturerName=MyScanner.nextLine();
ShowMassage.println("Enter weight of car :");
car.CarWeight=MyScanner.nextFloat();
ShowMassage.println("Enter the amount of Fuel : ");
car.FuealAmount= MyScanner.nextFloat();
ShowMassage.println("Enter the car CC : ");
car.CarCC= MyScanner.nextInt();
inputCar2("Enter For 2nd Car : ");
}
protected static void inputCar2(String Massege)
{
ShowMassage.println(Massege);
ShowMassage.println("Enter Name of car manufactueing : ");
//car2nd.manufacturerName=MyScanner.nextLine();
car2nd.manufacturerName=MyScanner.nextLine();//problem indicated
ShowMassage.println("Enter weight of car 2nd :");
car2nd.CarWeight=MyScanner.nextFloat();
ShowMassage.println("Enter the amount of Fuel 2nd : ");
car2nd.FuealAmount= MyScanner.nextFloat();
ShowMassage.println("Enter the car 2nd CC : ");
car2nd.CarCC= MyScanner.nextInt();
CarClass SeeCar1=new CarClass("1st car details: \n",car.manufacturerName,car.CarCC,car.FuealAmount,car.CarWeight);
//2nd car
CarClass See=new CarClass("2nd car details: \n",car2nd.manufacturerName,car2nd.CarCC,car2nd.FuealAmount,car2nd.CarWeight);
}
}
输出:
Enter for first car:
Enter Name of car manufactueing :
lamborghini
Enter weight of car :
2222
Enter the amount of Fuel :
2.369
Enter the car CC :
236
Enter For 2nd Car :
Enter Name of car manufactueing :
Enter weight of car 2nd :
263
Enter the amount of Fuel 2nd :
3
Enter the car 2nd CC :
56
1st car details:
Car Manufacturer Name : lamborghini
CC of Car : 236
Amount of fuel of car : 2.369
Weight of car : 2222.0
2nd car details:
Car Manufacturer Name :
CC of Car : 56
Amount of fuel of car : 3.0
Weight of car : 263.0
1st car details:
Car Manufacturer Name : lamborghini
CC of Car : 236
Amount of fuel of car : 2.369
Weight of car : 2222.0
2nd car details:
Car Manufacturer Name :
CC of Car : 56
Amount of fuel of car : 3.0
Weight of car : 263.0
答案 0 :(得分:0)
问题在于,当您按ENTER键时,您会向输入缓冲区发送一个新行('\n'
),当您执行car.CarCC= MyScanner.nextInt();
时,它将使用数字而不是新行。所以稍后,当您执行car2nd.manufacturerName=MyScanner.nextLine();
时,'\n'
仍在输入缓冲区中,nextLine()
将使用'\n'
并立即返回空字符串。
解决方法是在这两行之间放置一个MyScanner.nextLine();
以消耗丢失的新行。