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
Re: Please Can Anyone Help me with this?
Quote:
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.
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);
}
}
}
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.
Re: Please Can Anyone Help me with this?
Quote:
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.