This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
    Bookmark Topic Watch Topic
  • New Topic

Help with Programming Homework  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello, I am currently taking a java In school with very little prior experience. I am having trouble with the following program. In the program I suppose to print print RandomNumber1 - RandomNumber2. (For an example it should output 9-4). When I do this it only outputs the answer. I would greatly appreciate it if you could help. Thanks!

package Labs;
// Allows the use of Scanner utility for user input
import java.util.Scanner;
import java.util.Random;
public class Lab4 {

public static void main(String[] args) {
// Declare variables
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);

Scanner keyboard = new Scanner (System.in);

if(number1 >= number2)
System.out.println("What is:" + (number1 +(-number2)));
}

}
 
Frankie Law
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Junaid, to answer your question.
If I am reading correctly, you want to output an answer that looks like written arithmetic, am I correct?

In the following program, it looks like you are trying to output a string of arithmetic (9 - 4), but you ended up using the arithmetic itself which gives you the answer ( 5 = 9 - 4 )

Simply change the program output using each variable as if they are string.

println( "What is: " + number1 + " - " + number2 );

Is that what you are asking?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Report post to moderator
Yes That is exactly what I am trying to do. Thank you so much!!
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
Most people make the mistake of using the + sign for String catenation when they think they are doing arithmetic... and wonder why it says 23. I have done that myself. It is explained in the Java® Language Specification (=JLS); that is one of the easier parts of theJLS to understand.
You cannot put a − instead of the + signs in that code; because you can add two Strings together you can use + but you cannot subtract Strings, so − won't compile. So what you have done is to add () until it does compile, and you have converted the operators to arithmetical operators, addition and sign change (=unary minus). Now you are printing the answer. The solution to your problem is what FL said: convert it all back to String catenation by wrapping the arithmetic operators in "" so they become Strings.
And welcome to the Ranch FL.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am still not finished with this program. I need help with displaying correct if the user inputs the correct answer and if not I need to display saying incorrect with what the actual answer is. PLEASE HELP. THANKS!

package Labs;
// Allows the use of Scanner utility for user input
import java.util.Scanner;
import java.util.Random;
public class Lab4 {

public static void main(String[] args) {
// Declare variables
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
int answer;
Scanner keyboard = new Scanner (System.in);

if(number1 >= number2)
System.out.println("What is: " + number1 + " - " +number2 );
answer = keyboard.nextInt();
if(answer == (number1 - number2));
System.out.println("Correct");
if (answer != (number1 - number2));
System.out.println("Incorrect");
if(number2 >= number1)
System.out.println("What is: " + number2 + " - " +number1 );
answer = keyboard.nextInt();

}

}
 
S.Hari Haran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
you are using a random method from Math Class that is in default lang package there is no need to import java.util.Random class unless you are use methods from Random class.Sorry for my english.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Report post to moderator
It would be better to use a Random object and forget about the Math#random method, even if the Java Tutorials seem to like it.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question multiple times. Let us continue the discussion in your original topic
https://coderanch.com/t/670785/java/java/print-random-numbers
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!