import java.util.Scanner;
public class ArithmeticDemo {
public static void main(String[] args)
{
int firstNumber;
int secondNumber;
int sum;
int difference;
int average;
Scanner input = new Scanner(System.in);
System.out.print("Please enter a double >> 14");
firstNumber = input.nextInt();
System.out.print("Please enter another double >> 11 ");
secondNumber = input.nextInt();
sum = firstNumber + secondNumber;
difference = firstNumber - secondNumber;
average = sum / 2;
System.out.println(firstNumber + " + " + secondNumber + " is " + sum);
System.out.println(firstNumber + " - " + secondNumber + " is " + difference);
System.out.println("The average of " + firstNumber + " and " + secondNumber + " is " + average);
}
}
I am doing school homework and everything seems fine to me at glance I even took a break and came back but the code is supposed to display when executed
Please enter an integer >> 14
Please enter another integer >> 11
14 + 11 is 25
14 - 11 is 3
The average of 14 and 11 is 12
But when I execute it, it only reads
Please enter an integer >> 14
I am using Eclipse and it stats there is something wrong with my scanner input command but I am unsure of what it is, thanks for the help.
Your print statement should not contain the number there. It should only read " System.out.print("Please enter a double >> ")
". That's the first problem. Also, your prompt reads "Please enter a double", but you are reading an integer. Change that line to input.nextDouble()
. Otherwise, it runs completely as expected. It prints "Please enter a double >> ", I type a number, it prints the next line, I enter another number, and it outputs the information. Take a look at the docs for java.util.Scanner
here
It runs as expected for me
Please enter a double >> 1414
Please enter another double >> 11 11
14 + 11 is 25
14 - 11 is 3
The average of 14 and 11 is 12
The number appears twice as you have it in your prompt. I suggest you change the code to
System.out.print("Please enter a double >> ");
firstNumber = input.nextInt();
System.out.print("Please enter another double >> ");
secondNumber = input.nextInt();
Are you giving your program the input it needs to continue? As in Peters answer you are already printing 14 and haven't inputted it yet. I know its kind of silly but that might be why your program is hanging.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.