4 Attachment(s)
Help me please with my codes
Hi everyone
I've just registered here, and really need your help with my assignment
I have these 2 codes that won't work with me
Q1: Write a program that reads a number of patients and then let the employee enter their names and ages and count them. The program should check if the age of the patient is below 25 years old , otherwise, the patient will not be counted . your program should display total number of patient under the age 25.
Q2: Write a program that reads unlimited number of items and their prices until the user enters “Exit “, after that the program should print out the total bill.
I uploaded my codes and print screen for sample run ,
the problem with Q1 is that the print should be like this :
Enter a Number of Patients > 20
Enter patients Name: sara
Enter patients Age: 23
so I can count the ages under 25 .. ???
the problem with Q2 is when I enter the sentinel "Exit" it won't end the loop !!!
that's all , waiting for your help @};-
Re: Help me please with my codes
no replay :(
I guess the codes aren't clear
Q1:
PHP Code:
import java.util.*;
public class Q_1 {
static Scanner console = new Scanner (System.in);
public static void main (String [] args) {
System.out.print("Enter a number of Patients : " );
int num= console.nextInt();
int count=0;
while (num>=20)
{
System.out.print("Enter patient Name : " );
String s1= console.nextLine();
System.out.println("Enter patient age : " );
int x= console.nextInt();
if (x<25){
count++;
num--;
}
}
System.out.println("Total number of patient under age 25 : "+ count );
}
}
Q2:
PHP Code:
import java.util.*;
public class Q_2 {
static Scanner console = new Scanner (System.in);
public static void main (String [] args) {
final String SENTINEL = "Exit";
System.out.println("Enter an Item and its price (ending with '" + SENTINEL + "'):");
String s1= console.nextLine();
int sum=0;
int x=s1.indexOf(' ',0);
String s2= s1.substring(0,x);
String s3= s1.substring(x+1,s1.length());
int y= Integer.parseInt(s3);
boolean t= s1.equals (SENTINEL);
while (t==false)
{
sum= sum + y;
System.out.println("Enter an Item and its price (ending with '" + SENTINEL + "'):");
s1= console.nextLine();
}
System.out.println("the total bill is : " + sum);
}
}
help me please :(
Re: Help me please with my codes
Try putting the code between code tags.
[*code]
[*/code]
Without the *
Then you will get more replies then with screens. :)
Re: Help me please with my codes
For Q1:
- I would use an HashMap to store the name with the age. (they they are linked together)
- Also add the age to an arrayList if age is below the 25.
- Then have the program look inside the arrayList how many values are below the 25.
Hope that helped. :)
Re: Help me please with my codes
Sara92 Hi and welcome to the forum!
Please see the announcements page for the use of code tags and other useful facts.
Q1: The user is to give a name and an age one at a time. If the current age is less than 25 add 1 to the number of people under 25. If not do not add 1 and continue. How would you do it without a computer? Write the steps down on paper. (No, seriously, write them down) When you have the steps in front of you it is much easier to write the code.
Q2: The problem I see with this program is logical. You didn't write the code to do what you wanted. Again write the steps down on paper and use that to code from. Doing so will help keep the steps in order. For one thing you set the boolean variable before the loop, and the value never changes. You need some way to change the value of that variable inside the loop. Another thing I see is duplicated lines of code. Any time you see yourself writing the same thing over again, question the design. Use your favorite search engine and 'java do while loop' and 'java loop and a half'
The following code can be seen here:
Quote:
You use loop-and-a-half to avoid repeating code from outside the loop to the inside. Example:
Code :
read a;
while a != b do
stuff;
read a;
end
becomes
Code :
while true do
read a
if a == b then break
stuff;
end
Now I only have the read in one place.
Edit
I wouldn't worry about the hash map just yet. That is likely some time down the road from now.
Re: Help me please with my codes
thanks a lot,, I got my mistakes now
I appreciate your advice & i will do it in future..