+ 2

Whats Wrong in this code?

Help Me Please!!! https://code.sololearn.com/cGy6qoqM7EAy/?ref=app

4th Sep 2017, 3:27 PM
Irman Utamara
Irman Utamara - avatar
3 Réponses
+ 9
import java.util.Scanner; public class Program { // ■enter ur name , car name and no. of cars u want of that type ... leave space after each input public static void main(String[] args) { String car,name; int quantity; float total; float price=0; Scanner key = new Scanner(System.in); System.out.println("Sell Car "); System.out.println("------------"); System.out.println("1.BMW \n2.FERARI"); System.out.print("Your Name ? "); name = key.nextLine(); System.out.print("Input the name of car : "); car = key.nextLine(); System.out.print("Quantity ?"); quantity = key.nextInt(); if((car.equals("BMW"))||(car.equals("bmw"))) //■ { price=100000; } else if((car.equals("ferari"))||(car.equals("ferari"))) //■ { price=200000; } else { System.out.print(" hey , we sell only ferrari or bmw ... thanks for trying "); System.exit (0); } //■ total=price*quantity; System.out.println("Output\n----------\n"); System.out.println("Name : "+name); System.out.println("Quantity : "+quantity); System.out.println("Total : "+total); System.out.println("Thanks For Buying"); } } //hope it helps☺
4th Sep 2017, 3:44 PM
Changed
Changed - avatar
+ 4
Your float "price" is put forward by 1 space indent from the rest of the code. Thats just from upfront. Further looking is required for analysis.
4th Sep 2017, 3:30 PM
ghostwalker13
ghostwalker13 - avatar
+ 4
1. In Java, == is a reference comparison operator. Since variable 'car' and a string literal "BMW" is a different thing, it will return false. If you want to compare content with another String, you should use equals(). For example in your code, if(car=="BMW"){} should be if(car.equals("BMW")){}. +You can use equalsIgnoreCase() if you want to compare two string without considering case. 2. if(car=="BMW" && car=="bmw"){} You should use || here since a variable car can't be both "BMW" and "bmw". You might want to go into if statement either car is "BMW" or "bmw". Fixed code will be if(car.equals("BMW") || car.equals("bmw")){}.
4th Sep 2017, 3:44 PM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar