|
-
March 18th, 2012, 08:01 PM
#1
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
-
March 19th, 2012, 10:59 AM
#2
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.
-
March 19th, 2012, 02:58 PM
#3
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);
}
}
}
-
March 20th, 2012, 01:38 PM
#4
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.
-
March 20th, 2012, 02:26 PM
#5
Re: Please Can Anyone Help me with this?
 Originally Posted by sunghun94
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|