Results 1 to 11 of 11
Thread: sweets Java program
- 10-09-2011, 11:54 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
sweets Java program
hi
i am still trying to work out how to do this program i have had lots of help on here on this and for that i am so grateful i am finding java somewhat confusing to say the least but i am not going to give up.
scenario (just in case you don't know all ready.
A teacher has bought a packet of 40 sweets which she is going to share out between her class of 14 students.
Write a program to determine and output the number of sweets each child will receive. As a single sweet can not be shared between pupils, the program must calculate the number of sweets per child and the number that the teacher keeps for herself.
my code so far (with help on here and tutorials)
import java.until.*;
//determine the ouput of number of sweets per child
public class Sweets
{
public static void main (String args[])
{
//create Scanner to read in data
Scanner myKeyboard = new Scanner(System.in);
//prompt user for input – use print to leave cursor on line
System.out.print("Number of sweets ");
int sweets = myKeyboard.nextInt();
System.out.print(" pupil ");
int pupil = myKeyboard.nextInt();
System.out.print("Each pupil ");
int sweet/pupil = myKeyboard.nextInt();
System.out.print("Left for teacher ");
int sweet%pupil= myKeyboard.nextInt();
System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");
}
}
the 2 errors i am getting still.
H:\FPT>javac Sweets.java
Sweets.java:25: error: ';' expected
int sweet/pupil = myKeyboard.nextInt();
^
Sweets.java:29: error: ';' expected
int sweet%pupil= myKeyboard.nextInt();
^
2 errors
Many thanks..
- 10-09-2011, 12:33 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
Re: sweets Java program
Hi there.
You cannot declare variable names with % and /
Java Code:Sweets.java:25: error: ';' expected int sweet/pupil = myKeyboard.nextInt(); ^ Sweets.java:29: error: ';' expected int sweet%pupil= myKeyboard.nextInt();
Java Code:System.out.print(pupil/sweets + " for each pupil "); System.out.print(pupil%sweets + " Left for teacher ");
Regards Andy.Last edited by AndyVidk; 10-09-2011 at 12:40 PM.
- 10-09-2011, 12:36 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: sweets Java program
hi thanks for the speedy reply so do i need to rewrite my code then?
Many thanks Andy
- 10-09-2011, 12:48 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
Re: sweets Java program
Look at my above code, i was a bit to slow to add it in the EDIT. so you probably haven't seen it.
Regards Andy.
- 10-09-2011, 12:55 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: sweets Java program
code:
import java.until.*;
//determine the ouput of number of sweets per child
public class Sweets
{
public static void main (String args[])
{
//create Scanner to read in data
Scanner myKeyboard = new Scanner(System.in);
//prompt user for input – use print to leave cursor on line
System.out.print("Number of sweets ");
int sweets = myKeyboard.nextInt();
System.out.print(" pupil ");
int pupil = myKeyboard.nextInt();
int System.out.print(pupil/sweets + " for each pupil ");
int System.out.print(pupil%sweets + " Left for teacher ");
System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher+ " sweets are left for teacher");
}
}
2 different errors now..
H:\FPT>javac Sweets.java
Sweets.java:19: error: ';' expected
int System.out.print(pupil/sweets + " for each pupil ");
^
Sweets.java:21: error: ';' expected
int System.out.print(pupil%sweets + " Left for teacher ");
many thanks Andy
ps. how to make the code easy to read for you like you did me when you post.
- 10-09-2011, 01:00 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
Re: sweets Java program
I think you have misunderstood something :) you should rad up on the basics again. but here i think this will work:
Java Code:import java.until.*; //determine the ouput of number of sweets per child public class Sweets { public static void main (String args[]) { //create Scanner to read in data Scanner myKeyboard = new Scanner(System.in); //prompt user for input – use print to leave cursor on line System.out.print("Number of sweets "); int sweets = myKeyboard.nextInt(); System.out.print(" pupil "); int pupil = myKeyboard.nextInt(); int eachPupil = sweets/pupil; int leftForTeacher = sweets%pupil; System.out.println("each pupil gets " + eachPupil +" sweets and " + leftForTeacher + " sweets are left for teacher"); } }
Regards Andy.Last edited by AndyVidk; 10-09-2011 at 01:02 PM. Reason: switched "sweets" and "pupil" around.
- 10-09-2011, 01:04 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: sweets Java program
many thanks i am sorry for being a pain i am just starting Java and finding it hard to grasp and is really confusing me but yes i will have to go over from the start again i think (i will get there)
many thanks Andy..
- 10-09-2011, 01:09 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: sweets Java program
H:\FPT>javac Sweets.java
Sweets.java:1: error: package java.until does not exist
import java.until.*;
^
Sweets.java:12: error: cannot find symbol
Scanner myKeyboard = new Scanner(System.in);
^
symbol: class Scanner
location: class Sweets
Sweets.java:12: error: cannot find symbol
Scanner myKeyboard = new Scanner(System.in);
^
symbol: class Scanner
location: class Sweets
3 errors
i still get these errors
- 10-09-2011, 01:15 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 13
- Rep Power
- 0
Re: sweets Java program
It was not meant as such, but the systom.out.print(String); is a void method meaning you it does not return anything than you can put in an int as such :
Java Code:int someVariableName =system.out.println(String);
Java Code:int someVariableName = aInt / anOtherInt;
Java Code:System.out.println("Result: " + someVariableName);
Try changing you import in the top to "util" instead of "until"
Regards Andy.Last edited by AndyVidk; 10-09-2011 at 01:19 PM.
- 10-09-2011, 01:52 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: sweets Java program
hi thks for your help got working but does this look right to you?
H:\FPT>java Sweets
Number of sweets 40
Number of Students 14
Number of sweets 0 Number of sweets per child14the number that the teacher keeps
for herself
Many thanks Andy..
- 10-09-2011, 02:22 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Call one Java Program from another Java Program
By rajpalparyani in forum New To JavaReplies: 3Last Post: 02-14-2011, 05:13 AM -
Is There A Way To Call Another Java Program Within A Java Program
By SwissR in forum New To JavaReplies: 4Last Post: 07-30-2010, 01:25 PM -
execute java program within java program
By popey in forum New To JavaReplies: 2Last Post: 10-22-2009, 06:32 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM
Bookmarks