import java.util.Scanner; public class TVSale { public static void main(String[]
ID: 3626555 • Letter: I
Question
import java.util.Scanner;public class TVSale {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//scan inputs and prompt error message if input is invalid
System.out.println("size [inches]: ");
double size = input.nextDouble();
if (size < 10 || size > 45) {
System.out.println("invalid size");
return;
}
System.out.println("type [LCD/CRT]: ");
String type = input.next();
if ((size > 10 && size <= 45)&&((type != "LCD") || (type != "CRT"))) {
System.out.println("invalid type");
return;
}
System.out.println("tag price [$]: ");
double price = input.nextDouble();
if ((size > 10 && size <= 45)&&(type != "LCD" || type != "CRT")&&(price < 30.95)){
System.out.println("invalid price");
return;
}
// nested if's
if (size >= 10 && size < 20)
if (type.equals("LCD"))
System.out.println("disc price: " + price*0.95);
else if (type.equals("CRT"))
System.out.println("disc price: " + price*0.80);
else if (size >= 20 && size < 30)
if (type.equals("LCD"))
System.out.println("disc price: " + price*0.90);
else if (type.equals("CRT"))
System.out.println("disc price: " + price*0.78);
else if (size >= 30 && size < 35)
if (type.equals("LCD"))
System.out.println("disc price: " + price*0.85);
else if (type.equals("CRT"))
System.out.println("disc price: " + price*0.75);
//manager's discount on TVs with size between 35'' to 45''
else if (size >= 35 && size < 40)
if (type.equals("LCD"))
System.out.println("disc price: " + (price*0.85)*0.94 + " - manager's discount.");
else if (type.equals("CRT"))
System.out.println("disc price: " + (price*0.78)*0.94 + " - manager's discount.");
else if (size >= 40 && size < 45)
if (type.equals("LCD"))
System.out.println("disc price: " + (price*0.80)*0.94 + " - manager's discount.");
//crt with size >= 40 invalid
else if (type.equals("CRT"))
System.out.println("Invalid type");
}
}
This is what I have so far....
But this code will not work properly.
How would you write "type not equal to LCD or CRT"? I searched online and they said to write "! type.equals("LCD")" like this but it still doesn't work.
I also have question about the keyword "return".
We did not learn to use this in class so I was wondering if there is any other way to put this so that when you type in wrong size, type, or price, it would end and print out an error message.
Explanation / Answer
!type.equals("LCD") is the correct way to check if type equals "LCD". Your boolean logic is unsound. You used or instead of and.
if ((size > 10 && size <= 45)&& ((type != "LCD") && (type != "CRT")))
As for returning from the method, there are 2 other ways. One is to structure your program using if statements, so that if an error occurs, no other statements are executed. The other way is to throw an exception. The best way is to use return.
When you use return methods, which return a value, return is used to return the value, and is the last statement in the method. A void return type does not return a value, but return will exit the method. I assure you that if you haven't learned about return yet in class, you soon will.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.