Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: what is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong with my code?

    Hi. i was asked to put 3 numbers in array, and print them by order of who's the highest one.
    I am a beginner, so this code is supposed to be simple (no strings what so ever..)
    package Maaracim;
     
    import MyPackage.MyConsole;
     
    public class Maarachim6 {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
     
     
    	int []Array = new int [3];
    	 Array[0]= (int)MyConsole.readInt("leave first  number");
    	 Array[1]= (int)MyConsole.readInt("leave second  number");
    	 Array[2]= (int)MyConsole.readInt("leave third number");
    	 int smallest=Array[0];
    	 int between=Array[0];
    	 int highest=Array[0];
     
    	for (int k = 0; k < Array.length; k++) {
     
    		if (Array[k]<smallest) smallest = Array[k]; 
    		if (Array[k]>highest) highest = Array[k]; 
    		if ( Array[k]>smallest &&Array[k]<highest) between = Array[k];
     
    			}
    	System.out.println("highest number is "+highest);
    	System.out.println("between number is "+between);
    	System.out.println("smallest number is "+smallest);
     
    	}}
    Last edited by matanbn; November 19th, 2019 at 06:47 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: what is wrong with my code?

    Why do you think there is something wrong with the code?
    Can you copy the contents of the command prompt window and paste it here to show what happens when the code is compiled and executed?
    Add some comments describing what the problem is.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code?

    hi. it won't give me back the "between" number correctly ....

    package Maaracim;
     
    import MyPackage.MyConsole;
     
    public class Maarachim6 {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
     
     
    	int []Array = new int [3];
    	 Array[0]= (int)MyConsole.readInt("leave first  number");
    	 Array[1]= (int)MyConsole.readInt("leave second  number");
    	 Array[2]= (int)MyConsole.readInt("leave third number");
    	 int smallest=Array[0];
    	 int between=Array[0];
    	 int highest=Array[0];
     
    	for (int k = 0; k < Array.length; k++) {
     
    		if (Array[k]<smallest) smallest = Array[k]; 
     
    	if (Array[k]>highest) highest = Array[k]; 
     
    	}
    	for (int k = 0; k < Array.length; k++) {
    		if ( Array[k]>smallest && Array[k]<highest) between = Array[k];
     
    			}
    	System.out.println("highest number is "+highest);
    	System.out.println("between number is "+between);
    	System.out.println("smallest number is "+smallest);
     
    	}}

    this is the way i found to correct it, though i don't understand why the first one is not working.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: what is wrong with my code?

    why the first one is not working.
    Find the smallest and largest first, then find the middle one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code?

    Quote Originally Posted by Norm View Post
    Find the smallest and largest first, then find the middle one.
    its what i did

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: what is wrong with my code?

    Yes, that is the way to do it.

    Another way that is possible with only 3 elements to test is with nested if statements.
    if a is low, order b and c
    if b is low, order a and c
    if c is low, order a and b
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Not sure what is wrong with my code.
    By Deeyz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 05:10 AM
  2. what is wrong with my code
    By BLUEJ in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 10th, 2013, 07:52 PM
  3. what is wrong with my code? please help
    By black.angel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 02:34 PM
  4. What's wrong with my code
    By maronski in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2012, 09:07 AM
  5. what is wrong with this code???plz help me :(
    By kanikapant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 17th, 2011, 06:03 PM