I have been asked to create a program that requires the user purchasing glasses to provide a yes or no input to any additional accessories. Though probably not the cleanest code ever, the true statements work properly, my "false" or else statements are displaying the wrong outputs. I have tried nested ifs, else statements without specifying a false Boolean. I am guessing my problem is with the way I have my integers declared and initialized. Somewhere along the process the values are not being stored properly (if at all). Forgive me if this seems like an extremely basic question, it's been a long time since I've had to code and it was visual basic at that, Java feels extremely foreign to me. I should add, I'm not looking for solutions, just guidance. Thank you in advance, I would normally just walk in during office hours but I'm currently deployed with dang near zero resources.
We are still early in the course, and I've considered the options that our professor may have wanted to see us use a do or while statement. Unfortunately, any guidance has been fairly abstract (I'm aware this is likely done on purpose for freedom of development).
import java.util.Scanner; //for Scanner
class Main {
public static void main(String[] args) {
System.out.println("Basic glasses = $25");
System.out.println("Premium?");
Scanner myScanner = new Scanner(System.in);
String myInput = myScanner.next();
boolean isTrue = (myInput.equals("y"));
boolean isFalse = (myInput.equals("n"));
//declare int
int subTotal = 0;
double total = 0;
int tint = 10;
int polar = 20;
int base = 25;
int premium = 50;
double half = .50;
//premium price
if (isTrue) {
subTotal = base + premium;
System.out.println("$" + subTotal);
}
else if (isFalse) {
subTotal = subTotal + base;
System.out.println("$" + subTotal);
}
System.out.println("tint?");
myScanner.next();
//tint price
if(isTrue) {
subTotal = subTotal + tint;
System.out.println("$" + subTotal);
}
else if (isFalse) {
System.out.println("$" + subTotal);
}
System.out.println("polar?");
myScanner.next();
//polarization price
if (isTrue) {
subTotal = subTotal + polar;
System.out.println("$" + subTotal);
}
else if (isFalse) {
System.out.println("$" + subTotal);
}
System.out.println("Second pair?");
myScanner.next();
//Second pair
if (isTrue) {
total = subTotal + (subTotal * half);
System.out.println("$" + total);
}
else {
total = subTotal;
System.out.println("$" + total);
}
myScanner.close();
}
}
Aucun commentaire:
Enregistrer un commentaire