我是Java新手,甚至不确定自己在做什么。
switch
case
语句和if
else
语句不能很好地发挥作用吗?
我的程序要求是:
首先要求用户提供屏幕尺寸。如果屏幕尺寸等于13.3,请在笔记本电脑价格上加上$ 200。如果屏幕尺寸等于15.0,请在笔记本电脑价格上加上$ 300。如果屏幕尺寸等于17.3,请在笔记本电脑价格上加上$ 400。
向用户询问CPU类型。如果CPU类型等于i3,则在笔记本电脑价格上增加$ 150。如果CPU类型等于i5,则在笔记本电脑价格上加上$ 250。如果CPU类型等于i7,则在笔记本电脑价格上加上$ 350。
向用户询问RAM大小。每增加4GB的内存,笔记本电脑价格就要增加$ 50。然后,询问用户存储类型。有2个选项:SSD和HDD。如果是HDD,则每增加500GB笔记本电脑价格要增加50美元。如果是SSD,则每500GB笔记本电脑价格需增加100美元。
向用户询问屏幕分辨率。有2个选项,FULLHD和4K。如果是FULLHD屏幕,则加$ 100;如果是4K屏幕,则加$ 200。
非常感谢您提供帮助使我的代码更具可读性,或者您可以给我提供任何其他提示!
import java.util.Scanner;
public class laptopConfigurator {
public static void main(String[] args) {
String screenSize = "";
String cpu = "";
int ram = 0;
String storageType = "";
String screenResolution = "";
int memorySize = 0;
double total= 0.0;
Scanner scan = new Scanner(System.in);
System.out.print("Select screen size:");
screenSize = scan.next();
switch(screenSize){
case("13.3"):
total += 200;
break;
case("15.0"):
total += 300;
break;
case("17.3"):
total += 400;
break;
default: System.out.println("Invalid choice");
}
System.out.print("Select CPU type:");
cpu = scan.next();
switch (cpu){
case("i3"):
total += 150;
break;
case("i5"):
total += 250;
break;
case("i7"):
total += 350;
break;
default: System.out.println("Invalid choice");
}
System.out.print("Select RAM size:");
ram = scan.nextInt();
total = total + ram * 50;
System.out.println("Select storage type:");
switch(storageType){
case "SSD":
storageType = "SSD";
break;
case "HDD":
storageType = "HDD";
break;
default: System.out.println("Invalid choice");
}
System.out.println("Enter memory size:");
memorySize = scan.nextInt();
if (storageType.equals("SSD")){
memorySize = memorySize * 100;
total = total + memorySize;
}else if (storageType.equals("HDD"));
memorySize = memorySize * 50;
total = total + memorySize;
System.out.println("Enter screen resolution");
screenResolution = scan.next();
if (screenResolution.equals ("FULLHD")){
total = total +100;
}else if (storageType.equals("4K"));
total = total +200;
System.out.println("Laptop price is: "+ total);
}
}
答案 0 :(得分:2)
storageType
您永远不会在使用System.out.println("Select storage type:");
switch (storageType) {
case "SSD":
storageType = "SSD";
break;
case "HDD":
storageType = "HDD";
break;
default:
System.out.println("Invalid choice");
}
之前重新分配
default
将始终采用switch
分支(这个System.out.println("Invalid choice");
毫无意义吗?)
if (storageType.equals("SSD")) {
memorySize = memorySize * 100;
total = total + memorySize;
} else if (storageType.equals("HDD")) {
;
}
以及使用该变量的后续操作将不正确,例如
SELECT
user_name
,employee_number
,veteran_status
,ethnic_origin
,gender
FROM table.HR hr
LEFT JOIN table.HR hrvs ON hrvs.employee_number = hr.employee_number AND hrvs.veteran_status <> 'not specified'
LEFT JOIN table.HR hreo ON hreo.employee_number = hr.employee_number AND hreo.ethnic_origin <> 'not specified'
LEFT JOIN table.HR hrge ON hrge.employee_number = hr.employee_number AND hrge.gender <> 'not specified'
两个分支都将被跳过,最终价格将是错误的。
答案 1 :(得分:-2)
您需要添加布尔标志并将菜单包装在while
循环中。注意:我是在手机上写的,所以我面前没有您的代码。
Boolean validRam = false; // same for screen size, etc.
while(!validRam) {
// same code as normal
switch(choiceRam) {
case "16":
totalCost +=250;
validRam = true; // <--- this line
break;
case "32":
// etc
}
}