import java.util.Scanner;
import javax.swing.JOptionPane;
public class Multiples
{
public static void main (String args [] )
{
Scanner input = new Scanner (System.in);
int number1;
int number2;
String message = "";
System.out.println( "Enter first integer" );
number1 = input.nextInt();
System.out.println( "Enter second integer" );
number2 = input.nextInt();
if (number1 % number2 != 0);
message = number1 + " is not a multiple of " + number2;
if (number1 % number2 == 0);
message = number1 + " is a multiple of " + number2;
JOptionPane.showMessageDialog(null, message);
}
}
Scanner as input and JOptionPane as output.
Page 1 of 13 Replies - 756 Views - Last Post: 11 February 2012 - 02:21 PM
#1
Scanner as input and JOptionPane as output.
Posted 10 February 2012 - 10:48 PM
Hello everyone, I have been stuck trying to figure out how to output if an integer is a multiple of another integer. I know how to output the information if i was to use Scanner as my output, but the task is to use Scanner as my input and JOptionPane as my output. If someone here could show me in the correct direction I would be very thankful. Now I understand that in my code I have JOptionPane.showMessageDialog at the very end and thats why in the "message box" I always output "int is a multiple of another int" but no matter what I enter and integers I get the same output even if the integers are not multiples of each other. So basically I want it to be accurate when entering the information. Here is my code:
Replies To: Scanner as input and JOptionPane as output.
#2
Re: Scanner as input and JOptionPane as output.
Posted 10 February 2012 - 11:14 PM
You have a semicolon after your if statements, and since you're not using brackets, the semi-colon (or blank statement), eats your if statements. The code you think is being executed only if a condition is true is being executed regardless. So you will always get the last one there.
TL:DR Remove the ; after your if statements.
TL:DR Remove the ; after your if statements.
#3
Re: Scanner as input and JOptionPane as output.
Posted 10 February 2012 - 11:17 PM
The problems is the semicolon infront of the if conditions:
Remove those semicolons, also you can use if else instead...
if (number1 % number2 != 0);
Remove those semicolons, also you can use if else instead...
#4
Re: Scanner as input and JOptionPane as output.
Posted 11 February 2012 - 02:21 PM
Thank you Sheph and smohd, wow such an easy fix and I dint see that, learning as I go, thanks alot. both post were helpful.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|