d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question > Hm
Prev123
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 08:30pm
This is what we were attempting to avoid by extracting all the duplicate code, and doing it once before we evaluated the other choices. While this may work, it is not very good design. I would challenge you to refactor it so that you only have to prompt for input and input it once for each operation, like I had in my example. You are very close.
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 08:35pm
I will try this, and I think I understand what you're articulating.. I'm pressed for time, having to get up in 8 hours for work so I'm going to keep it for now and finish.

I am stuck in the part: While-------code------ and then I get to my else(the one that represents anything deviating from normal input) and my idea would be first to set a counter.. and use the i++ method to log the times/counts the user has reacher this output, set it i<=3 allowing the user to try again, and then i==3 break at that point... I have the while setup but unsure of the code needed for this.. I'll be digging through my book.


Maybe there is a fancy way of using a different class and breaking, logging each counter separately, but I'm trying to find the most simplistic to start


Also, would boolean status come into play to log the count?

This post was edited by axid3nt on Jan 25 2015 08:38pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 08:43pm
Quote (axid3nt @ Jan 25 2015 09:35pm)
I will try this, and I think I understand what you're articulating.. I'm pressed for time, having to get up in 8 hours for work so I'm going to keep it for now and finish.

I am stuck in the part: While-------code------ and then I get to my else(the one that represents anything deviating from normal input) and my idea would be first to set a counter.. and use the i++ method to log the times/counts the user has reacher this output, set it i<=3 allowing the user to try again, and then i==3 break at that point... I have the while setup but unsure of the code needed for this.. I'll be digging through my book.


something like this?

Code
//Prompts the user for a double. if the user fails to supply a correct double after 3 attempts, the program exits
public double readDouble(Scanner console)
{
boolean correct = false;
int attempts = 0;
double number = 0.0;
do
{
try
{
System.out.println("Please enter a double: ");
String in = console.nextLine();
number = Double.parseDouble(in);
correct = true;
}catch(NumberFormatException nfe)
{
System.out.println("ERROR: You did not enter a double");
correct = false;
}

attempts++;
}while(!correct && attempts < 3);

if(attempts == 3)
System.exit(1); //exit with 1 to indicate there was an error

return number;
}
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 08:46pm
Code
import java.util.Scanner;


public class Calc {

public static void main(String[] args) {
// TODO Auto-generated method stub

int count;
count = 0;
Scanner input = new Scanner(System.in);
System.out.println("Menu: Welcome to Basic Calculator \n "
+ "1. Add \n "
+ "2. Subtract \n "
+ "3. Divide \n "
+ "4. Multiply \n "
+ "5. Generate Random Number \n "
+ "6. Quit \n ");
//Menu for Operations

String in = input.next();
char oper = in.charAt(0);


while (count < 3) {


if (oper == '6') {
System.out.println("Your have chosen to Quit. Thank you for using Basic Calculator!");
System.exit(0);

}

else





if (oper == '1') {
in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 + num2;
System.out.println("Your answer is: " + result);


} else if(oper == '5') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = (num1 * num2 + num1 + num2 + 77);
System.out.println("Your answer is: " + result);


} else if(oper == '2') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 - num2;
System.out.println("Your answer is: " + result);


} else if(oper == '4') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 * num2;
System.out.println("Your answer is: " + result);


} else if(oper == '3') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 / num2;
System.out.println("Your answer is: " + result);
}

else
System.out.println("Sorry, you have entered an invlaid option. Please try again");
System.out.println("You have: " + count + "trys remaining");
count++;



}

}
{

}

}


Thats what i have so far


Edit:

Okay, I get a better idea of what's going on.. Can you illustrate it by using a bit of what i have? I am unsure of how to incorporate that into what I already have

This post was edited by axid3nt on Jan 25 2015 08:48pm
Member
Posts: 1
Joined: Jan 27 2015
Gold: 0.00
Jan 27 2015 10:50am
Hello guys! :)

This post was edited by Cache1989 on Jan 27 2015 10:50am
Go Back To Programming & Development Topic List
Prev123
Add Reply New Topic New Poll