CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    7

    Please Can Anyone Help me with this?

    I have been having very hard trouble trying to code even these simple codings. I am taking intro to computer science in my school and I have not been able to fully understand the concepts and is getting pretty far behind....


    can anyone just show me how the codes are supposed to look like for these questions


    (Conditional Statement)
    1.) Write a program that asks the user for three numbers(They could be decimals numbers.) The user inputs the numbers one by one(Not necessarily sorted.) Then, the program outputs the three numbers in increasing order.

    (I'm pretty sure this is for loops, while)
    2.) Write a program that prints whether an integer given by user is prime or not

    3.) Write a program that finds all divisors(one by line) of a number given by user.

    Please help me with these if possible.. Thank you

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Please Can Anyone Help me with this?

    can anyone just show me how the codes are supposed to look like for these questions
    We don;t do peoples homework for them but if you show you have made an effort yourself we will help.

    Post the code you have so far and ask a specific question.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2012
    Posts
    7

    Re: Please Can Anyone Help me with this?

    I got to here for #1.

    import java.util.Scanner;

    public class Conditional3{

    public static void main(String [] args){
    Scanner reader = new Scanner(System.in);

    System.out.print(" Enter the first Number: ");
    double number1 = reader.nextInt();
    System.out.print(" Enter the second Number: ");
    double number2 = reader.nextInt();
    System.out.print(" Enter the third Number: ");
    double number3 = reader.nextInt();

    if ( number1 < number2 && number1 < number3) {


    System.out.print("The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    if ( number2 < number3) && number2 > number1){


    System.out.print(" The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    else {


    System.out.print(" The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    }
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Please Can Anyone Help me with this?

    OK, that's your code, so what's your problem apart from the fact that you are reading in an int when you are supposed to be reading in a double.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: Please Can Anyone Help me with this?

    Quote Originally Posted by sunghun94 View Post
    I got to here for #1.

    import java.util.Scanner;

    public class Conditional3{

    public static void main(String [] args){
    Scanner reader = new Scanner(System.in);

    System.out.print(" Enter the first Number: ");
    double number1 = reader.nextInt();
    System.out.print(" Enter the second Number: ");
    double number2 = reader.nextInt();
    System.out.print(" Enter the third Number: ");
    double number3 = reader.nextInt();

    if ( number1 < number2 && number1 < number3) {


    System.out.print("The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    if ( number2 < number3) && number2 > number1){


    System.out.print(" The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    else {


    System.out.print(" The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    }
    }
    }
    Each branch of your if statements is doing the exact same thing (except for an extra space in the last two branches). So your code reduces to:
    Code:
    import java.util.Scanner;
    
    public class Conditional3{
    
    	public static void main(String [] args){
    		Scanner reader = new Scanner(System.in);
    
    		System.out.print(" Enter the first Number: ");
    		double number1 = reader.nextInt();
    		System.out.print(" Enter the second Number: ");
    		double number2 = reader.nextInt();
    		System.out.print(" Enter the third Number: ");
    		double number3 = reader.nextInt();
    
    		System.out.print("The first number is:" + number1 + "The second number is:" + number2 + "The third number is:" + number3);
    	}
    }
    It is just printing the numbers in the order you type them.

    Also, there are 6 possible orders for 3 numbers but you only have three branches anyway. Think them through carefully.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured