Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Using IF statements to compare two numbers

 
Andrena Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning,
I need help with a school assignment using the If and else statement in Java
The teacher is asking: Request a user to enter two whole numbers with comparison operators.
Here is what I have got so far: see below. I have run it through the compiler and interpreter but I cannot get my whole numbers to show up in the output.
I am supposed to be getting whatever number the user entered to look as so:
the number < the other number
the number != the other number and so on...
Please assist

import java.util.Scanner;

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

System.out.println( "Enter Two Numbers (Press Enter after each ): " ) ;
//two variables to hold numbers
double n1 , n2 ;
n1 = readme.nextDouble( ) ;
n2 = readme.nextDouble( ) ;
int a = 0;
int b = 0;

boolean state =  ( n1 != n2 ) ;

if ( n1 != n2 ) {
System.out.println( "not equal to" ) ;
if ( n1 == n2 )
System.out.println( "equal to" ) ;
if ( n1 < n2 )
System.out.println( "a < b" ) ;
if ( n1 > n2 )
System.out.println( "a > b" ) ;
if ( n1 <= n2 )
System.out.println( "a <= b" ) ;
if ( n1 >= n2 )
System.out.println( "a >= b" ) ;





 }

}

}
 
fred rosenberger
lowercase baba
Posts: 13053
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...what output ARE you getting?  

you're not going to get your input numbers printed because...well...you never print them.  All i see you printing is various string literals.  and I'm not sure what your a and b variables are for -  you never use them.


One piece of advice I'd give is to  comment out most of this code.  at minimum, i'd comment out everything after your second "readme" call.  Add in a line or two to print your n1 and n2, to prove you got them.

Once you know you have n1 and n2 correct, just code ONE condition.  Test it to be sure it works right in all cases.  Then add a second, and re-run all your old tests, adding in new ones to be sure everything that used to work still does, and the new code also works correctly.

Keep repeating that until you have all you conditions coded and tested.
 
Andrena Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I get when I compile and interpret my code but the teacher is asking that the numbers that are input show up in the output instead of the letters. I am new to learning this, maybe I am not understanding what he is asking for.
Output.PNG
[Thumbnail for Output.PNG]
 
Andrena Anderson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies, I should have first said " Thank You" and then moved on to my response.
 
fred rosenberger
lowercase baba
Posts: 13053
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well...based on the code you provided, that's about what i'd expect to see.  

Each of your println statements has a string literal. Everything inside the quote marks is printed EXACTLY as you see it.  

What you probably want is to print the value of a variable.  I'm still not sure why you have those 'a' and 'b' variables (which, by the way, are TERRIBLE names for variables).

I would think you want to print the value of n1 and n2, possibly with some text in between, to do that, you can write something like this:

System.out.println("n1 has value " + n1);

What i have there is a string literal:   "n1 has value ", followed by a variable,  the '+' in between means "concatenate these things together".  Java will automatically convert the value of n1 here into a string, combine them, then print out something like "n1 has value 27" or whatever n1 is.

You need to do something like this in each of your println statements.  And note - you can concatenate together multiple variables and strings.  so this should work:

System.out.println("n1 has value " + n1 + ", and n2 has a value " + n2);

see if you can get your code do to what you want now, based off these examples.  Obviously i'm not giving you the exact solution, but i hope you can get it from what I did provide.
 
Andrena Anderson
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much. I will appreciate your help. I will try to follow your hint as to the direction I should be looking. I will post back once I am able to.
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that n1 and n2 are also bad names for variables, but in this case the variables only hold numbers.
It is not necessary to use enter twice to get your numbers with a Scanner.
You are going to run into problems with your formatting. Make sure to indent your code correctly; that way you won't miss how many of your if statements (not If nor IF, please) do and how many don't have {...} after them. That is a surefire recipe for confusion. Try with the same number twice and you can see the program go wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic