Jump to content

I am a beginner in Java Programming. I am still doing my high-school......

 

I want to write a simple car comparison game. here is how.

 

[i use Eclipse] After running the program it will ask to type a car's name which is actually a number, then a 2nd car name.......then it will compare it and choose the winner.

 

I just want to know - how to input a no. as a word.... Example- Ferrari ...it is actually 7.5, then Mini ...it is 6. The program will just compare the numbers..and find the greater one....but it will print the car name as the answer.

 

Here is how I start:

import java.util.*;public class Carz{public static Scanner lP;public static void main (String args[]){lP= new Scanner (System.in);
Link to comment
https://linustechtips.com/topic/264941-a-simple-java-program-help/
Share on other sites

Link to post
Share on other sites

Oh!.....But..I am just showing how I start a program.....Please...help...I clearly know out  teacher is never going to teach that for sure!!!

I know. I'm just talking about general forum code. I don't know Java, I'm just trying to help however I can.

Sig under construction.

Link to post
Share on other sites

I know. I'm just talking about general forum code. I don't know Java, I'm just trying to help however I can.

 
package car;import java.util.*;public class car {	final static double FERRARI = 7.5;	final static double MINI = 6;	static double betterCar = -1;	public static Scanner lP = new Scanner(System.in);	public static void main(String args[]) {		System.out.print("Type car name 1: ");		double car1 = lP.nextDouble();		System.out.print("Type car name 2: ");		double car2 = lP.nextDouble();		if (car1 > car2) {			betterCar = car1;		} else {			betterCar = car2;		}		if (betterCar == FERRARI) {			System.out.print("Better car is Ferrari");		}		if (betterCar == MINI) {			System.out.print("Better car is Mini");		}	}}

Quick and dirty. You can clean it up from there.

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

DayZ Forum Moderator, DayZ Developer, ARMA 3: 2017 Developer, System-Admin, Gameserver-Admin, always interested to learn something new as well as new people.

Link to post
Share on other sites

Switch-Statements are far better.

You cant do switch-case for doubles? 

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

It's not a home work........just trying to how well I know programming...

& That program you gave doesn't seems to work..

Works fine for me. You're supposed to use it and massage the best solution out of it.

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

 

Here is the error I am getting:

 
*Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at car.car.main(car.java:15)

 

Clear out your workspace, change Class name to match whatever you initially put in, massage from there.

And remove the package line.

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

You cant do switch-case for doubles? 

 

Ain't double - and the perfect showcase of using a switch-statement. Especially considering the code-redudancy.

 

 

		if (betterCar == FERRARI) {			System.out.print("Better car is Ferrari");		}		if (betterCar == MINI) {			System.out.print("Better car is Mini");		}

DayZ Forum Moderator, DayZ Developer, ARMA 3: 2017 Developer, System-Admin, Gameserver-Admin, always interested to learn something new as well as new people.

Link to post
Share on other sites

Sorry...but..you almost got my point.......I want type ferrari...not 7.5...

 

Oops. You should be able to figure it out from there.

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

Ain't double - and the perfect showcase of using a switch-statement. Especially considering the code-redudancy.

I tried to use a switch, but then I remembered that you can't with a double because of precision issues. 

Care to show me? I am curious to know what you mean.

CPU Intel Core i7-4790K Motherboard ASUS Saberthooth Z97 Mark1 RAM Corsair Dominator Platinum 16GB @ 2133MHZ GPU 2 X MSI GeForce GTX GTX 980TI GAMING 6G SLI Case Phantex Enthoo Evolv ATX Storage Samsung 840 Pro Series 256GB / Western Digital Caviar Black 2TB PSU Corsair RM1000i Display Asus 4K PB287Q Cooling Noctua NF-A14 FLX & NF-F12 PWM /Thermaltake Water 3.0 Ultimate 360mm Keyboard Corsair K70 Mouse ROCCAT Kone XTD Sound Card Asus Xonar STX

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×