English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to write a program that will ask the user their name and their age and depending on their age work out the price of the bus ticket. The output should look like this: What is your name? What is your age? (name) enjoy the ride. The ticket price is: (price)

public class BusTicketPrice1
{
public main static void (String args [])
{
int name
int age
double a = 10.75
double b = 3.50

if (age>11)
price = a

if (age<=10)
price = a - b

getScannerInput.aString("What is your name?");
getScannerInput.anInt("What is your age?");

System.out.println("+ name + enjot the ride ","\n "The ticket price is: + price + ",");

}
}// end class

2006-10-23 01:28:12 · 9 answers · asked by Princess Peach 3 in Computers & Internet Programming & Design

Using Java language and using JCreator

2006-10-23 01:35:58 · update #1

9 answers

The corrections are given in the braces:

public class BusTicketPrice1
{
public main static void (String args []) (public static void main (String args[]))
{
int name;
int age;
double a = 10.75;
double b = 3.50;

if (age>11)
price = a; (Declare the price)

if (age<=10)
price = a - b;

getScannerInput.aString("What is your name?");
getScannerInput.anInt("What is your age?");

System.out.println("+ name + enjot the ride ","\n "The ticket price is: + price + ",");

}
}// end class

2006-10-23 02:21:17 · answer #1 · answered by Anonymous · 0 1

You need to learn the fact that JAVA is an object orientated language, and as such should program with that in mind.

You are writing as though it is procedural, like C.

Define 3 classes, ticket, passenger and main.

Then define methods for name retrieval, age, ticket prices etc in their respective classes. It should mimic a real world environment as each instantiation of a class becomes an object with characteristics specified by the methods that are made available to it.

Coding in Java the way you are at the moment will give you headaches, especially when you move onto something bigger.

2006-10-24 09:40:35 · answer #2 · answered by Caseh 2 · 0 0

Hi...

One problem regarding the approach of the program i can spot is that you are asking the name and age of the passenger but not storing it any variable.

Secondly you are declaring the variables name, age but not assigning any values. The values from the question should go in this variables.

Thirdly you have to do the calculation after asking the age and storing it in the variable. Whereas you are calculating first and then asking the age of the passenger.

I think rectifying this problem will solve and make your program right.

Happy Programming
Hare Krsna.....

2006-10-23 08:46:34 · answer #3 · answered by keep_smiling_n_be_happy 2 · 0 0

Looking at the code from a C programmers point of view I can see some errors in code.

1) You need to declare what "price" is? It should be a double
2) not sure how your specifying how name and age are getting assigned
3 name, age - should they not be string variables and not integer?

I hope those pointers help you.

2006-10-23 16:40:39 · answer #4 · answered by Siu02rk 3 · 0 0

you've not said a) what language this is or b) what your problem is - but I can spot one MAJOR problem straight away...

You need to move the questions (getscannerinput) to BEFORE the age calculations (eg age>11 etc). Currently its trying to do an age calculation before its even requested the age.

It should be something like:

initialise variables
request name
request age
calculate price according to age
output price

make sense? :)

2006-10-23 08:34:00 · answer #5 · answered by gromitski 5 · 0 0

int name;
int age;
double a = 10.75;
double b = 3.50;


if (age>11)
price = a;

if (age<=10)
price = a - b;

simi colon mising

age is always zero

you should use java.swing.joptionpane

and i'm not sure inputdialog("enter ur age ");

2006-10-23 08:40:44 · answer #6 · answered by Rami 5 · 0 0

1. You have name ( a string) assigned to an integer variable
2. You never assign values to your output variables.

2006-10-23 08:36:03 · answer #7 · answered by sjj571 4 · 0 0

hey look ur code...
u are checking the age and then getting the input.
Problem lies here. change it. and in some of the statement u have missed semicolon. correct that. then ur code ll work fine

2006-10-23 13:06:37 · answer #8 · answered by Sudha P 2 · 0 0

major problem is in java you need ; after each line

2006-10-23 08:44:05 · answer #9 · answered by Anonymous · 0 0

fedest.com, questions and answers