Results 1 to 20 of 24
Thread: how do i write this code?
- 06-21-2010, 02:48 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
how do i write this code?
the goal is to make the program get 10 random numbers and then choose the number which is the biggest. i think i am wrong somewhere. how can i write the code correctly?
class Ex4
{
public static void main(String[] args)
{
int count;
count=0;
while(count<10)
{
Console console=System.console();
System.out.println("put a number");
String number
number=console.readLine();
int n1;
n1=Integer.parseInt(number);
System.out.println("put next number");
String next_number
next_number=console.readLine();
int n2;
n2=Integer.parseInt(next_number);
System.out.println("biggest value"+(n2>n1?n2:n1));
count=count+1;
}
}
}
- 06-21-2010, 02:50 PM #2
Can you describe the logic/design of your program?
A good design before writing code, makes for a better solution.
- 06-21-2010, 02:59 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
the program should give the user 10 questions/options to put any number he wants.
after he put the 10 numbers when asked, the program at the end should choose the highest number
- 06-21-2010, 03:04 PM #4
What you've just posted is a statement of the problem. It doesn't sound like the original problem??
Now you need to come up with a design on how to solve that problem.
That means what the steps are to solve the problem.
- 06-21-2010, 03:26 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
it tells me that i have an error:
Console console = System.console();
Ex:java13 cant find symbol
symbol class console
Location class Ex4
why? how do i correct that?
- 06-21-2010, 03:29 PM #6
Please copy and paste the FULL text of the error message. There is info there that you left out.
Do you have an import statement for the package the Console class is in?
- 06-21-2010, 03:33 PM #7
Where did you get that code from in the first place? Read the API doc for System.console() and read the basic IO tutorial.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-21-2010, 04:39 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
import java.io.*;
class Ex4
{
public static void main(String[] args)
{
int count;
count=0;
while(count<10)
{
System.out.println("put a number");
Console console=System.console();
String number;
number=console.readLine();
int n1;
n1=Integer.parseInt(number);
System.out.println("Your number is "+ " " + n1);
count=count+1;
}
int n2;
System.out.println("Your Biggest number is"+ n2>n1?n2:n1);\\ i need to set this line to give me the biggest value that was displayed
}
}
the program works like that. the user types 10 numbers. and what i need to do is in the last line present the largest number he typed.
i need one command line to make this work, but i dont know what it is
- 06-21-2010, 04:44 PM #9
For starters your code is fragile, but you seem to accept that. Secondly you never remember the numbers being entered, so you have no chance to find the biggest number.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-21-2010, 04:50 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
please tell me how to do that. i want to get over with that exercise. i am workign on those codes since the morning
what if i write that the user needs to type 10 numbers... - that would be n1
would i be able to make a formala finding the largest number?Last edited by Libertyman; 06-21-2010 at 04:52 PM.
- 06-21-2010, 04:55 PM #11
No way, it doesn't work that way. Learn! You've been given the link to the basic tutorials, find out how to use arrays and sorting algorythms, or do it with pen and paper and imagine there could be somebody asking you anytime about the biggest number so far.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 06-21-2010, 04:56 PM #12
How would you do it if you were using a piece of paper and pencil?
A user gives you a number
You write it down on the paper
You get the next number.
Is this number bigger than the last number?
If so, replace the last number written with the new number
Loop back to get the next number
When you are done, the number written down will be the largest.
In a program use variables to save values instead of writing them down
- 06-21-2010, 05:03 PM #13
@PhHein Great minds work in ....do it with pen and paper
- 06-21-2010, 06:33 PM #14
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
That would seem to be a long code.
giving a value to each variable.
like this
int 1
int b
int c
int d
int e.
i dont think it would be the answer.
- 06-21-2010, 07:12 PM #15
Can you explain your last post?giving a value to each variable.
There is only one variable to hold the current largest one.
And one variable to hold what the user input.
- 06-21-2010, 07:49 PM #16
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
Java Code:import java.io.*; import java.io.*; class Ex4 { public static void main(String[] args) { int count; count=0; while(count<10) { System.out.println("put a number"); Console console=System.console(); String number; number=console.readLine(); int n1; n1=Integer.parseInt(number); int n2; n2=n1 System.out.println("Your number is "+ " " + n1 + "Biggest input" + (n2>n1)?n2:n1); count=count+1; } } }
or
why does he tell me that System.out.println line is wrong?Java Code:import java.io.*; class Ex4 { public static void main(String[] args) { int count; count=0; while(count<10) { System.out.println("put a number"); Console console=System.console(); String number; number=console.readLine(); int n1; n1=Integer.parseInt(number); int n2; n2=n1>n2?n1:n2; System.out.println("Your number is "+ " " + n1 + "Biggest input"+ " " + n2); count=count+1; } } }
why cant i use a trillneary expression?
actually the whole think is a mess. i want to put a new statement out of the loop
System.out.println(" your Biggest number" = )
but i wonder how can i make the programme make that calculationLast edited by Libertyman; 06-21-2010 at 08:07 PM.
- 06-21-2010, 07:56 PM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Two problems: your use of = within the expression rather than ==, and your trying to compare a String with an int.
Edit: I see that you modified your post, which I'll post here:
but you're still using = to compare one number with the other, not ==. Please understand that = assigns a value to a variable whereas == compares two variables. Also, you are comparing n1 with n2, but never assigned any value to n2 so it's meaningless. What is n2 supposed to represent?Java Code:int n1; n1=Integer.parseInt(number); int n2; n2=n1>n2?n1:n2; System.out.println("Your number is "+ " " + n1 + "Biggest input"+ " " + n2);Last edited by curmudgeon; 06-21-2010 at 08:20 PM.
- 06-21-2010, 08:30 PM #18
Member
- Join Date
- Jun 2010
- Posts
- 26
- Rep Power
- 0
i saw that problem with n2. i deleted the whole thing
i want n2 to be the previous number..
but because it is a loop..the whole thing is too confusing and tricky.
i think someone who knows java well, should know..
after i have the values from the loop. how can i make the application list the highest value from the list?
- 06-21-2010, 08:37 PM #19
You still haven't come up with a design for how the program should work.
Look at this:
There are two variables: currVal and highestVal.
Set highestVal to the smallest number possible so that any number input will be larger
1) get the next number from user into currVal
2) compare currVal to highestVal
3) if currVal is greater than highestVal then set highestVal to currVal (ie save the new highest value)
4) test if 10 numbers gotten yet (loop test)
5) if < 10 numbers go to step 1)
6) if 10 numbers, then highestVal has the highest value entered
- 06-21-2010, 08:37 PM #20
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Then declare n2 before the loop even starts:
But you probably want to give it some initial value too, so you'll need to declare and assign it, again before the while loop:Java Code:int n2; // this is a variable declaration
Java Code:int n2 = Integer.MIN_VALUE; // this is a variable declaration with an assignment while (count < 10) { .....
Similar Threads
-
What are you using to write your code?
By CaptainMorgan in forum New To JavaReplies: 947Last Post: 05-14-2013, 05:58 PM -
New to java and does not know how to write the code ...
By poisson in forum New To JavaReplies: 57Last Post: 07-15-2010, 04:10 AM -
How To Write Unmaintainable Code
By tim in forum Forum LobbyReplies: 2Last Post: 02-07-2010, 09:19 PM -
Can I write code in 2 different files ?
By fartek in forum New To JavaReplies: 2Last Post: 02-14-2009, 03:48 PM -
Help me to write a secure code
By Bhavis in forum Advanced JavaReplies: 4Last Post: 01-21-2009, 06:06 AM


LinkBack URL
About LinkBacks


Bookmarks