The program I am trying to finish needs to read in three numbers from the user and then store them in the variables num1, num2 and num3 so that they can be used in steps 2 and 3. The problem I am having is that when the program loops through after the user inputs their numbers and tries to move on to step 2 or 3, it says must complete step 1 first.
This is my code as of right now:
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1;
int num2;
int num3;
int input;
do {
boolean opt1Done = false;
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
int answer = console.nextInt();
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();
input = console.nextInt();
if (answer == 1) {
//do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
System.out.println("Enter a value for num2 between 1 and 100.");
System.out.println("Enter a value for num3 between 1 and 100.");
opt1Done = true;
} else if (answer == 2) {
if (opt1Done) {
//...... do whatever to order the numbers
int[] arraynum;
arraynum = new int[3];
arraynum[0] = num1;
arraynum[1] = num2;
arraynum[2] = num3;
Arrays.sort(arraynum);
int i;
for (i = 0; i < arraynum.length; i++) {
System.out.println("num:" + arraynum[i]);
}
} else {
System.out.println("you must complete Step 1 before Step 2");
}
} else if (answer == 3) {
if (opt1Done) {
//... do whatever to determine if triangle or not
if (num1 + num2 > num3 && num1 + num3 > num2 && num2 + num3 > num1) {
System.out.print("TRIANGLE");
} else {
System.out.print("NO TRIANGLE");
}
} else {
System.out.println("you must complete Step 1 before Step 3");
}
if (answer == 4) {
System.exit(0);
}
}
} while (input != 4);
}
}
Do I need to add another loop or change the one already have?
Move the boolean declaration opt1Done and the numbers outside the loop and read input after every user prompt.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class ReadChar {
public ReadChar() {
// TODO Auto-generated constructor stub
}
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1 = 0;
int num2 = 0;
int num3 = 0;
boolean opt1Done = false;
while (true) {
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out
.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
int answer = console.nextInt();
if (answer == 1) {
// do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
num1 = console.nextInt();
System.out.println("Enter a value for num2 between 1 and 100.");
num2 = console.nextInt();
System.out.println("Enter a value for num3 between 1 and 100.");
num3 = console.nextInt();
opt1Done = true;
} else if (answer == 2) {
if (opt1Done) {
// ...... do whatever to order the numbers
int[] arraynum;
arraynum = new int[3];
arraynum[0] = num1;
arraynum[1] = num2;
arraynum[2] = num3;
Arrays.sort(arraynum);
int i;
for (i = 0; i < arraynum.length; i++) {
System.out.println("num:" + arraynum[i]);
}
} else {
System.out
.println("you must complete Step 1 before Step 2");
}
} else if (answer == 3) {
if (opt1Done) {
// ... do whatever to determine if triangle or not
if (num1 + num2 > num3 && num1 + num3 > num2
&& num2 + num3 > num1) {
System.out.print("TRIANGLE");
} else {
System.out.print("NO TRIANGLE");
}
} else {
System.out
.println("you must complete Step 1 before Step 3");
}
}
if (answer == 4) {
System.exit(0);
}
}
}
}
You don't loop over the instructions. Every time you have a step completed, the program then ends and the next time you run it, nothing you've done before is saved.
You need an outer loop to iterate over commands:
public static void main(String[] args) {
int num1;
int num2;
int num3;
boolean opt1Done = false;
while (true) {
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
//rest of your code here
}
}
It seems that you must loop until the user choose the option 4. I don't see any loops that encompass the options asking questions.
Hope this helps
Introduced a while loop and opt1True should outside of the loop
check for is answer 4 is inside answer 3. So moved that in else if to be same level with other answer checks
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1 = 0;
int num2 = 0;
int num3 = 0;
boolean opt1Done = false;
while(true) {
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
int answer = console.nextInt();
if (answer == 1) {
// do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
num1 = console.nextInt();
System.out.println("Enter a value for num2 between 1 and 100.");
num2 = console.nextInt();
System.out.println("Enter a value for num3 between 1 and 100.");
num3 = console.nextInt();
opt1Done = true;
} else if (answer == 2) {
if (opt1Done) {
// ...... do whatever to order the numbers
int[] arraynum;
arraynum = new int[3];
arraynum[0] = num1;
arraynum[1] = num2;
arraynum[2] = num3;
Arrays.sort(arraynum);
int i;
for (i = 0; i < arraynum.length; i++) {
System.out.println("num:" + arraynum[i]);
}
} else {
System.out.println("you must complete Step 1 before Step 2");
}
} else if (answer == 3) {
if (opt1Done) {
// ... do whatever to determine if triangle or not
if (num1 + num2 > num3 && num1 + num3 > num2
&& num2 + num3 > num1) {
System.out.print("TRIANGLE");
} else {
System.out.print("NO TRIANGLE");
}
} else {
System.out.println("you must complete Step 1 before Step 3");
}
} else if (answer == 4) {
System.exit(0);
}
}
}
you are putting condition in wrong place.
as i am thinking first check answer from user then take those 3 numbers inside first condition as
int answer = console.nextInt();
if (answer == 1) {
//do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
num1 = console.nextInt();
System.out.println("Enter a value for num2 between 1 and 100.");
num2 = console.nextInt();
System.out.println("Enter a value for num3 between 1 and 100.");
num3 = console.nextInt();
opt1Done = true;
}
and move this line outside of loop
boolean opt1Done = false;
All you had to do was add a while loop to your logic and reorder it.
boolean opt1Done = false;
int num1, num2, num3;
while(answer != 4){
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
int answer = console.nextInt();
if (answer == 1) {
//do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
num1 = console.nextInt();
System.out.println("Enter a value for num2 between 1 and 100.");
num2 = console.nextInt();
System.out.println("Enter a value for num3 between 1 and 100.");
num3 = console.nextInt();
opt1Done = true;
}
...
if (answer == 4) {
System.exit(0);
}
}
}
}
Your problem was every time you were looping you were re-declaring and setting the boolean variable to false. Plus your console.nextInts() were in the wrong spot. Also do while is not necessary in this case because the starting value of answer is null and therefore is not equal to four.
Related
I am trying to create a Java program that reads a double value from the user, Printing the difference between these two numbers so that the difference is always positive. I need to display an Error message if anything other than a number is entered. Please help, thank you !!
When I run the program and enter the second double value nothing happens. I have also tried adding try and catch but I get errors saying num1 cannot be resolved to a variable :(
import java.util.Scanner;
public class Positive {
public static void main(String[] args) {
//Reads input
Scanner sc = new Scanner(System.in);
System.out.println ("Please enter a double vaule: ");
double num1 = Math.abs(sc.nextDouble());
System.out.println("Please enter a second double vaule: " );
double num2 = Math.abs(sc.nextDouble());
double total = 0;
double total2 = 0;
if (sc.hasNextDouble()) {
num1 = sc.nextDouble();
if (num1>num2) {
total = ((num1 - num2));
System.out.println("The difference is " + total);
}
if ((num1 < num2)); {
total2 = ((num2 - num1));
System.out.println("The difference is "+ total2);
}
}else {
System.out.println("Wrong vaule entered");
}
}
}
You have the right idea. You just need to remove the semicolon (;) after the last if and properly nest your conditions:
if (sc.hasNextDouble()) {
num1 = sc.nextDouble();
if (num1 > num2) {
total = (num1 - num2);
System.out.println("The difference is " + total);
}
else if (num1 < num2) {
total2 = (num2 - num1);
System.out.println("The difference is "+ total2);
}
} else {
System.out.println("Wrong vaule entered");
}
Try running your code and when you enter your first double, type in something random like "abc". What happens? In your console it should display an error named java.util.InputMismatchException. You can use a try catch block like so:
try {
//tries to get num1 and num2
double num1 = Math.abs(sc.nextDouble());
double num2 = Math.abs(sc.nextDouble());
} catch (java.util.InputMismatchException i){
//will only go here if either num1 or num2 isn't a double
System.out.println("error");
}
Basically, the code will try to get num1 and num2. However if you enter a non-double, it'll "catch" the java.util.InputMismatchException and go to the catchblock
I might've misinterpreted your question, but if you want to find the absolute value of the difference between num1 and num2, all you have to do is:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Please enter a double value: ");
double num1 = sc.nextDouble();
System.out.println("Please enter a double value: ");
double num2 = sc.nextDouble();
System.out.println("The difference is: " + Math.abs(num1 - num2));
} catch (java.util.InputMismatchException i) {
System.out.println("error");
}
}
}
To show error message until the user enters a double value I used a while loop for each value (num1 and num2).
If user enters a wrong value "Wrong vaule entered, Please enter again: " message will show and waits for the next input sc.next();
If user enters a double value check will be false and exits while loop
import java.util.Scanner;
public class Positive {
public static void main(String[] args) {
// Reads input
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a double vaule: ");
double num1 = 0;
double num2 = 0;
double total = 0;
double total2 = 0;
boolean check = true;
while (check) {
if (sc.hasNextDouble()) {
num1 = Math.abs(sc.nextDouble());
check = false;
} else {
System.out.println("Wrong vaule entered, Please enter again: ");
sc.next();
}
}
check = true; // that's for second control
System.out.println("Please enter a second double vaule: ");
while (check) {
if (sc.hasNextDouble()) {
num2 = Math.abs(sc.nextDouble());
check = false;
} else {
System.out.println("Wrong vaule entered, Please enter again: ");
sc.next();
}
}
if (num1 > num2) {
total = ((num1 - num2));
System.out.println("The difference is " + total);
} else if ((num1 < num2)) {
total2 = ((num2 - num1));
System.out.println("The difference is " + total2);
}
}
}
The answer is probably staring me in the face but I have been looking at this so long the words are blurring together. The assignment is to have the user input 3 numbers, add the numbers together using a method, then to determine if the sum is a prime number using a different method.
package chpt6_Project;
import java.util.Scanner;
public class Chpt6_Project {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1;
int num2;
int num3;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first number: ");
num1 = scan.nextInt();
System.out.println("Enter the second number: ");
num2 = scan.nextInt();
System.out.println("Enter the third number: ");
num3 = scan.nextInt();
Chpt6_Project.sum(num1, num2, num3);
if(isPrime()) {
System.out.println("The number is prime");
} else {
System.out.println("The number is not prime.");
}
}
public static void sum(int num1, int num2, int num3) {
int total = num1 + num2 + num3;
System.out.println(total);
}
public static boolean isPrime(int total) {
if((total > 2 && total % 2 == 0) || total == 1) {
return false;
}
for (int i = 3; i <= (int)Math.sqrt(total); i += 2) {
if (total % i == 0) {
return false;
}
}
return true;
}
}
Edit the code as follow and you should do the trick.
The sum function now returns the sum calculated, this value is passed by main to the isPrime function which will return the right value
package chpt6_Project;
import java.util.Scanner;
public class Chpt6_Project {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1;
int num2;
int num3;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first number: ");
num1 = scan.nextInt();
System.out.println("Enter the second number: ");
num2 = scan.nextInt();
System.out.println("Enter the third number: ");
num3 = scan.nextInt();
if(isPrime(Chpt6_Project.sum(num1, num2, num3))) {
System.out.println("The number is prime");
} else {
System.out.println("The number is not prime.");
}
}
public static int sum(int num1, int num2, int num3) {
int total = num1 + num2 + num3;
System.out.println(total);
return total;
}
public static boolean isPrime(int total) {
if((total > 2 && total % 2 == 0) || total == 1) {
return false;
}
for (int i = 3; i <= (int)Math.sqrt(total); i += 2) {
if (total % i == 0) {
return false;
}
}
return true;
}
Morover i guess this is an homework but there are better way of doing this. For example, there is no need for a sum function.
I'm having trouble finishing this program. I understand what the program is suppose to do, but I'm having trouble finishing it. I have my code posted below.
For this program I am required to determine if a number is a prime number. A part from this, I'm required to ask the user to enter a range (ex. 1-10) and display which numbers are prime and which are not.
This is what I have so far...
import java.util.Scanner;
public class PrimeNumbers
{
public static void main(String[]args)
{
//Create Scanner Object
Scanner input = new Scanner(System.in);
//Initialize variable
double num1, range;
//Prompt the user to enter in a number
do
{
System.out.println("Please enter in a number:");
num1 = input.nextDouble();
//Decision making
if(num1 % 2 == 0 || num1 % 3 == 0 || num1 % 4 == 0 || num1 % 5 == 0 || num1 % 6 == 0 || num1 % 7 ==0 || num1 % 8 ==0 || num1 % 9 == 0)
{
//Display message
System.out.println(num1 + " is not a prime number.");
System.out.println("Please enter a range: ");
range = input.nextInt();
if ()
}
else
//Display output
System.out.println(num1 + " is prime.");
}
while(num1 == -1);
{
System.out.println("This program has now ended.");
}
}
}
http://beginnersbook.com/2014/01/java-program-to-display-prime-numbers/
import java.util.Scanner;
class PrimeNumbers2
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to n are :");
System.out.println(primeNumbers);
}
}
So this is my code for a simple calculator. It works for the most part, and I'm trying to create a loop at the end of the operation to allow a user the chance to choose to do another operation or exit. So far I've tried adding a nested if loop but I keep getting an error. Any help would be helpful. I'm fairly new.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
// Introduction to Calculator providing instructions to user
Scanner input = new Scanner(System.in);
int num1, num2, ans, Y, N;
System.out.println("Welcome to Simple Calculator: Please enter \n" +
"which mathimatical operation you would like to \n "
+ "accomplish by imputing 1-addition, 2-subtraction \n"
+ "3-multiplication, 4-division, 5-modulo");
int Operation = input.nextInt();
//Operation sequence for user to input which operation they would like to accomplish
if (Operation == 1) {
System.out.println("You have Choosen Addition");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 + num2;
System.out.print("Answer is: " + ans);
/*
* Right here in the following I want to put something in to make this loop
* so the user can make multiple calculation.
* So far nothing has worked.
*/
System.out.println("Would you like to do another calculation?\n " +
"Enter Y for Yes or N to Exit");
}else if (Operation == 2){
System.out.println("You have Choosen Subtraction");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 - num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 3){
System.out.println("You have Choosen Multiplication");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 * num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 4){
System.out.println("You have Choosen Division");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 / num2;
System.out.print("Answer is: " + ans);
}else if (Operation == 5){
System.out.println("You have Choosen Modulo");
System.out.print("Enter your First number: ");
num1 = (int) input.nextDouble();
System.out.print("Enter your second number: ");
num2 = (int) input.nextDouble();
ans = num1 % num2;
System.out.print("Answer is: " + ans);
}else{
System.out.print("Invalid Operation, please try again.");
}
}
}
You're on the right track here. I would suggest using a do-while loop. So encase the entire part beginning with if (Operation == 1) { all the way down to your final else statement like this:
do {
/*
if (Operation == 1) {
...
...
...
else {
...
}
*/
System.out.print("Would you like to do another calculation? [Y/n] ");
} while (input.nextLine().toUpperCase().charAt(0) == 'Y');
I would put the entire thing in a while loop and at the end ask if they want to continue
yes - it goes back through the loop
no - you have it break out of the loop
I am trying to get my code to loop back to the beginning of the program after the user completes one of the options. I cannot seem to figure out how to get it to work properly. Here is my code so far
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1;
int num2;
int num3;
boolean opt1Done = false;
System.out.println("Select your next step");
System.out.println("1: Enter three numbers between 1 and 100.");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
System.out.println("4: Exit");
int answer = console.nextInt();
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();
if (answer == 1) {
//do whatever for option 1
System.out.println("Enter a value for num1 between 1 and 100.");
System.out.println("Enter a value for num2 between 1 and 100.");
System.out.println("Enter a value for num3 between 1 and 100.");
opt1Done = true;
} else if (answer == 2) {
if (opt1Done) {
//...... do whatever to order the numbers
int[] arraynum;
arraynum = new int[3];
arraynum[0] = num1;
arraynum[1] = num2;
arraynum[2] = num3;
Arrays.sort(arraynum);
int i;
for (i=0; i < arraynum.length; i++) {
System.out.println("num:" + arraynum[i]);
}
} else {
System.out.println("you must complete Step 1 before Step 2");
}
} else if (answer == 3) {
if (opt1Done) {
//... do whatever to determine if triangle or not
if (num1+num2>num3 && num1+num3>num2 && num2+num3>num1)
{
System.out.print("TRIANGLE");
}
else
{
System.out.print("NO TRIANGLE");
}
} else {
System.out.println("you must complete Step 1 before Step 3");
}
}
}
}
Basically I need it so that after the user enters 2 and completes option 2, the program will then go back to the beginning and ask again to choose which option the user wants. How can I get this to work properly? Also if anything else is wrong with the code that I do not see please let me know. Thanks
What you need is put this whole bunch of code in while loop which will always be true and ask the user input again at end. If user presses 4 which i think is exit just exit the program by using break in your while loop.
int input;
do {
//---
//The rest of your logic
//---
} while(input != 4);
You can try something like this.
Boolean p = true;
while(p){
System.out.println(
" Select an option:\n" +
"1: Enter three numbers between 1 and 100.\n" +
"2: Order your number in ascending order\n"+
"3: Determine if the three inputs form a triangle\n"+
"4: Exit\n"
);
switch(){
case 1:
.
.
.
case 4:
System.exit();
}
}