我工作的书被称为'Java in two semesters- third addition'在第3章我正在做一个例子(第5号),我无法工作,所以我的理解和逻辑一定是错的!
这个问题要我编写一个程序来接受计算机系统的订单。基本系统例如是300英镑。然后,用户必须以两种不同的价格从两个不同的显示器中进行选择。然后提示用户选择附加设备,例如65.99英镑的DVD或125英镑的打印机。用户需要从这些选项中进行选择,然后程序将显示整个订单的总金额。
我尝试使用嵌套if else ..并使用switch来编写它。我在选择存储计算的多个项目以及选择的所有项目的最终计算时遇到问题。
我使用price1然后将价格2分成两个程序,但它不会编译,因为我有需要初始化的变量。我理解为什么,但没有想到更好的解决方案或尝试的事情:(
我不是在看任何代码直接在代码中给我答案我想要理解逻辑和程序流程所以我可以从我明显错误的东西中学习!
这是一个基本的例子,并没有什么花哨的,因为它在本书的早期,只是通过键盘输入,如果其他..案例和开关所以这些必须是解决方案,如果我能正确地思考这个。< / p>
由于
---代码---
import java.util。*;
公共类orderComputer {
public static void main(String[] args)
{
double baseunit = 375.99;
double screen1 = 75.99;
double screen2 = 99.99;
double dvd = 65.99;
double printer = 125.0;
int total1;
int total2;
double price1;
double price2;
Scanner keyboard = new Scanner(System.in);
System.out.println("************************************************************");
System.out.println("* This is a computer order system *");
System.out.println("* You have already selected the Base Unit at £375.99 *");
System.out.println("* 1. For the 38cm Screen *");
System.out.println("* 2. For the 48cm Sceen £99.99 *");
System.out.println("************************************************************");
total1 = keyboard.nextInt();
switch (total1)
{
case 1: price1 = baseunit + screen1;
System.out.println("The price is currently £" + price1);
break;
case 2: price1 = baseunit + screen2;
System.out.println("The price is currently £" + price1);
break;
default: System.out.println("You did not select a screen!!");
}
System.out.println("************************************************************");
System.out.println("* This is a computer order system *");
System.out.println("* You have already selected the Base Unit at £375.99 *");
System.out.println("* 1. DVD at £65.99 *");
System.out.println("* 2. For a printer £125.00 *");
System.out.println("************************************************************");
total2 = keyboard.nextInt();
switch (total2)
{
case 1: price2 = total1 + dvd;
System.out.println("The price is currently £" + price2);
break;
case 2: price2 = total1 + printer;
System.out.println("The price is currently £" + price2);
break;
default: System.out.println("You did not select a screen!!");
}
}
}
答案 0 :(得分:0)
关于“未初始化的变量”:
最简单的解决方法是将变量初始化为您选择的某个值,例如:
double price1 = 0.0;
double price2 = 0.0;
虽然你的代码在我的机器上编译得很好(java 1.8.0_91)。
但请注意在程序流程中如何初始化/更改值。例如。并且两个switch
语句之一中的无效输入会导致程序吐出不正确的值。
而是使用删除冗余变量的方法。例如:为什么使用多个变量来总结总价?只需为所有添加重用单个变量:
double price = baseunit; //we know for sure the user selected the base-unit
...
switch(total1){
case 1:
price += screen1;
break;
...
}
...
不要为一次性值创建变量。消除total1
和total2
的使用:
double baseunit = 375.99;
double screen1 = 75.99;
double screen2 = 99.99;
double dvd = 65.99;
double printer = 125.0;
double price1;
double price2;
...
switch(keyboard.nextInt()){
...
}
...
在用户输入无效输入后,只需继续执行某个程序,就不会产生任何有用/正确的值。通知他他输入的价值无用,然后再终止或再次询问:
switch(keyboard.nextInt()){
...
default:
System.out.println("Invalid input");
return; //invalid input was entered, program is terminated
}
答案 1 :(得分:-1)
尝试更加面向对象的方法。你正在学习OOP,不是吗? :)
为Product项创建一个类。该类将具有Description字段(字符串)和Price字段。创建一个列表来表示客户的订单,并添加客户选择的所有项目(基本计算机,监视器,任何选项)到列表中。每个订单项都是一个产品。完成后,您可以通过遍历列表并添加其价格字段来获得总计。