Java switch语句将直接变为默认值
我从结果类中的java类继承了一个Java switch语句将直接变为默认值,java,Java,我从结果类中的java类继承了一个Result类。在构造函数中,我使用一个开关,但如果用户输入1或其他值,它将不执行,并直接进入默认值 有什么问题?任何人都可以向我解释这一点,也希望在这段代码中使用文件处理,但它怎么不明白呢 public class Vehicle { final public void choose() { System.out.println("Thanks!for choosing us"); } public void sele
Result
类。在构造函数中,我使用一个开关,但如果用户输入1或其他值,它将不执行,并直接进入默认值
有什么问题?任何人都可以向我解释这一点,也希望在这段代码中使用文件处理,但它怎么不明白呢
public class Vehicle {
final public void choose() {
System.out.println("Thanks!for choosing us");
}
public void select() {
System.out.println("select");
}
}
class Car extends Vehicle {
int Brand;
int price;
int noofpeople;
public void select() {
Scanner s = new Scanner(System.in);
System.out.println("hello we will help you to choose"
+ "best car for you but for this you have answer some questions.");
System.out.println("Which Brand you want to choose?");
System.out.println("press 1 for Toyota");
System.out.println("Press 2 for Honda");
System.out.println("Press 3 for BMW");
Brand = s.nextInt();
System.out.println();
System.out.println("Please enter your price less than or equal to 20lack");
price = s.nextInt();
System.out.println("please enter your family members");
noofpeople = s.nextInt();
}
}
class Toyota extends Car {
Toyota() {
System.out.println("You selected toyota");
if (price == 1000000 && noofpeople == 4) {
System.out.println("The Best Cars for you are:");
System.out.println("passo");
System.out.println("mooco");
} else if (price == 200000 && noofpeople == 5) {
System.out.println("Corolla Grande");
} else if (price <= 2000000 && noofpeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Corolla gli");
} else if (price > 2000000 && noofpeople > 5) {
System.out.println("No car for found");
}
}
}
class Honda extends Car {
Honda() {
System.out.println("You selected Honda");
if (price < 1000000 && noofpeople >= 4) {
System.out.println("The Best Cars for you are:");
System.out.println("honda fit");
System.out.println("honda pure");
} else if (price == 2000000 && noofpeople >= 5) {
System.out.println("honda vezel");
} else if ((price < 2000000 && price > 1000000) && noofpeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Honda civic");
} else if (price >= 200000 && noofpeople >= 5) {
System.out.println("No car for found");
} else if (noofpeople < 4) {
System.out.println("No car for found");
}
}
}
class BMW extends Car {
BMW() {
System.out.println("You selected BMW");
if (price < 1000000 && noofpeople >= 4) {
System.out.println("The Best Cars for you are:");
System.out.println("honda fit");
System.out.println("honda pure");
} else if (price == 2000000 && noofpeople >= 5) {
System.out.println("honda vezel");
} else if ((price < 2000000 && price > 1000000) && noofpeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Honda civic");
} else if (price >= 200000 && noofpeople >= 5) {
System.out.println("No car for found");
} else if (noofpeople < 4) {
System.out.println("No car for found");
}
}
}
class Result extends Car {
Result() {
Car c = new Car();
c.choose();
c.select();
switch (Brand) {
case 1:
Toyota a = new Toyota();
break;
case 2:
Honda h = new Honda();
break;
default:
BMW n = new BMW();
break;
}
}
}
公共级车辆{
最终公开作废选择(){
System.out.println(“感谢您选择我们”);
}
公共作废选择(){
System.out.println(“选择”);
}
}
二等车{
国际品牌;
国际价格;
int noofpeople;
公共作废选择(){
扫描仪s=新的扫描仪(System.in);
System.out.println(“您好,我们将帮助您选择”
+“最适合你的车,但为此你必须回答一些问题。”);
System.out.println(“您要选择哪个品牌?”);
System.out.println(“丰田按1”);
System.out.println(“本田冲床2”);
System.out.println(“宝马按3”);
品牌=s.nextInt();
System.out.println();
System.out.println(“请输入小于或等于20L的价格”);
价格=s.nextInt();
System.out.println(“请输入您的家庭成员”);
noofpeople=s.nextInt();
}
}
丰田级轿车{
丰田(){
System.out.println(“您选择了丰田”);
如果(价格=1000000&&noofpeople==4){
System.out.println(“最适合你的汽车是:”);
系统输出打印项次(“passo”);
系统输出打印号(“mooco”);
}否则如果(价格=200000&&noofpeople==5){
System.out.println(“Corolla Grande”);
}如果有其他情况(价格2000000且无超过5人){
System.out.println(“找不到车辆”);
}
}
}
本田级轿车{
本田(){
System.out.println(“您选择了本田”);
如果(价格<1000000&&noofpeople>=4){
System.out.println(“最适合你的汽车是:”);
System.out.println(“本田飞度”);
System.out.println(“本田纯”);
}否则如果(价格=2000000&&noofpeople>=5){
System.out.println(“本田威泽尔”);
}如果((价格<2000000和价格>1000000)和无员工==5){
System.out.println(“最适合你的汽车是:”);
System.out.println(“本田思域”);
}否则,如果(价格>=200000&&noofpeople>=5){
System.out.println(“找不到车辆”);
}否则如果(没有人<4){
System.out.println(“找不到车辆”);
}
}
}
宝马级轿车{
宝马({
System.out.println(“您选择了宝马”);
如果(价格<1000000&&noofpeople>=4){
System.out.println(“最适合你的汽车是:”);
System.out.println(“本田飞度”);
System.out.println(“本田纯”);
}否则如果(价格=2000000&&noofpeople>=5){
System.out.println(“本田威泽尔”);
}如果((价格<2000000和价格>1000000)和无员工==5){
System.out.println(“最适合你的汽车是:”);
System.out.println(“本田思域”);
}否则,如果(价格>=200000&&noofpeople>=5){
System.out.println(“找不到车辆”);
}否则如果(没有人<4){
System.out.println(“找不到车辆”);
}
}
}
课程结果扩展了Car{
结果(){
c车=新车();
c、 选择();
c、 选择();
开关(品牌){
案例1:
丰田a=新丰田();
打破
案例2:
本田h=新本田();
打破
违约:
宝马n=新宝马();
打破
}
}
}
代码中几乎没有错误的概念。以下是其中一些:
- 您的类
不应扩展结果
。它不是一辆汽车汽车
- 您的类
将汽车的属性与选择汽车的逻辑混合在一起Result
- 将汽车的属性保留在汽车类本身中
- 开关是您输入的一部分,不要让它进入您的模型
- 使用camel标准进行编码,了解更多详细信息
- 我不明白你们为什么把汽车和汽车一起使用
- 就价格而言,
不是生产代码环境中的最佳选择。您正在学习的一个建议是使用int
BigDecimal
- 删除类结果
- 让值位于Car对象内部
- 选择是流程的一部分,而不是模型的一部分
import java.util.Scanner;
class Scratch {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("hello we will help you to choose"
+ "best car for you but for this you have answer some questions.");
System.out.println("Which Brand you want to choose?");
System.out.println("press 1 for Toyota");
System.out.println("Press 2 for Honda");
System.out.println("Press 3 for BMW");
int brand = s.nextInt();
System.out.println("\nPlease enter your price less than or equal to 20lack");
int price = s.nextInt();
System.out.println("please enter your family members");
int numberOfPeople = s.nextInt();
switch (brand) {
case 1:
Car toyota = new Toyota(price, numberOfPeople);
toyota.select();
break;
case 2:
Car honda = new Honda(price, numberOfPeople);
honda.select();
break;
default:
Car bmw = new BMW(price, numberOfPeople);
bmw.select();
break;
}
}
}
abstract class Car {
int price;
int numberOfPeople;
public Car(int price, int numberOfPeople) {
this.price = price;
this.numberOfPeople = numberOfPeople;
}
public abstract void select();
}
class Toyota extends Car {
public Toyota(int price, int numberOfPeople) {
super(price, numberOfPeople);
}
@Override
public void select() {
System.out.println("You selected toyota");
if (price == 1000000 && numberOfPeople == 4) {
System.out.println("The Best Cars for you are:");
System.out.println("passo");
System.out.println("mooco");
} else if (price == 200000 && numberOfPeople == 5) {
System.out.println("Corolla Grande");
} else if (price <= 2000000 && numberOfPeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Corolla gli");
} else if (price > 2000000 && numberOfPeople > 5) {
System.out.println("No car for found");
}
}
}
class Honda extends Car {
public Honda(int price, int numberOfPeople) {
super(price, numberOfPeople);
}
@Override
public void select() {
System.out.println("You selected Honda");
if (price < 1000000 && numberOfPeople >= 4) {
System.out.println("The Best Cars for you are:");
System.out.println("honda fit");
System.out.println("honda pure");
} else if (price == 2000000 && numberOfPeople >= 5) {
System.out.println("honda vezel");
} else if ((price < 2000000 && price > 1000000) && numberOfPeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Honda civic");
} else if (price >= 200000 && numberOfPeople >= 5) {
System.out.println("No car for found");
} else if (numberOfPeople < 4) {
System.out.println("No car for found");
}
}
}
class BMW extends Car {
public BMW(int price, int numberOfPeople) {
super(price, numberOfPeople);
}
public void select() {
System.out.println("You selected BMW");
if (price < 1000000 && numberOfPeople >= 4) {
System.out.println("The Best Cars for you are:");
System.out.println("honda fit");
System.out.println("honda pure");
} else if (price == 2000000 && numberOfPeople >= 5) {
System.out.println("honda vezel");
} else if ((price < 2000000 && price > 1000000) && numberOfPeople == 5) {
System.out.println("The Best Cars for you are:");
System.out.println("Honda civic");
} else if (price >= 200000 && numberOfPeople >= 5) {
System.out.println("No car for found");
} else if (numberOfPeople < 4) {
System.out.println("No car for found");
}
}
}
import java.util.Scanner;
课堂擦伤{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“您好,我们将帮助您选择”
+“最适合你的车,但为此你必须回答一些问题。”);
System.out.println(“您要选择哪个品牌?”);
System.out.println(“丰田按1”);
System.out.println(“本田冲床2”);
System.out.println(“宝马按3”);
int brand=s.nextInt();
System.out.println(“\n请输入小于或等于20L的价格”);
int price=s.nextInt();
System.out.println(“请输入您的家庭成员”);
int numberOfPeople=s.nextInt();
开关(品牌){
案例1:
汽车丰田=新丰田(价格,人数);
toyota.select();
打破
案例2:
汽车本田=新本田(价格,人数);
honda.select();
打破
违约:
汽车宝马=新宝马(价格,人数);
bmw.select();